1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* config-parser.c XML-library-agnostic configuration file parser
4 * Copyright (C) 2003, 2004 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "config-parser-common.h"
26 #include "config-parser.h"
32 #include <dbus/dbus-list.h>
33 #include <dbus/dbus-internals.h>
34 #include <dbus/dbus-misc.h>
35 #include <dbus/dbus-sysdeps.h>
40 /* we ignore policies for unknown groups/users */
55 unsigned int had_content : 1;
61 unsigned int ignore_missing : 1;
62 unsigned int if_selinux_enabled : 1;
63 unsigned int selinux_root_relative : 1;
69 unsigned long gid_uid_or_at_console;
83 * Parser for bus configuration file.
85 struct BusConfigParser
87 int refcount; /**< Reference count */
89 DBusString basedir; /**< Directory we resolve paths relative to */
91 DBusList *stack; /**< stack of Element */
93 char *user; /**< user to run as */
95 char *servicehelper; /**< location of the setuid helper */
97 char *bus_type; /**< Message bus type */
99 DBusList *listen_on; /**< List of addresses to listen to */
101 DBusList *mechanisms; /**< Auth mechanisms */
103 DBusList *service_dirs; /**< Directories to look for session services in */
105 DBusList *conf_dirs; /**< Directories to look for policy configuration in */
107 BusPolicy *policy; /**< Security policy */
109 BusLimits limits; /**< Limits */
111 char *pidfile; /**< PID file */
113 DBusList *included_files; /**< Included files stack */
115 DBusHashTable *service_context_table; /**< Map service names to SELinux contexts */
117 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
119 unsigned int syslog : 1; /**< TRUE to enable syslog */
120 unsigned int keep_umask : 1; /**< TRUE to keep original umask when forking */
122 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
124 unsigned int allow_anonymous : 1; /**< TRUE to allow anonymous connections */
128 push_element (BusConfigParser *parser,
133 _dbus_assert (type != ELEMENT_NONE);
135 e = dbus_new0 (Element, 1);
139 if (!_dbus_list_append (&parser->stack, e))
151 element_free (Element *e)
153 if (e->type == ELEMENT_LIMIT)
154 dbus_free (e->d.limit.name);
160 pop_element (BusConfigParser *parser)
164 e = _dbus_list_pop_last (&parser->stack);
170 peek_element (BusConfigParser *parser)
174 e = _dbus_list_get_last (&parser->stack);
180 top_element_type (BusConfigParser *parser)
184 e = _dbus_list_get_last (&parser->stack);
193 merge_service_context_hash (DBusHashTable *dest,
203 _dbus_hash_iter_init (from, &iter);
204 while (_dbus_hash_iter_next (&iter))
206 const char *service = _dbus_hash_iter_get_string_key (&iter);
207 const char *context = _dbus_hash_iter_get_value (&iter);
209 service_copy = _dbus_strdup (service);
210 if (service_copy == NULL)
212 context_copy = _dbus_strdup (context);
213 if (context_copy == NULL)
216 if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
227 dbus_free (service_copy);
230 dbus_free (context_copy);
236 service_dirs_find_dir (DBusList **service_dirs,
241 _dbus_assert (dir != NULL);
243 for (link = *service_dirs; link; link = _dbus_list_get_next_link(service_dirs, link))
245 const char *link_dir;
247 link_dir = (const char *)link->data;
248 if (strcmp (dir, link_dir) == 0)
256 service_dirs_append_unique_or_free (DBusList **service_dirs,
259 if (!service_dirs_find_dir (service_dirs, dir))
260 return _dbus_list_append (service_dirs, dir);
267 service_dirs_append_link_unique_or_free (DBusList **service_dirs,
270 if (!service_dirs_find_dir (service_dirs, dir_link->data))
272 _dbus_list_append_link (service_dirs, dir_link);
276 dbus_free (dir_link->data);
277 _dbus_list_free_link (dir_link);
282 merge_included (BusConfigParser *parser,
283 BusConfigParser *included,
288 if (!bus_policy_merge (parser->policy,
295 if (!merge_service_context_hash (parser->service_context_table,
296 included->service_context_table))
302 if (included->user != NULL)
304 dbus_free (parser->user);
305 parser->user = included->user;
306 included->user = NULL;
309 if (included->bus_type != NULL)
311 dbus_free (parser->bus_type);
312 parser->bus_type = included->bus_type;
313 included->bus_type = NULL;
319 if (included->keep_umask)
320 parser->keep_umask = TRUE;
322 if (included->allow_anonymous)
323 parser->allow_anonymous = TRUE;
325 if (included->pidfile != NULL)
327 dbus_free (parser->pidfile);
328 parser->pidfile = included->pidfile;
329 included->pidfile = NULL;
332 if (included->servicehelper != NULL)
334 dbus_free (parser->servicehelper);
335 parser->servicehelper = included->servicehelper;
336 included->servicehelper = NULL;
339 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
340 _dbus_list_append_link (&parser->listen_on, link);
342 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
343 _dbus_list_append_link (&parser->mechanisms, link);
345 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
346 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
348 while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
349 _dbus_list_append_link (&parser->conf_dirs, link);
355 seen_include (BusConfigParser *parser,
356 const DBusString *file)
360 iter = parser->included_files;
363 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
366 iter = _dbus_list_get_next_link (&parser->included_files, iter);
373 bus_config_parser_new (const DBusString *basedir,
374 dbus_bool_t is_toplevel,
375 const BusConfigParser *parent)
377 BusConfigParser *parser;
379 parser = dbus_new0 (BusConfigParser, 1);
383 parser->is_toplevel = !!is_toplevel;
385 if (!_dbus_string_init (&parser->basedir))
391 if (((parser->policy = bus_policy_new ()) == NULL) ||
392 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
393 ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
395 dbus_free)) == NULL))
398 bus_policy_unref (parser->policy);
400 _dbus_string_free (&parser->basedir);
408 /* Initialize the parser's limits from the parent. */
409 parser->limits = parent->limits;
411 /* Use the parent's list of included_files to avoid
412 circular inclusions. */
413 parser->included_files = parent->included_files;
418 /* Make up some numbers! woot! */
419 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 127;
420 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 127;
421 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
423 /* We set relatively conservative values here since due to the
424 way SCM_RIGHTS works we need to preallocate an array for the
425 maximum number of file descriptors we can receive. Picking a
426 high value here thus translates directly to more memory
428 parser->limits.max_incoming_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS*4;
429 parser->limits.max_outgoing_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS*4;
430 parser->limits.max_message_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS;
432 /* Making this long means the user has to wait longer for an error
433 * message if something screws up, but making it too short means
434 * they might see a false failure.
436 parser->limits.activation_timeout = 25000; /* 25 seconds */
438 /* Making this long risks making a DOS attack easier, but too short
439 * and legitimate auth will fail. If interactive auth (ask user for
440 * password) is allowed, then potentially it has to be quite long.
442 parser->limits.auth_timeout = 30000; /* 30 seconds */
444 /* Do not allow a fd to stay forever in dbus-daemon
445 * https://bugs.freedesktop.org/show_bug.cgi?id=80559
447 parser->limits.pending_fd_timeout = 150000; /* 2.5 minutes */
449 parser->limits.max_incomplete_connections = 64;
450 parser->limits.max_connections_per_user = 256;
452 /* Note that max_completed_connections / max_connections_per_user
453 * is the number of users that would have to work together to
454 * DOS all the other users.
456 parser->limits.max_completed_connections = 2048;
458 parser->limits.max_pending_activations = 512;
459 parser->limits.max_services_per_connection = 512;
461 /* For this one, keep in mind that it isn't only the memory used
462 * by the match rules, but slowdown from linearly walking a big
463 * list of them. A client adding more than this is almost
464 * certainly a bad idea for that reason, and should change to a
465 * smaller number of wider-net match rules - getting every last
466 * message to the bus is probably better than having a thousand
469 parser->limits.max_match_rules_per_connection = 512;
471 parser->limits.reply_timeout = -1; /* never */
473 /* this is effectively a limit on message queue size for messages
474 * that require a reply
476 parser->limits.max_replies_per_connection = 128;
479 parser->refcount = 1;
485 bus_config_parser_ref (BusConfigParser *parser)
487 _dbus_assert (parser->refcount > 0);
489 parser->refcount += 1;
495 bus_config_parser_unref (BusConfigParser *parser)
497 _dbus_assert (parser->refcount > 0);
499 parser->refcount -= 1;
501 if (parser->refcount == 0)
503 while (parser->stack != NULL)
504 pop_element (parser);
506 dbus_free (parser->user);
507 dbus_free (parser->servicehelper);
508 dbus_free (parser->bus_type);
509 dbus_free (parser->pidfile);
511 _dbus_list_foreach (&parser->listen_on,
512 (DBusForeachFunction) dbus_free,
515 _dbus_list_clear (&parser->listen_on);
517 _dbus_list_foreach (&parser->service_dirs,
518 (DBusForeachFunction) dbus_free,
521 _dbus_list_clear (&parser->service_dirs);
523 _dbus_list_foreach (&parser->conf_dirs,
524 (DBusForeachFunction) dbus_free,
527 _dbus_list_clear (&parser->conf_dirs);
529 _dbus_list_foreach (&parser->mechanisms,
530 (DBusForeachFunction) dbus_free,
533 _dbus_list_clear (&parser->mechanisms);
535 _dbus_string_free (&parser->basedir);
538 bus_policy_unref (parser->policy);
540 if (parser->service_context_table)
541 _dbus_hash_table_unref (parser->service_context_table);
548 bus_config_parser_check_doctype (BusConfigParser *parser,
552 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
554 if (strcmp (doctype, "busconfig") != 0)
556 dbus_set_error (error,
558 "Configuration file has the wrong document type %s",
573 locate_attributes (BusConfigParser *parser,
574 const char *element_name,
575 const char **attribute_names,
576 const char **attribute_values,
578 const char *first_attribute_name,
579 const char **first_attribute_retloc,
587 LocateAttr attrs[MAX_ATTRS];
591 _dbus_assert (first_attribute_name != NULL);
592 _dbus_assert (first_attribute_retloc != NULL);
597 attrs[0].name = first_attribute_name;
598 attrs[0].retloc = first_attribute_retloc;
599 *first_attribute_retloc = NULL;
601 va_start (args, first_attribute_retloc);
603 name = va_arg (args, const char*);
604 retloc = va_arg (args, const char**);
608 _dbus_assert (retloc != NULL);
609 _dbus_assert (n_attrs < MAX_ATTRS);
611 attrs[n_attrs].name = name;
612 attrs[n_attrs].retloc = retloc;
616 name = va_arg (args, const char*);
617 retloc = va_arg (args, const char**);
623 while (attribute_names[i])
632 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
634 retloc = attrs[j].retloc;
638 dbus_set_error (error, DBUS_ERROR_FAILED,
639 "Attribute \"%s\" repeated twice on the same <%s> element",
640 attrs[j].name, element_name);
645 *retloc = attribute_values[i];
654 dbus_set_error (error, DBUS_ERROR_FAILED,
655 "Attribute \"%s\" is invalid on <%s> element in this context",
656 attribute_names[i], element_name);
669 check_no_attributes (BusConfigParser *parser,
670 const char *element_name,
671 const char **attribute_names,
672 const char **attribute_values,
675 if (attribute_names[0] != NULL)
677 dbus_set_error (error, DBUS_ERROR_FAILED,
678 "Attribute \"%s\" is invalid on <%s> element in this context",
679 attribute_names[0], element_name);
687 start_busconfig_child (BusConfigParser *parser,
688 const char *element_name,
689 const char **attribute_names,
690 const char **attribute_values,
693 ElementType element_type;
695 element_type = bus_config_parser_element_name_to_type (element_name);
697 if (element_type == ELEMENT_USER)
699 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
702 if (push_element (parser, ELEMENT_USER) == NULL)
710 else if (element_type == ELEMENT_CONFIGTYPE)
712 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
715 if (push_element (parser, ELEMENT_CONFIGTYPE) == NULL)
723 else if (element_type == ELEMENT_FORK)
725 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
728 if (push_element (parser, ELEMENT_FORK) == NULL)
738 else if (element_type == ELEMENT_SYSLOG)
740 if (!check_no_attributes (parser, "syslog", attribute_names, attribute_values, error))
743 if (push_element (parser, ELEMENT_SYSLOG) == NULL)
749 parser->syslog = TRUE;
753 else if (element_type == ELEMENT_KEEP_UMASK)
755 if (!check_no_attributes (parser, "keep_umask", attribute_names, attribute_values, error))
758 if (push_element (parser, ELEMENT_KEEP_UMASK) == NULL)
764 parser->keep_umask = TRUE;
768 else if (element_type == ELEMENT_PIDFILE)
770 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
773 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
781 else if (element_type == ELEMENT_LISTEN)
783 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
786 if (push_element (parser, ELEMENT_LISTEN) == NULL)
794 else if (element_type == ELEMENT_AUTH)
796 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
799 if (push_element (parser, ELEMENT_AUTH) == NULL)
807 else if (element_type == ELEMENT_SERVICEHELPER)
809 if (!check_no_attributes (parser, "servicehelper", attribute_names, attribute_values, error))
812 if (push_element (parser, ELEMENT_SERVICEHELPER) == NULL)
820 else if (element_type == ELEMENT_INCLUDEDIR)
822 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
825 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
833 else if (element_type == ELEMENT_STANDARD_SESSION_SERVICEDIRS)
839 if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
842 if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
848 if (!_dbus_get_standard_session_servicedirs (&dirs))
854 while ((link = _dbus_list_pop_first_link (&dirs)))
855 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
859 else if (element_type == ELEMENT_STANDARD_SYSTEM_SERVICEDIRS)
865 if (!check_no_attributes (parser, "standard_system_servicedirs", attribute_names, attribute_values, error))
868 if (push_element (parser, ELEMENT_STANDARD_SYSTEM_SERVICEDIRS) == NULL)
874 if (!_dbus_get_standard_system_servicedirs (&dirs))
880 while ((link = _dbus_list_pop_first_link (&dirs)))
881 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
885 else if (element_type == ELEMENT_ALLOW_ANONYMOUS)
887 if (!check_no_attributes (parser, "allow_anonymous", attribute_names, attribute_values, error))
890 if (push_element (parser, ELEMENT_ALLOW_ANONYMOUS) == NULL)
896 parser->allow_anonymous = TRUE;
899 else if (element_type == ELEMENT_SERVICEDIR)
901 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
904 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
912 else if (element_type == ELEMENT_INCLUDE)
915 const char *if_selinux_enabled;
916 const char *ignore_missing;
917 const char *selinux_root_relative;
919 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
925 e->d.include.ignore_missing = FALSE;
926 e->d.include.if_selinux_enabled = FALSE;
927 e->d.include.selinux_root_relative = FALSE;
929 if (!locate_attributes (parser, "include",
933 "ignore_missing", &ignore_missing,
934 "if_selinux_enabled", &if_selinux_enabled,
935 "selinux_root_relative", &selinux_root_relative,
939 if (ignore_missing != NULL)
941 if (strcmp (ignore_missing, "yes") == 0)
942 e->d.include.ignore_missing = TRUE;
943 else if (strcmp (ignore_missing, "no") == 0)
944 e->d.include.ignore_missing = FALSE;
947 dbus_set_error (error, DBUS_ERROR_FAILED,
948 "ignore_missing attribute must have value \"yes\" or \"no\"");
953 if (if_selinux_enabled != NULL)
955 if (strcmp (if_selinux_enabled, "yes") == 0)
956 e->d.include.if_selinux_enabled = TRUE;
957 else if (strcmp (if_selinux_enabled, "no") == 0)
958 e->d.include.if_selinux_enabled = FALSE;
961 dbus_set_error (error, DBUS_ERROR_FAILED,
962 "if_selinux_enabled attribute must have value"
963 " \"yes\" or \"no\"");
968 if (selinux_root_relative != NULL)
970 if (strcmp (selinux_root_relative, "yes") == 0)
971 e->d.include.selinux_root_relative = TRUE;
972 else if (strcmp (selinux_root_relative, "no") == 0)
973 e->d.include.selinux_root_relative = FALSE;
976 dbus_set_error (error, DBUS_ERROR_FAILED,
977 "selinux_root_relative attribute must have value"
978 " \"yes\" or \"no\"");
985 else if (element_type == ELEMENT_POLICY)
991 const char *at_console;
993 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
999 e->d.policy.type = POLICY_IGNORED;
1001 if (!locate_attributes (parser, "policy",
1005 "context", &context,
1008 "at_console", &at_console,
1012 if (((context && user) ||
1013 (context && group) ||
1014 (context && at_console)) ||
1016 (user && at_console)) ||
1017 (group && at_console) ||
1018 !(context || user || group || at_console))
1020 dbus_set_error (error, DBUS_ERROR_FAILED,
1021 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
1025 if (context != NULL)
1027 if (strcmp (context, "default") == 0)
1029 e->d.policy.type = POLICY_DEFAULT;
1031 else if (strcmp (context, "mandatory") == 0)
1033 e->d.policy.type = POLICY_MANDATORY;
1037 dbus_set_error (error, DBUS_ERROR_FAILED,
1038 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
1043 else if (user != NULL)
1045 DBusString username;
1046 _dbus_string_init_const (&username, user);
1048 if (_dbus_parse_unix_user_from_config (&username,
1049 &e->d.policy.gid_uid_or_at_console))
1050 e->d.policy.type = POLICY_USER;
1052 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
1055 else if (group != NULL)
1057 DBusString group_name;
1058 _dbus_string_init_const (&group_name, group);
1060 if (_dbus_parse_unix_group_from_config (&group_name,
1061 &e->d.policy.gid_uid_or_at_console))
1062 e->d.policy.type = POLICY_GROUP;
1064 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
1067 else if (at_console != NULL)
1070 t = (strcmp (at_console, "true") == 0);
1071 if (t || strcmp (at_console, "false") == 0)
1073 e->d.policy.gid_uid_or_at_console = t;
1074 e->d.policy.type = POLICY_CONSOLE;
1078 dbus_set_error (error, DBUS_ERROR_FAILED,
1079 "Unknown value \"%s\" for at_console in message bus configuration file",
1087 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
1092 else if (element_type == ELEMENT_LIMIT)
1097 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1099 BUS_SET_OOM (error);
1103 if (!locate_attributes (parser, "limit",
1113 dbus_set_error (error, DBUS_ERROR_FAILED,
1114 "<limit> element must have a \"name\" attribute");
1118 e->d.limit.name = _dbus_strdup (name);
1119 if (e->d.limit.name == NULL)
1121 BUS_SET_OOM (error);
1127 else if (element_type == ELEMENT_SELINUX)
1129 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1132 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1134 BUS_SET_OOM (error);
1140 else if (element_type == ELEMENT_APPARMOR)
1145 if ((e = push_element (parser, ELEMENT_APPARMOR)) == NULL)
1147 BUS_SET_OOM (error);
1151 if (!locate_attributes (parser, "apparmor",
1159 return bus_apparmor_set_mode_from_config (mode, error);
1163 dbus_set_error (error, DBUS_ERROR_FAILED,
1164 "Element <%s> not allowed inside <%s> in configuration file",
1165 element_name, "busconfig");
1171 append_rule_from_element (BusConfigParser *parser,
1172 const char *element_name,
1173 const char **attribute_names,
1174 const char **attribute_values,
1179 const char *send_interface;
1180 const char *send_member;
1181 const char *send_error;
1182 const char *send_destination;
1183 const char *send_path;
1184 const char *send_type;
1185 const char *receive_interface;
1186 const char *receive_member;
1187 const char *receive_error;
1188 const char *receive_sender;
1189 const char *receive_path;
1190 const char *receive_type;
1191 const char *eavesdrop;
1192 const char *send_requested_reply;
1193 const char *receive_requested_reply;
1195 const char *own_prefix;
1199 BusPolicyRule *rule;
1201 if (!locate_attributes (parser, element_name,
1205 "send_interface", &send_interface,
1206 "send_member", &send_member,
1207 "send_error", &send_error,
1208 "send_destination", &send_destination,
1209 "send_path", &send_path,
1210 "send_type", &send_type,
1211 "receive_interface", &receive_interface,
1212 "receive_member", &receive_member,
1213 "receive_error", &receive_error,
1214 "receive_sender", &receive_sender,
1215 "receive_path", &receive_path,
1216 "receive_type", &receive_type,
1217 "eavesdrop", &eavesdrop,
1218 "send_requested_reply", &send_requested_reply,
1219 "receive_requested_reply", &receive_requested_reply,
1221 "own_prefix", &own_prefix,
1228 if (!(send_interface || send_member || send_error || send_destination ||
1229 send_type || send_path ||
1230 receive_interface || receive_member || receive_error || receive_sender ||
1231 receive_type || receive_path || eavesdrop ||
1232 send_requested_reply || receive_requested_reply ||
1233 own || own_prefix || user || group))
1235 dbus_set_error (error, DBUS_ERROR_FAILED,
1236 "Element <%s> must have one or more attributes",
1241 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1242 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1244 dbus_set_error (error, DBUS_ERROR_FAILED,
1245 "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.",
1250 /* Allowed combinations of elements are:
1252 * base, must be all send or all receive:
1255 * interface + member
1258 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1259 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1261 * user, group, own, own_prefix must occur alone
1263 * Pretty sure the below stuff is broken, FIXME think about it more.
1266 if ((send_interface && (send_error ||
1267 receive_interface ||
1271 receive_requested_reply ||
1272 own || own_prefix ||
1276 (send_member && (send_error ||
1277 receive_interface ||
1281 receive_requested_reply ||
1282 own || own_prefix ||
1286 (send_error && (receive_interface ||
1290 receive_requested_reply ||
1291 own || own_prefix ||
1295 (send_destination && (receive_interface ||
1299 receive_requested_reply ||
1300 own || own_prefix ||
1304 (send_type && (receive_interface ||
1308 receive_requested_reply ||
1309 own || own_prefix ||
1313 (send_path && (receive_interface ||
1317 receive_requested_reply ||
1318 own || own_prefix ||
1322 (send_requested_reply && (receive_interface ||
1326 receive_requested_reply ||
1327 own || own_prefix ||
1331 (receive_interface && (receive_error ||
1332 own || own_prefix ||
1336 (receive_member && (receive_error ||
1337 own || own_prefix ||
1341 (receive_error && (own || own_prefix ||
1345 (eavesdrop && (own || own_prefix ||
1349 (receive_requested_reply && (own || own_prefix ||
1353 (own && (own_prefix || user || group)) ||
1355 (own_prefix && (own || user || group)) ||
1359 dbus_set_error (error, DBUS_ERROR_FAILED,
1360 "Invalid combination of attributes on element <%s>",
1367 /* In BusPolicyRule, NULL represents wildcard.
1368 * In the config file, '*' represents it.
1370 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1372 if (send_interface || send_member || send_error || send_destination ||
1373 send_path || send_type || send_requested_reply)
1377 if (IS_WILDCARD (send_interface))
1378 send_interface = NULL;
1379 if (IS_WILDCARD (send_member))
1381 if (IS_WILDCARD (send_error))
1383 if (IS_WILDCARD (send_destination))
1384 send_destination = NULL;
1385 if (IS_WILDCARD (send_path))
1387 if (IS_WILDCARD (send_type))
1390 message_type = DBUS_MESSAGE_TYPE_INVALID;
1391 if (send_type != NULL)
1393 message_type = dbus_message_type_from_string (send_type);
1394 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1396 dbus_set_error (error, DBUS_ERROR_FAILED,
1397 "Bad message type \"%s\"",
1404 !(strcmp (eavesdrop, "true") == 0 ||
1405 strcmp (eavesdrop, "false") == 0))
1407 dbus_set_error (error, DBUS_ERROR_FAILED,
1408 "Bad value \"%s\" for %s attribute, must be true or false",
1409 "eavesdrop", eavesdrop);
1413 if (send_requested_reply &&
1414 !(strcmp (send_requested_reply, "true") == 0 ||
1415 strcmp (send_requested_reply, "false") == 0))
1417 dbus_set_error (error, DBUS_ERROR_FAILED,
1418 "Bad value \"%s\" for %s attribute, must be true or false",
1419 "send_requested_reply", send_requested_reply);
1423 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1428 rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1431 rule->d.send.log = (strcmp (log, "true") == 0);
1433 if (send_requested_reply)
1434 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1436 rule->d.send.message_type = message_type;
1437 rule->d.send.path = _dbus_strdup (send_path);
1438 rule->d.send.interface = _dbus_strdup (send_interface);
1439 rule->d.send.member = _dbus_strdup (send_member);
1440 rule->d.send.error = _dbus_strdup (send_error);
1441 rule->d.send.destination = _dbus_strdup (send_destination);
1442 if (send_path && rule->d.send.path == NULL)
1444 if (send_interface && rule->d.send.interface == NULL)
1446 if (send_member && rule->d.send.member == NULL)
1448 if (send_error && rule->d.send.error == NULL)
1450 if (send_destination && rule->d.send.destination == NULL)
1453 else if (receive_interface || receive_member || receive_error || receive_sender ||
1454 receive_path || receive_type || eavesdrop || receive_requested_reply)
1458 if (IS_WILDCARD (receive_interface))
1459 receive_interface = NULL;
1460 if (IS_WILDCARD (receive_member))
1461 receive_member = NULL;
1462 if (IS_WILDCARD (receive_error))
1463 receive_error = NULL;
1464 if (IS_WILDCARD (receive_sender))
1465 receive_sender = NULL;
1466 if (IS_WILDCARD (receive_path))
1467 receive_path = NULL;
1468 if (IS_WILDCARD (receive_type))
1469 receive_type = NULL;
1471 message_type = DBUS_MESSAGE_TYPE_INVALID;
1472 if (receive_type != NULL)
1474 message_type = dbus_message_type_from_string (receive_type);
1475 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1477 dbus_set_error (error, DBUS_ERROR_FAILED,
1478 "Bad message type \"%s\"",
1486 !(strcmp (eavesdrop, "true") == 0 ||
1487 strcmp (eavesdrop, "false") == 0))
1489 dbus_set_error (error, DBUS_ERROR_FAILED,
1490 "Bad value \"%s\" for %s attribute, must be true or false",
1491 "eavesdrop", eavesdrop);
1495 if (receive_requested_reply &&
1496 !(strcmp (receive_requested_reply, "true") == 0 ||
1497 strcmp (receive_requested_reply, "false") == 0))
1499 dbus_set_error (error, DBUS_ERROR_FAILED,
1500 "Bad value \"%s\" for %s attribute, must be true or false",
1501 "receive_requested_reply", receive_requested_reply);
1505 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1510 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1512 if (receive_requested_reply)
1513 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1515 rule->d.receive.message_type = message_type;
1516 rule->d.receive.path = _dbus_strdup (receive_path);
1517 rule->d.receive.interface = _dbus_strdup (receive_interface);
1518 rule->d.receive.member = _dbus_strdup (receive_member);
1519 rule->d.receive.error = _dbus_strdup (receive_error);
1520 rule->d.receive.origin = _dbus_strdup (receive_sender);
1522 if (receive_path && rule->d.receive.path == NULL)
1524 if (receive_interface && rule->d.receive.interface == NULL)
1526 if (receive_member && rule->d.receive.member == NULL)
1528 if (receive_error && rule->d.receive.error == NULL)
1530 if (receive_sender && rule->d.receive.origin == NULL)
1533 else if (own || own_prefix)
1535 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1541 if (IS_WILDCARD (own))
1544 rule->d.own.prefix = 0;
1545 rule->d.own.service_name = _dbus_strdup (own);
1546 if (own && rule->d.own.service_name == NULL)
1551 rule->d.own.prefix = 1;
1552 rule->d.own.service_name = _dbus_strdup (own_prefix);
1553 if (rule->d.own.service_name == NULL)
1559 if (IS_WILDCARD (user))
1561 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1565 rule->d.user.uid = DBUS_UID_UNSET;
1569 DBusString username;
1572 _dbus_string_init_const (&username, user);
1574 if (_dbus_parse_unix_user_from_config (&username, &uid))
1576 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1580 rule->d.user.uid = uid;
1584 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1585 user, element_name);
1591 if (IS_WILDCARD (group))
1593 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1597 rule->d.group.gid = DBUS_GID_UNSET;
1601 DBusString groupname;
1604 _dbus_string_init_const (&groupname, group);
1606 if (_dbus_parse_unix_group_from_config (&groupname, &gid))
1608 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1612 rule->d.group.gid = gid;
1616 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1617 group, element_name);
1622 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1628 pe = peek_element (parser);
1629 _dbus_assert (pe != NULL);
1630 _dbus_assert (pe->type == ELEMENT_POLICY);
1632 switch (pe->d.policy.type)
1634 case POLICY_IGNORED:
1635 /* drop the rule on the floor */
1638 case POLICY_DEFAULT:
1639 if (!bus_policy_append_default_rule (parser->policy, rule))
1642 case POLICY_MANDATORY:
1643 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1647 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1649 dbus_set_error (error, DBUS_ERROR_FAILED,
1650 "<%s> rule cannot be per-user because it has bus-global semantics",
1655 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1660 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1662 dbus_set_error (error, DBUS_ERROR_FAILED,
1663 "<%s> rule cannot be per-group because it has bus-global semantics",
1668 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1674 case POLICY_CONSOLE:
1675 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1681 bus_policy_rule_unref (rule);
1688 BUS_SET_OOM (error);
1691 bus_policy_rule_unref (rule);
1696 start_policy_child (BusConfigParser *parser,
1697 const char *element_name,
1698 const char **attribute_names,
1699 const char **attribute_values,
1702 if (strcmp (element_name, "allow") == 0)
1704 if (!append_rule_from_element (parser, element_name,
1705 attribute_names, attribute_values,
1709 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1711 BUS_SET_OOM (error);
1717 else if (strcmp (element_name, "deny") == 0)
1719 if (!append_rule_from_element (parser, element_name,
1720 attribute_names, attribute_values,
1724 if (push_element (parser, ELEMENT_DENY) == NULL)
1726 BUS_SET_OOM (error);
1734 dbus_set_error (error, DBUS_ERROR_FAILED,
1735 "Element <%s> not allowed inside <%s> in configuration file",
1736 element_name, "policy");
1742 start_selinux_child (BusConfigParser *parser,
1743 const char *element_name,
1744 const char **attribute_names,
1745 const char **attribute_values,
1752 context_copy = NULL;
1754 if (strcmp (element_name, "associate") == 0)
1757 const char *context;
1759 if (!locate_attributes (parser, "associate",
1764 "context", &context,
1768 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1770 BUS_SET_OOM (error);
1774 if (own == NULL || context == NULL)
1776 dbus_set_error (error, DBUS_ERROR_FAILED,
1777 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1781 own_copy = _dbus_strdup (own);
1782 if (own_copy == NULL)
1784 context_copy = _dbus_strdup (context);
1785 if (context_copy == NULL)
1788 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1789 own_copy, context_copy))
1796 dbus_set_error (error, DBUS_ERROR_FAILED,
1797 "Element <%s> not allowed inside <%s> in configuration file",
1798 element_name, "selinux");
1804 dbus_free (own_copy);
1807 dbus_free (context_copy);
1809 BUS_SET_OOM (error);
1814 bus_config_parser_start_element (BusConfigParser *parser,
1815 const char *element_name,
1816 const char **attribute_names,
1817 const char **attribute_values,
1822 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1824 /* printf ("START: %s\n", element_name); */
1826 t = top_element_type (parser);
1828 if (t == ELEMENT_NONE)
1830 if (strcmp (element_name, "busconfig") == 0)
1832 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1835 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1837 BUS_SET_OOM (error);
1845 dbus_set_error (error, DBUS_ERROR_FAILED,
1846 "Unknown element <%s> at root of configuration file",
1851 else if (t == ELEMENT_BUSCONFIG)
1853 return start_busconfig_child (parser, element_name,
1854 attribute_names, attribute_values,
1857 else if (t == ELEMENT_POLICY)
1859 return start_policy_child (parser, element_name,
1860 attribute_names, attribute_values,
1863 else if (t == ELEMENT_SELINUX)
1865 return start_selinux_child (parser, element_name,
1866 attribute_names, attribute_values,
1871 dbus_set_error (error, DBUS_ERROR_FAILED,
1872 "Element <%s> is not allowed in this context",
1879 set_limit (BusConfigParser *parser,
1884 dbus_bool_t must_be_positive;
1885 dbus_bool_t must_be_int;
1887 must_be_int = FALSE;
1888 must_be_positive = FALSE;
1890 if (strcmp (name, "max_incoming_bytes") == 0)
1892 must_be_positive = TRUE;
1893 parser->limits.max_incoming_bytes = value;
1895 else if (strcmp (name, "max_incoming_unix_fds") == 0)
1897 must_be_positive = TRUE;
1898 parser->limits.max_incoming_unix_fds = value;
1900 else if (strcmp (name, "max_outgoing_bytes") == 0)
1902 must_be_positive = TRUE;
1903 parser->limits.max_outgoing_bytes = value;
1905 else if (strcmp (name, "max_outgoing_unix_fds") == 0)
1907 must_be_positive = TRUE;
1908 parser->limits.max_outgoing_unix_fds = value;
1910 else if (strcmp (name, "max_message_size") == 0)
1912 must_be_positive = TRUE;
1913 parser->limits.max_message_size = value;
1915 else if (strcmp (name, "max_message_unix_fds") == 0)
1917 must_be_positive = TRUE;
1918 parser->limits.max_message_unix_fds = value;
1920 else if (strcmp (name, "service_start_timeout") == 0)
1922 must_be_positive = TRUE;
1924 parser->limits.activation_timeout = value;
1926 else if (strcmp (name, "auth_timeout") == 0)
1928 must_be_positive = TRUE;
1930 parser->limits.auth_timeout = value;
1932 else if (strcmp (name, "pending_fd_timeout") == 0)
1934 must_be_positive = TRUE;
1936 parser->limits.pending_fd_timeout = value;
1938 else if (strcmp (name, "reply_timeout") == 0)
1940 must_be_positive = TRUE;
1942 parser->limits.reply_timeout = value;
1944 else if (strcmp (name, "max_completed_connections") == 0)
1946 must_be_positive = TRUE;
1948 parser->limits.max_completed_connections = value;
1950 else if (strcmp (name, "max_incomplete_connections") == 0)
1952 must_be_positive = TRUE;
1954 parser->limits.max_incomplete_connections = value;
1956 else if (strcmp (name, "max_connections_per_user") == 0)
1958 must_be_positive = TRUE;
1960 parser->limits.max_connections_per_user = value;
1962 else if (strcmp (name, "max_pending_service_starts") == 0)
1964 must_be_positive = TRUE;
1966 parser->limits.max_pending_activations = value;
1968 else if (strcmp (name, "max_names_per_connection") == 0)
1970 must_be_positive = TRUE;
1972 parser->limits.max_services_per_connection = value;
1974 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1976 must_be_positive = TRUE;
1978 parser->limits.max_match_rules_per_connection = value;
1980 else if (strcmp (name, "max_replies_per_connection") == 0)
1982 must_be_positive = TRUE;
1984 parser->limits.max_replies_per_connection = value;
1988 dbus_set_error (error, DBUS_ERROR_FAILED,
1989 "There is no limit called \"%s\"\n",
1994 if (must_be_positive && value < 0)
1996 dbus_set_error (error, DBUS_ERROR_FAILED,
1997 "<limit name=\"%s\"> must be a positive number\n",
2003 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
2005 dbus_set_error (error, DBUS_ERROR_FAILED,
2006 "<limit name=\"%s\"> value is too large\n",
2015 bus_config_parser_end_element (BusConfigParser *parser,
2016 const char *element_name,
2023 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2025 /* printf ("END: %s\n", element_name); */
2027 t = top_element_type (parser);
2029 if (t == ELEMENT_NONE)
2031 /* should probably be an assertion failure but
2032 * being paranoid about XML parsers
2034 dbus_set_error (error, DBUS_ERROR_FAILED,
2035 "XML parser ended element with no element on the stack");
2039 n = bus_config_parser_element_type_to_name (t);
2040 _dbus_assert (n != NULL);
2041 if (strcmp (n, element_name) != 0)
2043 /* should probably be an assertion failure but
2044 * being paranoid about XML parsers
2046 dbus_set_error (error, DBUS_ERROR_FAILED,
2047 "XML element <%s> ended but topmost element on the stack was <%s>",
2052 e = peek_element (parser);
2053 _dbus_assert (e != NULL);
2058 _dbus_assert_not_reached ("element in stack has no type");
2061 case ELEMENT_INCLUDE:
2063 case ELEMENT_CONFIGTYPE:
2064 case ELEMENT_LISTEN:
2065 case ELEMENT_PIDFILE:
2067 case ELEMENT_SERVICEDIR:
2068 case ELEMENT_SERVICEHELPER:
2069 case ELEMENT_INCLUDEDIR:
2071 if (!e->had_content)
2073 dbus_set_error (error, DBUS_ERROR_FAILED,
2074 "XML element <%s> was expected to have content inside it",
2075 bus_config_parser_element_type_to_name (e->type));
2079 if (e->type == ELEMENT_LIMIT)
2081 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
2087 case ELEMENT_BUSCONFIG:
2088 case ELEMENT_POLICY:
2092 case ELEMENT_SYSLOG:
2093 case ELEMENT_KEEP_UMASK:
2094 case ELEMENT_SELINUX:
2095 case ELEMENT_ASSOCIATE:
2096 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2097 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2098 case ELEMENT_ALLOW_ANONYMOUS:
2099 case ELEMENT_APPARMOR:
2103 pop_element (parser);
2109 all_whitespace (const DBusString *str)
2113 _dbus_string_skip_white (str, 0, &i);
2115 return i == _dbus_string_get_length (str);
2119 make_full_path (const DBusString *basedir,
2120 const DBusString *filename,
2121 DBusString *full_path)
2123 if (_dbus_path_is_absolute (filename))
2125 if (!_dbus_string_copy (filename, 0, full_path, 0))
2130 if (!_dbus_string_copy (basedir, 0, full_path, 0))
2133 if (!_dbus_concat_dir_and_file (full_path, filename))
2137 if (!_dbus_replace_install_prefix (full_path))
2144 include_file (BusConfigParser *parser,
2145 const DBusString *filename,
2146 dbus_bool_t ignore_missing,
2149 /* FIXME good test case for this would load each config file in the
2150 * test suite both alone, and as an include, and check
2151 * that the result is the same
2153 BusConfigParser *included;
2154 const char *filename_str;
2155 DBusError tmp_error;
2157 dbus_error_init (&tmp_error);
2159 filename_str = _dbus_string_get_const_data (filename);
2161 /* Check to make sure this file hasn't already been included. */
2162 if (seen_include (parser, filename))
2164 dbus_set_error (error, DBUS_ERROR_FAILED,
2165 "Circular inclusion of file '%s'",
2170 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2172 BUS_SET_OOM (error);
2176 /* Since parser is passed in as the parent, included
2177 inherits parser's limits. */
2178 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2180 _dbus_list_pop_last (&parser->included_files);
2182 if (included == NULL)
2184 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2186 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2189 dbus_error_free (&tmp_error);
2194 dbus_move_error (&tmp_error, error);
2200 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2202 if (!merge_included (parser, included, error))
2204 bus_config_parser_unref (included);
2208 /* Copy included's limits back to parser. */
2209 parser->limits = included->limits;
2211 bus_config_parser_unref (included);
2217 servicehelper_path (BusConfigParser *parser,
2218 const DBusString *filename,
2221 const char *filename_str;
2222 char *servicehelper;
2224 filename_str = _dbus_string_get_const_data (filename);
2226 /* copy to avoid overwriting with NULL on OOM */
2227 servicehelper = _dbus_strdup (filename_str);
2230 if (servicehelper == NULL)
2232 BUS_SET_OOM (error);
2236 /* save the latest servicehelper only if not OOM */
2237 dbus_free (parser->servicehelper);
2238 parser->servicehelper = servicehelper;
2240 /* We don't check whether the helper exists; instead we
2241 * would just fail to ever activate anything if it doesn't.
2242 * This allows an admin to fix the problem if it doesn't exist.
2243 * It also allows the parser test suite to successfully parse
2244 * test cases without installing the helper. ;-)
2251 include_dir (BusConfigParser *parser,
2252 const DBusString *dirname,
2255 DBusString filename;
2257 DBusError tmp_error;
2261 if (!_dbus_string_init (&filename))
2263 BUS_SET_OOM (error);
2269 dir = _dbus_directory_open (dirname, error);
2273 if (dbus_error_has_name (error, DBUS_ERROR_FILE_NOT_FOUND))
2275 dbus_error_free (error);
2282 dbus_error_init (&tmp_error);
2283 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2285 DBusString full_path;
2287 if (!_dbus_string_init (&full_path))
2289 BUS_SET_OOM (error);
2293 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2295 BUS_SET_OOM (error);
2296 _dbus_string_free (&full_path);
2300 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2302 BUS_SET_OOM (error);
2303 _dbus_string_free (&full_path);
2307 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2309 if (!include_file (parser, &full_path, TRUE, error))
2311 if (dbus_error_is_set (error))
2313 /* We use both syslog and stderr here, because this is
2314 * the configuration parser, so we don't yet know whether
2315 * this bus is going to want to write to syslog! Err on
2316 * the side of making sure the message gets to the sysadmin
2318 _dbus_init_system_log ("dbus-daemon",
2319 DBUS_LOG_FLAGS_STDERR | DBUS_LOG_FLAGS_SYSTEM_LOG);
2320 _dbus_log (DBUS_SYSTEM_LOG_INFO,
2321 "Encountered error '%s' while parsing '%s'",
2323 _dbus_string_get_const_data (&full_path));
2324 dbus_error_free (error);
2329 _dbus_string_free (&full_path);
2332 if (dbus_error_is_set (&tmp_error))
2334 dbus_move_error (&tmp_error, error);
2339 if (!_dbus_string_copy_data (dirname, &s))
2341 BUS_SET_OOM (error);
2345 if (!_dbus_list_append (&parser->conf_dirs, s))
2348 BUS_SET_OOM (error);
2356 _dbus_string_free (&filename);
2359 _dbus_directory_close (dir);
2365 bus_config_parser_content (BusConfigParser *parser,
2366 const DBusString *content,
2371 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2377 _dbus_string_get_const_data (content, &c_str);
2379 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2383 e = peek_element (parser);
2386 dbus_set_error (error, DBUS_ERROR_FAILED,
2387 "Text content outside of any XML element in configuration file");
2390 else if (e->had_content)
2392 _dbus_assert_not_reached ("Element had multiple content blocks");
2396 switch (top_element_type (parser))
2399 _dbus_assert_not_reached ("element at top of stack has no type");
2402 case ELEMENT_BUSCONFIG:
2403 case ELEMENT_POLICY:
2407 case ELEMENT_SYSLOG:
2408 case ELEMENT_KEEP_UMASK:
2409 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2410 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2411 case ELEMENT_ALLOW_ANONYMOUS:
2412 case ELEMENT_SELINUX:
2413 case ELEMENT_ASSOCIATE:
2414 case ELEMENT_APPARMOR:
2415 if (all_whitespace (content))
2419 dbus_set_error (error, DBUS_ERROR_FAILED,
2420 "No text content expected inside XML element %s in configuration file",
2421 bus_config_parser_element_type_to_name (top_element_type (parser)));
2425 case ELEMENT_PIDFILE:
2429 e->had_content = TRUE;
2431 if (!_dbus_string_copy_data (content, &s))
2434 dbus_free (parser->pidfile);
2435 parser->pidfile = s;
2439 case ELEMENT_INCLUDE:
2441 DBusString full_path, selinux_policy_root;
2443 e->had_content = TRUE;
2445 if (e->d.include.if_selinux_enabled
2446 && !bus_selinux_enabled ())
2449 if (!_dbus_string_init (&full_path))
2452 if (e->d.include.selinux_root_relative)
2454 if (!bus_selinux_get_policy_root ())
2456 dbus_set_error (error, DBUS_ERROR_FAILED,
2457 "Could not determine SELinux policy root for relative inclusion");
2458 _dbus_string_free (&full_path);
2461 _dbus_string_init_const (&selinux_policy_root,
2462 bus_selinux_get_policy_root ());
2463 if (!make_full_path (&selinux_policy_root, content, &full_path))
2465 _dbus_string_free (&full_path);
2469 else if (!make_full_path (&parser->basedir, content, &full_path))
2471 _dbus_string_free (&full_path);
2475 if (!include_file (parser, &full_path,
2476 e->d.include.ignore_missing, error))
2478 _dbus_string_free (&full_path);
2482 _dbus_string_free (&full_path);
2486 case ELEMENT_SERVICEHELPER:
2488 DBusString full_path;
2490 e->had_content = TRUE;
2492 if (!_dbus_string_init (&full_path))
2495 if (!make_full_path (&parser->basedir, content, &full_path))
2497 _dbus_string_free (&full_path);
2501 if (!servicehelper_path (parser, &full_path, error))
2503 _dbus_string_free (&full_path);
2507 _dbus_string_free (&full_path);
2511 case ELEMENT_INCLUDEDIR:
2513 DBusString full_path;
2515 e->had_content = TRUE;
2517 if (!_dbus_string_init (&full_path))
2520 if (!make_full_path (&parser->basedir, content, &full_path))
2522 _dbus_string_free (&full_path);
2526 if (!include_dir (parser, &full_path, error))
2528 _dbus_string_free (&full_path);
2532 _dbus_string_free (&full_path);
2540 e->had_content = TRUE;
2542 if (!_dbus_string_copy_data (content, &s))
2545 dbus_free (parser->user);
2550 case ELEMENT_CONFIGTYPE:
2554 e->had_content = TRUE;
2556 if (!_dbus_string_copy_data (content, &s))
2559 dbus_free (parser->bus_type);
2560 parser->bus_type = s;
2564 case ELEMENT_LISTEN:
2568 e->had_content = TRUE;
2570 if (!_dbus_string_copy_data (content, &s))
2573 if (!_dbus_list_append (&parser->listen_on,
2586 e->had_content = TRUE;
2588 if (!_dbus_string_copy_data (content, &s))
2591 if (!_dbus_list_append (&parser->mechanisms,
2600 case ELEMENT_SERVICEDIR:
2603 DBusString full_path;
2605 e->had_content = TRUE;
2607 if (!_dbus_string_init (&full_path))
2610 if (!make_full_path (&parser->basedir, content, &full_path))
2612 _dbus_string_free (&full_path);
2616 if (!_dbus_string_copy_data (&full_path, &s))
2618 _dbus_string_free (&full_path);
2622 /* _only_ extra session directories can be specified */
2623 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2625 _dbus_string_free (&full_path);
2630 _dbus_string_free (&full_path);
2638 e->had_content = TRUE;
2641 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2643 dbus_set_error (error, DBUS_ERROR_FAILED,
2644 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2649 e->d.limit.value = val;
2651 _dbus_verbose ("Loaded value %ld for limit %s\n",
2658 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2662 BUS_SET_OOM (error);
2667 bus_config_parser_finished (BusConfigParser *parser,
2670 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2672 if (parser->stack != NULL)
2674 dbus_set_error (error, DBUS_ERROR_FAILED,
2675 "Element <%s> was not closed in configuration file",
2676 bus_config_parser_element_type_to_name (top_element_type (parser)));
2681 if (parser->is_toplevel && parser->listen_on == NULL)
2683 dbus_set_error (error, DBUS_ERROR_FAILED,
2684 "Configuration file needs one or more <listen> elements giving addresses");
2692 bus_config_parser_get_user (BusConfigParser *parser)
2694 return parser->user;
2698 bus_config_parser_get_type (BusConfigParser *parser)
2700 return parser->bus_type;
2704 bus_config_parser_get_addresses (BusConfigParser *parser)
2706 return &parser->listen_on;
2710 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2712 return &parser->mechanisms;
2716 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2718 return &parser->service_dirs;
2722 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2724 return &parser->conf_dirs;
2728 bus_config_parser_get_fork (BusConfigParser *parser)
2730 return parser->fork;
2734 bus_config_parser_get_syslog (BusConfigParser *parser)
2736 return parser->syslog;
2740 bus_config_parser_get_keep_umask (BusConfigParser *parser)
2742 return parser->keep_umask;
2746 bus_config_parser_get_allow_anonymous (BusConfigParser *parser)
2748 return parser->allow_anonymous;
2752 bus_config_parser_get_pidfile (BusConfigParser *parser)
2754 return parser->pidfile;
2758 bus_config_parser_get_servicehelper (BusConfigParser *parser)
2760 return parser->servicehelper;
2764 bus_config_parser_steal_policy (BusConfigParser *parser)
2768 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2770 policy = parser->policy;
2772 parser->policy = NULL;
2777 /* Overwrite any limits that were set in the configuration file */
2779 bus_config_parser_get_limits (BusConfigParser *parser,
2782 *limits = parser->limits;
2786 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2788 DBusHashTable *table;
2790 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2792 table = parser->service_context_table;
2794 parser->service_context_table = NULL;
2799 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
2810 do_check_own_rules (BusPolicy *policy)
2814 dbus_bool_t allowed;
2816 {"org.freedesktop", FALSE},
2817 {"org.freedesktop.ManySystem", FALSE},
2818 {"org.freedesktop.ManySystems", TRUE},
2819 {"org.freedesktop.ManySystems.foo", TRUE},
2820 {"org.freedesktop.ManySystems.foo.bar", TRUE},
2821 {"org.freedesktop.ManySystems2", FALSE},
2822 {"org.freedesktop.ManySystems2.foo", FALSE},
2823 {"org.freedesktop.ManySystems2.foo.bar", FALSE},
2828 while (checks[i].name)
2830 DBusString service_name;
2833 if (!_dbus_string_init (&service_name))
2834 _dbus_assert_not_reached ("couldn't init string");
2835 if (!_dbus_string_append (&service_name, checks[i].name))
2836 _dbus_assert_not_reached ("couldn't append string");
2838 ret = bus_policy_check_can_own (policy, &service_name);
2839 printf (" Check name %s: %s\n", checks[i].name,
2840 ret ? "allowed" : "not allowed");
2841 if (checks[i].allowed && !ret)
2843 _dbus_warn ("Cannot own %s\n", checks[i].name);
2846 if (!checks[i].allowed && ret)
2848 _dbus_warn ("Can own %s\n", checks[i].name);
2851 _dbus_string_free (&service_name);
2860 do_load (const DBusString *full_path,
2862 dbus_bool_t oom_possible,
2863 dbus_bool_t check_own_rules)
2865 BusConfigParser *parser;
2868 dbus_error_init (&error);
2870 parser = bus_config_load (full_path, TRUE, NULL, &error);
2873 _DBUS_ASSERT_ERROR_IS_SET (&error);
2876 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2878 _dbus_verbose ("Failed to load valid file due to OOM\n");
2879 dbus_error_free (&error);
2882 else if (validity == VALID)
2884 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2887 dbus_error_free (&error);
2892 dbus_error_free (&error);
2898 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2900 if (check_own_rules && do_check_own_rules (parser->policy) == FALSE)
2905 bus_config_parser_unref (parser);
2907 if (validity == INVALID)
2909 _dbus_warn ("Accepted invalid file\n");
2919 const DBusString *full_path;
2921 dbus_bool_t check_own_rules;
2925 check_loader_oom_func (void *data)
2927 LoaderOomData *d = data;
2929 return do_load (d->full_path, d->validity, TRUE, d->check_own_rules);
2933 process_test_valid_subdir (const DBusString *test_base_dir,
2937 DBusString test_directory;
2938 DBusString filename;
2946 if (!_dbus_string_init (&test_directory))
2947 _dbus_assert_not_reached ("didn't allocate test_directory");
2949 _dbus_string_init_const (&filename, subdir);
2951 if (!_dbus_string_copy (test_base_dir, 0,
2952 &test_directory, 0))
2953 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2955 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2956 _dbus_assert_not_reached ("couldn't allocate full path");
2958 _dbus_string_free (&filename);
2959 if (!_dbus_string_init (&filename))
2960 _dbus_assert_not_reached ("didn't allocate filename string");
2962 dbus_error_init (&error);
2963 dir = _dbus_directory_open (&test_directory, &error);
2966 _dbus_warn ("Could not open %s: %s\n",
2967 _dbus_string_get_const_data (&test_directory),
2969 dbus_error_free (&error);
2973 if (validity == VALID)
2974 printf ("Testing valid files:\n");
2975 else if (validity == INVALID)
2976 printf ("Testing invalid files:\n");
2978 printf ("Testing unknown files:\n");
2981 while (_dbus_directory_get_next_file (dir, &filename, &error))
2983 DBusString full_path;
2986 if (!_dbus_string_init (&full_path))
2987 _dbus_assert_not_reached ("couldn't init string");
2989 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2990 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2992 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2993 _dbus_assert_not_reached ("couldn't concat file to dir");
2995 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2997 _dbus_verbose ("Skipping non-.conf file %s\n",
2998 _dbus_string_get_const_data (&filename));
2999 _dbus_string_free (&full_path);
3003 printf (" %s\n", _dbus_string_get_const_data (&filename));
3005 _dbus_verbose (" expecting %s\n",
3006 validity == VALID ? "valid" :
3007 (validity == INVALID ? "invalid" :
3008 (validity == UNKNOWN ? "unknown" : "???")));
3010 d.full_path = &full_path;
3011 d.validity = validity;
3012 d.check_own_rules = _dbus_string_ends_with_c_str (&full_path,
3013 "check-own-rules.conf");
3015 /* FIXME hackaround for an expat problem, see
3016 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
3017 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
3019 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
3020 if (!check_loader_oom_func (&d))
3021 _dbus_assert_not_reached ("test failed");
3023 _dbus_string_free (&full_path);
3026 if (dbus_error_is_set (&error))
3028 _dbus_warn ("Could not get next file in %s: %s\n",
3029 _dbus_string_get_const_data (&test_directory),
3031 dbus_error_free (&error);
3040 _dbus_directory_close (dir);
3041 _dbus_string_free (&test_directory);
3042 _dbus_string_free (&filename);
3048 bools_equal (dbus_bool_t a,
3055 strings_equal_or_both_null (const char *a,
3058 if (a == NULL || b == NULL)
3061 return !strcmp (a, b);
3065 elements_equal (const Element *a,
3068 if (a->type != b->type)
3071 if (!bools_equal (a->had_content, b->had_content))
3077 case ELEMENT_INCLUDE:
3078 if (!bools_equal (a->d.include.ignore_missing,
3079 b->d.include.ignore_missing))
3083 case ELEMENT_POLICY:
3084 if (a->d.policy.type != b->d.policy.type)
3086 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
3091 if (strcmp (a->d.limit.name, b->d.limit.name))
3093 if (a->d.limit.value != b->d.limit.value)
3107 lists_of_elements_equal (DBusList *a,
3116 while (ia != NULL && ib != NULL)
3118 if (elements_equal (ia->data, ib->data))
3120 ia = _dbus_list_get_next_link (&a, ia);
3121 ib = _dbus_list_get_next_link (&b, ib);
3124 return ia == NULL && ib == NULL;
3128 lists_of_c_strings_equal (DBusList *a,
3137 while (ia != NULL && ib != NULL)
3139 if (strcmp (ia->data, ib->data))
3141 ia = _dbus_list_get_next_link (&a, ia);
3142 ib = _dbus_list_get_next_link (&b, ib);
3145 return ia == NULL && ib == NULL;
3149 limits_equal (const BusLimits *a,
3153 (a->max_incoming_bytes == b->max_incoming_bytes
3154 || a->max_incoming_unix_fds == b->max_incoming_unix_fds
3155 || a->max_outgoing_bytes == b->max_outgoing_bytes
3156 || a->max_outgoing_unix_fds == b->max_outgoing_unix_fds
3157 || a->max_message_size == b->max_message_size
3158 || a->max_message_unix_fds == b->max_message_unix_fds
3159 || a->activation_timeout == b->activation_timeout
3160 || a->auth_timeout == b->auth_timeout
3161 || a->pending_fd_timeout == b->pending_fd_timeout
3162 || a->max_completed_connections == b->max_completed_connections
3163 || a->max_incomplete_connections == b->max_incomplete_connections
3164 || a->max_connections_per_user == b->max_connections_per_user
3165 || a->max_pending_activations == b->max_pending_activations
3166 || a->max_services_per_connection == b->max_services_per_connection
3167 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
3168 || a->max_replies_per_connection == b->max_replies_per_connection
3169 || a->reply_timeout == b->reply_timeout);
3173 config_parsers_equal (const BusConfigParser *a,
3174 const BusConfigParser *b)
3176 if (!_dbus_string_equal (&a->basedir, &b->basedir))
3179 if (!lists_of_elements_equal (a->stack, b->stack))
3182 if (!strings_equal_or_both_null (a->user, b->user))
3185 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
3188 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
3191 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
3194 /* FIXME: compare policy */
3196 /* FIXME: compare service selinux ID table */
3198 if (! limits_equal (&a->limits, &b->limits))
3201 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
3204 if (! bools_equal (a->fork, b->fork))
3207 if (! bools_equal (a->keep_umask, b->keep_umask))
3210 if (! bools_equal (a->is_toplevel, b->is_toplevel))
3217 all_are_equiv (const DBusString *target_directory)
3219 DBusString filename;
3221 BusConfigParser *first_parser;
3222 BusConfigParser *parser;
3228 first_parser = NULL;
3232 if (!_dbus_string_init (&filename))
3233 _dbus_assert_not_reached ("didn't allocate filename string");
3235 dbus_error_init (&error);
3236 dir = _dbus_directory_open (target_directory, &error);
3239 _dbus_warn ("Could not open %s: %s\n",
3240 _dbus_string_get_const_data (target_directory),
3242 dbus_error_free (&error);
3246 printf ("Comparing equivalent files:\n");
3249 while (_dbus_directory_get_next_file (dir, &filename, &error))
3251 DBusString full_path;
3253 if (!_dbus_string_init (&full_path))
3254 _dbus_assert_not_reached ("couldn't init string");
3256 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
3257 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3259 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3260 _dbus_assert_not_reached ("couldn't concat file to dir");
3262 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3264 _dbus_verbose ("Skipping non-.conf file %s\n",
3265 _dbus_string_get_const_data (&filename));
3266 _dbus_string_free (&full_path);
3270 printf (" %s\n", _dbus_string_get_const_data (&filename));
3272 parser = bus_config_load (&full_path, TRUE, NULL, &error);
3276 _dbus_warn ("Could not load file %s: %s\n",
3277 _dbus_string_get_const_data (&full_path),
3279 _dbus_string_free (&full_path);
3280 dbus_error_free (&error);
3283 else if (first_parser == NULL)
3285 _dbus_string_free (&full_path);
3286 first_parser = parser;
3290 _dbus_string_free (&full_path);
3291 equal = config_parsers_equal (first_parser, parser);
3292 bus_config_parser_unref (parser);
3301 _dbus_string_free (&filename);
3303 bus_config_parser_unref (first_parser);
3305 _dbus_directory_close (dir);
3312 process_test_equiv_subdir (const DBusString *test_base_dir,
3315 DBusString test_directory;
3316 DBusString filename;
3325 if (!_dbus_string_init (&test_directory))
3326 _dbus_assert_not_reached ("didn't allocate test_directory");
3328 _dbus_string_init_const (&filename, subdir);
3330 if (!_dbus_string_copy (test_base_dir, 0,
3331 &test_directory, 0))
3332 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3334 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3335 _dbus_assert_not_reached ("couldn't allocate full path");
3337 _dbus_string_free (&filename);
3338 if (!_dbus_string_init (&filename))
3339 _dbus_assert_not_reached ("didn't allocate filename string");
3341 dbus_error_init (&error);
3342 dir = _dbus_directory_open (&test_directory, &error);
3345 _dbus_warn ("Could not open %s: %s\n",
3346 _dbus_string_get_const_data (&test_directory),
3348 dbus_error_free (&error);
3352 while (_dbus_directory_get_next_file (dir, &filename, &error))
3354 DBusString full_path;
3356 /* Skip CVS's magic directories! */
3357 if (_dbus_string_equal_c_str (&filename, "CVS"))
3360 if (!_dbus_string_init (&full_path))
3361 _dbus_assert_not_reached ("couldn't init string");
3363 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3364 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3366 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3367 _dbus_assert_not_reached ("couldn't concat file to dir");
3369 equal = all_are_equiv (&full_path);
3370 _dbus_string_free (&full_path);
3379 _dbus_string_free (&test_directory);
3380 _dbus_string_free (&filename);
3382 _dbus_directory_close (dir);
3388 static const char *test_session_service_dir_matches[] =
3391 "/testhome/foo/.testlocal/testshare/dbus-1/services",
3392 "/testusr/testlocal/testshare/dbus-1/services",
3393 "/testusr/testshare/dbus-1/services",
3394 DBUS_DATADIR"/dbus-1/services",
3396 /* will be filled in test_default_session_servicedirs() */
3405 test_default_session_servicedirs (void)
3410 DBusString install_root_based;
3412 dbus_bool_t ret = FALSE;
3414 const char *common_progs;
3417 /* On Unix we don't actually use these, but it's easier to handle the
3418 * deallocation if we always allocate them, whether needed or not */
3419 if (!_dbus_string_init (&progs) ||
3420 !_dbus_string_init (&install_root_based))
3421 _dbus_assert_not_reached ("OOM allocating strings");
3424 if (!_dbus_string_append (&install_root_based, DBUS_DATADIR) ||
3425 !_dbus_string_append (&install_root_based, "/dbus-1/services") ||
3426 !_dbus_replace_install_prefix (&install_root_based))
3429 _dbus_assert (_dbus_path_is_absolute (&install_root_based));
3431 test_session_service_dir_matches[0] = _dbus_string_get_const_data (
3432 &install_root_based);
3434 common_progs = _dbus_getenv ("CommonProgramFiles");
3438 if (!_dbus_string_append (&progs, common_progs))
3441 if (!_dbus_string_append (&progs, "/dbus-1/services"))
3444 test_session_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3449 printf ("Testing retrieving the default session service directories\n");
3450 if (!_dbus_get_standard_session_servicedirs (&dirs))
3451 _dbus_assert_not_reached ("couldn't get stardard dirs");
3453 /* make sure our defaults end with share/dbus-1/service */
3454 while ((link = _dbus_list_pop_first_link (&dirs)))
3458 printf (" default service dir: %s\n", (char *)link->data);
3459 _dbus_string_init_const (&path, (char *)link->data);
3460 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/services"))
3462 printf ("error with default session service directories\n");
3463 dbus_free (link->data);
3464 _dbus_list_free_link (link);
3468 dbus_free (link->data);
3469 _dbus_list_free_link (link);
3473 if (!dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3474 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3476 if (!dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3477 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3479 if (!_dbus_get_standard_session_servicedirs (&dirs))
3480 _dbus_assert_not_reached ("couldn't get stardard dirs");
3482 /* make sure we read and parse the env variable correctly */
3484 while ((link = _dbus_list_pop_first_link (&dirs)))
3486 printf (" test service dir: '%s'\n", (char *)link->data);
3487 printf (" current standard service dir: '%s'\n", test_session_service_dir_matches[i]);
3488 if (test_session_service_dir_matches[i] == NULL)
3490 printf ("more directories parsed than in match set\n");
3491 dbus_free (link->data);
3492 _dbus_list_free_link (link);
3496 if (strcmp (test_session_service_dir_matches[i],
3497 (char *)link->data) != 0)
3499 printf ("'%s' directory does not match '%s' in the match set\n",
3501 test_session_service_dir_matches[i]);
3502 dbus_free (link->data);
3503 _dbus_list_free_link (link);
3509 dbus_free (link->data);
3510 _dbus_list_free_link (link);
3513 if (test_session_service_dir_matches[i] != NULL)
3515 printf ("extra data %s in the match set was not matched\n",
3516 test_session_service_dir_matches[i]);
3523 _dbus_string_free (&install_root_based);
3524 _dbus_string_free (&progs);
3528 static const char *test_system_service_dir_matches[] =
3531 "/usr/local/share/dbus-1/system-services",
3532 "/usr/share/dbus-1/system-services",
3534 DBUS_DATADIR"/dbus-1/system-services",
3536 "/lib/dbus-1/system-services",
3546 test_default_system_servicedirs (void)
3552 const char *common_progs;
3556 /* On Unix we don't actually use this variable, but it's easier to handle the
3557 * deallocation if we always allocate it, whether needed or not */
3558 if (!_dbus_string_init (&progs))
3559 _dbus_assert_not_reached ("OOM allocating progs");
3562 common_progs = _dbus_getenv ("CommonProgramFiles");
3566 if (!_dbus_string_append (&progs, common_progs))
3568 _dbus_string_free (&progs);
3572 if (!_dbus_string_append (&progs, "/dbus-1/system-services"))
3574 _dbus_string_free (&progs);
3577 test_system_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3582 printf ("Testing retrieving the default system service directories\n");
3583 if (!_dbus_get_standard_system_servicedirs (&dirs))
3584 _dbus_assert_not_reached ("couldn't get stardard dirs");
3586 /* make sure our defaults end with share/dbus-1/system-service */
3587 while ((link = _dbus_list_pop_first_link (&dirs)))
3591 printf (" default service dir: %s\n", (char *)link->data);
3592 _dbus_string_init_const (&path, (char *)link->data);
3593 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/system-services"))
3595 printf ("error with default system service directories\n");
3596 dbus_free (link->data);
3597 _dbus_list_free_link (link);
3598 _dbus_string_free (&progs);
3602 dbus_free (link->data);
3603 _dbus_list_free_link (link);
3607 if (!dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3608 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3610 if (!dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3611 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3613 if (!_dbus_get_standard_system_servicedirs (&dirs))
3614 _dbus_assert_not_reached ("couldn't get stardard dirs");
3616 /* make sure we read and parse the env variable correctly */
3618 while ((link = _dbus_list_pop_first_link (&dirs)))
3620 printf (" test service dir: %s\n", (char *)link->data);
3621 if (test_system_service_dir_matches[i] == NULL)
3623 printf ("more directories parsed than in match set\n");
3624 dbus_free (link->data);
3625 _dbus_list_free_link (link);
3626 _dbus_string_free (&progs);
3630 if (strcmp (test_system_service_dir_matches[i],
3631 (char *)link->data) != 0)
3633 printf ("%s directory does not match %s in the match set\n",
3635 test_system_service_dir_matches[i]);
3636 dbus_free (link->data);
3637 _dbus_list_free_link (link);
3638 _dbus_string_free (&progs);
3644 dbus_free (link->data);
3645 _dbus_list_free_link (link);
3648 if (test_system_service_dir_matches[i] != NULL)
3650 printf ("extra data %s in the match set was not matched\n",
3651 test_system_service_dir_matches[i]);
3653 _dbus_string_free (&progs);
3657 _dbus_string_free (&progs);
3662 bus_config_parser_test (const DBusString *test_data_dir)
3664 if (test_data_dir == NULL ||
3665 _dbus_string_get_length (test_data_dir) == 0)
3667 printf ("No test data\n");
3671 if (!test_default_session_servicedirs())
3675 printf("default system service dir skipped\n");
3677 if (!test_default_system_servicedirs())
3681 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3685 if (!process_test_valid_subdir (test_data_dir, "valid-config-files-system", VALID))
3689 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3692 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3698 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */