1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config-parser.h"
28 #include <dbus/dbus-list.h>
29 #include <dbus/dbus-internals.h>
55 /* we ignore policies for unknown groups/users */
70 unsigned int had_content : 1;
76 unsigned int ignore_missing : 1;
77 unsigned int if_selinux_enabled : 1;
78 unsigned int selinux_root_relative : 1;
84 unsigned long gid_uid_or_at_console;
98 * Parser for bus configuration file.
100 struct BusConfigParser
102 int refcount; /**< Reference count */
104 DBusString basedir; /**< Directory we resolve paths relative to */
106 DBusList *stack; /**< stack of Element */
108 char *user; /**< user to run as */
110 char *bus_type; /**< Message bus type */
112 DBusList *listen_on; /**< List of addresses to listen to */
114 DBusList *mechanisms; /**< Auth mechanisms */
116 DBusList *service_dirs; /**< Directories to look for services in */
118 DBusList *conf_dirs; /**< Directories to look for policy configuration in */
120 BusPolicy *policy; /**< Security policy */
122 BusLimits limits; /**< Limits */
124 char *pidfile; /**< PID file */
126 DBusList *included_files; /**< Included files stack */
128 DBusHashTable *service_context_table; /**< Map service names to SELinux contexts */
130 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
132 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
136 element_type_to_name (ElementType type)
142 case ELEMENT_BUSCONFIG:
144 case ELEMENT_INCLUDE:
162 case ELEMENT_PIDFILE:
164 case ELEMENT_SERVICEDIR:
166 case ELEMENT_INCLUDEDIR:
170 case ELEMENT_SELINUX:
172 case ELEMENT_ASSOCIATE:
176 _dbus_assert_not_reached ("bad element type");
182 push_element (BusConfigParser *parser,
187 _dbus_assert (type != ELEMENT_NONE);
189 e = dbus_new0 (Element, 1);
193 if (!_dbus_list_append (&parser->stack, e))
205 element_free (Element *e)
207 if (e->type == ELEMENT_LIMIT)
208 dbus_free (e->d.limit.name);
214 pop_element (BusConfigParser *parser)
218 e = _dbus_list_pop_last (&parser->stack);
224 peek_element (BusConfigParser *parser)
228 e = _dbus_list_get_last (&parser->stack);
234 top_element_type (BusConfigParser *parser)
238 e = _dbus_list_get_last (&parser->stack);
247 merge_service_context_hash (DBusHashTable *dest,
252 _dbus_hash_iter_init (from, &iter);
253 while (_dbus_hash_iter_next (&iter))
255 const char *service = _dbus_hash_iter_get_string_key (&iter);
256 const char *context = _dbus_hash_iter_get_value (&iter);
260 service_copy = _dbus_strdup (service);
261 if (service_copy == NULL)
263 context_copy = _dbus_strdup (context);
264 if (context_copy == NULL)
267 if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
275 merge_included (BusConfigParser *parser,
276 BusConfigParser *included,
281 if (!bus_policy_merge (parser->policy,
288 if (!merge_service_context_hash (parser->service_context_table,
289 included->service_context_table))
295 if (included->user != NULL)
297 dbus_free (parser->user);
298 parser->user = included->user;
299 included->user = NULL;
302 if (included->bus_type != NULL)
304 dbus_free (parser->bus_type);
305 parser->bus_type = included->bus_type;
306 included->bus_type = NULL;
312 if (included->pidfile != NULL)
314 dbus_free (parser->pidfile);
315 parser->pidfile = included->pidfile;
316 included->pidfile = NULL;
319 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
320 _dbus_list_append_link (&parser->listen_on, link);
322 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
323 _dbus_list_append_link (&parser->mechanisms, link);
325 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
326 _dbus_list_append_link (&parser->service_dirs, link);
328 while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
329 _dbus_list_append_link (&parser->conf_dirs, link);
335 seen_include (BusConfigParser *parser,
336 const DBusString *file)
340 iter = parser->included_files;
343 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
346 iter = _dbus_list_get_next_link (&parser->included_files, iter);
353 bus_config_parser_new (const DBusString *basedir,
354 dbus_bool_t is_toplevel,
355 const BusConfigParser *parent)
357 BusConfigParser *parser;
359 parser = dbus_new0 (BusConfigParser, 1);
363 parser->is_toplevel = !!is_toplevel;
365 if (!_dbus_string_init (&parser->basedir))
371 if (((parser->policy = bus_policy_new ()) == NULL) ||
372 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
373 ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
375 dbus_free)) == NULL))
378 bus_policy_unref (parser->policy);
380 _dbus_string_free (&parser->basedir);
388 /* Initialize the parser's limits from the parent. */
389 parser->limits = parent->limits;
391 /* Use the parent's list of included_files to avoid
392 circular inclusions. */
393 parser->included_files = parent->included_files;
398 /* Make up some numbers! woot! */
399 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
400 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
401 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
403 /* Making this long means the user has to wait longer for an error
404 * message if something screws up, but making it too short means
405 * they might see a false failure.
407 parser->limits.activation_timeout = 25000; /* 25 seconds */
409 /* Making this long risks making a DOS attack easier, but too short
410 * and legitimate auth will fail. If interactive auth (ask user for
411 * password) is allowed, then potentially it has to be quite long.
413 parser->limits.auth_timeout = 30000; /* 30 seconds */
415 parser->limits.max_incomplete_connections = 32;
416 parser->limits.max_connections_per_user = 128;
418 /* Note that max_completed_connections / max_connections_per_user
419 * is the number of users that would have to work together to
420 * DOS all the other users.
422 parser->limits.max_completed_connections = 1024;
424 parser->limits.max_pending_activations = 256;
425 parser->limits.max_services_per_connection = 256;
427 parser->limits.max_match_rules_per_connection = 512;
429 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
430 parser->limits.max_replies_per_connection = 32;
433 parser->refcount = 1;
439 bus_config_parser_ref (BusConfigParser *parser)
441 _dbus_assert (parser->refcount > 0);
443 parser->refcount += 1;
449 bus_config_parser_unref (BusConfigParser *parser)
451 _dbus_assert (parser->refcount > 0);
453 parser->refcount -= 1;
455 if (parser->refcount == 0)
457 while (parser->stack != NULL)
458 pop_element (parser);
460 dbus_free (parser->user);
461 dbus_free (parser->bus_type);
462 dbus_free (parser->pidfile);
464 _dbus_list_foreach (&parser->listen_on,
465 (DBusForeachFunction) dbus_free,
468 _dbus_list_clear (&parser->listen_on);
470 _dbus_list_foreach (&parser->service_dirs,
471 (DBusForeachFunction) dbus_free,
474 _dbus_list_clear (&parser->service_dirs);
476 _dbus_list_foreach (&parser->conf_dirs,
477 (DBusForeachFunction) dbus_free,
480 _dbus_list_clear (&parser->conf_dirs);
482 _dbus_list_foreach (&parser->mechanisms,
483 (DBusForeachFunction) dbus_free,
486 _dbus_list_clear (&parser->mechanisms);
488 _dbus_string_free (&parser->basedir);
491 bus_policy_unref (parser->policy);
493 if (parser->service_context_table)
494 _dbus_hash_table_unref (parser->service_context_table);
501 bus_config_parser_check_doctype (BusConfigParser *parser,
505 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
507 if (strcmp (doctype, "busconfig") != 0)
509 dbus_set_error (error,
511 "Configuration file has the wrong document type %s",
526 locate_attributes (BusConfigParser *parser,
527 const char *element_name,
528 const char **attribute_names,
529 const char **attribute_values,
531 const char *first_attribute_name,
532 const char **first_attribute_retloc,
540 LocateAttr attrs[MAX_ATTRS];
544 _dbus_assert (first_attribute_name != NULL);
545 _dbus_assert (first_attribute_retloc != NULL);
550 attrs[0].name = first_attribute_name;
551 attrs[0].retloc = first_attribute_retloc;
552 *first_attribute_retloc = NULL;
554 va_start (args, first_attribute_retloc);
556 name = va_arg (args, const char*);
557 retloc = va_arg (args, const char**);
561 _dbus_assert (retloc != NULL);
562 _dbus_assert (n_attrs < MAX_ATTRS);
564 attrs[n_attrs].name = name;
565 attrs[n_attrs].retloc = retloc;
569 name = va_arg (args, const char*);
570 retloc = va_arg (args, const char**);
579 while (attribute_names[i])
588 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
590 retloc = attrs[j].retloc;
594 dbus_set_error (error, DBUS_ERROR_FAILED,
595 "Attribute \"%s\" repeated twice on the same <%s> element",
596 attrs[j].name, element_name);
601 *retloc = attribute_values[i];
610 dbus_set_error (error, DBUS_ERROR_FAILED,
611 "Attribute \"%s\" is invalid on <%s> element in this context",
612 attribute_names[i], element_name);
625 check_no_attributes (BusConfigParser *parser,
626 const char *element_name,
627 const char **attribute_names,
628 const char **attribute_values,
631 if (attribute_names[0] != NULL)
633 dbus_set_error (error, DBUS_ERROR_FAILED,
634 "Attribute \"%s\" is invalid on <%s> element in this context",
635 attribute_names[0], element_name);
643 start_busconfig_child (BusConfigParser *parser,
644 const char *element_name,
645 const char **attribute_names,
646 const char **attribute_values,
649 if (strcmp (element_name, "user") == 0)
651 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
654 if (push_element (parser, ELEMENT_USER) == NULL)
662 else if (strcmp (element_name, "type") == 0)
664 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
667 if (push_element (parser, ELEMENT_TYPE) == NULL)
675 else if (strcmp (element_name, "fork") == 0)
677 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
680 if (push_element (parser, ELEMENT_FORK) == NULL)
690 else if (strcmp (element_name, "pidfile") == 0)
692 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
695 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
703 else if (strcmp (element_name, "listen") == 0)
705 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
708 if (push_element (parser, ELEMENT_LISTEN) == NULL)
716 else if (strcmp (element_name, "auth") == 0)
718 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
721 if (push_element (parser, ELEMENT_AUTH) == NULL)
729 else if (strcmp (element_name, "includedir") == 0)
731 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
734 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
742 else if (strcmp (element_name, "servicedir") == 0)
744 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
747 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
755 else if (strcmp (element_name, "include") == 0)
758 const char *if_selinux_enabled;
759 const char *ignore_missing;
760 const char *selinux_root_relative;
762 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
768 e->d.include.ignore_missing = FALSE;
769 e->d.include.if_selinux_enabled = FALSE;
770 e->d.include.selinux_root_relative = FALSE;
772 if (!locate_attributes (parser, "include",
776 "ignore_missing", &ignore_missing,
777 "if_selinux_enabled", &if_selinux_enabled,
778 "selinux_root_relative", &selinux_root_relative,
782 if (ignore_missing != NULL)
784 if (strcmp (ignore_missing, "yes") == 0)
785 e->d.include.ignore_missing = TRUE;
786 else if (strcmp (ignore_missing, "no") == 0)
787 e->d.include.ignore_missing = FALSE;
790 dbus_set_error (error, DBUS_ERROR_FAILED,
791 "ignore_missing attribute must have value \"yes\" or \"no\"");
796 if (if_selinux_enabled != NULL)
798 if (strcmp (if_selinux_enabled, "yes") == 0)
799 e->d.include.if_selinux_enabled = TRUE;
800 else if (strcmp (if_selinux_enabled, "no") == 0)
801 e->d.include.if_selinux_enabled = FALSE;
804 dbus_set_error (error, DBUS_ERROR_FAILED,
805 "if_selinux_enabled attribute must have value"
806 " \"yes\" or \"no\"");
811 if (selinux_root_relative != NULL)
813 if (strcmp (selinux_root_relative, "yes") == 0)
814 e->d.include.selinux_root_relative = TRUE;
815 else if (strcmp (selinux_root_relative, "no") == 0)
816 e->d.include.selinux_root_relative = FALSE;
819 dbus_set_error (error, DBUS_ERROR_FAILED,
820 "selinux_root_relative attribute must have value"
821 " \"yes\" or \"no\"");
828 else if (strcmp (element_name, "policy") == 0)
834 const char *at_console;
836 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
842 e->d.policy.type = POLICY_IGNORED;
844 if (!locate_attributes (parser, "policy",
851 "at_console", &at_console,
855 if (((context && user) ||
856 (context && group) ||
857 (context && at_console)) ||
859 (user && at_console)) ||
860 (group && at_console) ||
861 !(context || user || group || at_console))
863 dbus_set_error (error, DBUS_ERROR_FAILED,
864 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
870 if (strcmp (context, "default") == 0)
872 e->d.policy.type = POLICY_DEFAULT;
874 else if (strcmp (context, "mandatory") == 0)
876 e->d.policy.type = POLICY_MANDATORY;
880 dbus_set_error (error, DBUS_ERROR_FAILED,
881 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
886 else if (user != NULL)
889 _dbus_string_init_const (&username, user);
891 if (_dbus_get_user_id (&username,
892 &e->d.policy.gid_uid_or_at_console))
893 e->d.policy.type = POLICY_USER;
895 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
898 else if (group != NULL)
900 DBusString group_name;
901 _dbus_string_init_const (&group_name, group);
903 if (_dbus_get_group_id (&group_name,
904 &e->d.policy.gid_uid_or_at_console))
905 e->d.policy.type = POLICY_GROUP;
907 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
910 else if (at_console != NULL)
913 t = (strcmp (at_console, "true") == 0);
914 if (t || strcmp (at_console, "false") == 0)
916 e->d.policy.gid_uid_or_at_console = t;
917 e->d.policy.type = POLICY_CONSOLE;
921 dbus_set_error (error, DBUS_ERROR_FAILED,
922 "Unknown value \"%s\" for at_console in message bus configuration file",
930 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
935 else if (strcmp (element_name, "limit") == 0)
940 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
946 if (!locate_attributes (parser, "limit",
956 dbus_set_error (error, DBUS_ERROR_FAILED,
957 "<limit> element must have a \"name\" attribute");
961 e->d.limit.name = _dbus_strdup (name);
962 if (e->d.limit.name == NULL)
970 else if (strcmp (element_name, "selinux") == 0)
972 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
975 if (push_element (parser, ELEMENT_SELINUX) == NULL)
985 dbus_set_error (error, DBUS_ERROR_FAILED,
986 "Element <%s> not allowed inside <%s> in configuration file",
987 element_name, "busconfig");
993 append_rule_from_element (BusConfigParser *parser,
994 const char *element_name,
995 const char **attribute_names,
996 const char **attribute_values,
1000 const char *send_interface;
1001 const char *send_member;
1002 const char *send_error;
1003 const char *send_destination;
1004 const char *send_path;
1005 const char *send_type;
1006 const char *receive_interface;
1007 const char *receive_member;
1008 const char *receive_error;
1009 const char *receive_sender;
1010 const char *receive_path;
1011 const char *receive_type;
1012 const char *eavesdrop;
1013 const char *send_requested_reply;
1014 const char *receive_requested_reply;
1019 BusPolicyRule *rule;
1021 if (!locate_attributes (parser, element_name,
1025 "send_interface", &send_interface,
1026 "send_member", &send_member,
1027 "send_error", &send_error,
1028 "send_destination", &send_destination,
1029 "send_path", &send_path,
1030 "send_type", &send_type,
1031 "receive_interface", &receive_interface,
1032 "receive_member", &receive_member,
1033 "receive_error", &receive_error,
1034 "receive_sender", &receive_sender,
1035 "receive_path", &receive_path,
1036 "receive_type", &receive_type,
1037 "eavesdrop", &eavesdrop,
1038 "send_requested_reply", &send_requested_reply,
1039 "receive_requested_reply", &receive_requested_reply,
1046 if (!(send_interface || send_member || send_error || send_destination ||
1047 send_type || send_path ||
1048 receive_interface || receive_member || receive_error || receive_sender ||
1049 receive_type || receive_path || eavesdrop ||
1050 send_requested_reply || receive_requested_reply ||
1051 own || user || group))
1053 dbus_set_error (error, DBUS_ERROR_FAILED,
1054 "Element <%s> must have one or more attributes",
1059 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1060 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1062 dbus_set_error (error, DBUS_ERROR_FAILED,
1063 "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.",
1068 /* Allowed combinations of elements are:
1070 * base, must be all send or all receive:
1073 * interface + member
1076 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1077 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1079 * user, group, own must occur alone
1081 * Pretty sure the below stuff is broken, FIXME think about it more.
1084 if (((send_interface && send_error) ||
1085 (send_interface && receive_interface) ||
1086 (send_interface && receive_member) ||
1087 (send_interface && receive_error) ||
1088 (send_interface && receive_sender) ||
1089 (send_interface && eavesdrop) ||
1090 (send_interface && receive_requested_reply) ||
1091 (send_interface && own) ||
1092 (send_interface && user) ||
1093 (send_interface && group)) ||
1095 ((send_member && send_error) ||
1096 (send_member && receive_interface) ||
1097 (send_member && receive_member) ||
1098 (send_member && receive_error) ||
1099 (send_member && receive_sender) ||
1100 (send_member && eavesdrop) ||
1101 (send_member && receive_requested_reply) ||
1102 (send_member && own) ||
1103 (send_member && user) ||
1104 (send_member && group)) ||
1106 ((send_error && receive_interface) ||
1107 (send_error && receive_member) ||
1108 (send_error && receive_error) ||
1109 (send_error && receive_sender) ||
1110 (send_error && eavesdrop) ||
1111 (send_error && receive_requested_reply) ||
1112 (send_error && own) ||
1113 (send_error && user) ||
1114 (send_error && group)) ||
1116 ((send_destination && receive_interface) ||
1117 (send_destination && receive_member) ||
1118 (send_destination && receive_error) ||
1119 (send_destination && receive_sender) ||
1120 (send_destination && eavesdrop) ||
1121 (send_destination && receive_requested_reply) ||
1122 (send_destination && own) ||
1123 (send_destination && user) ||
1124 (send_destination && group)) ||
1126 ((send_type && receive_interface) ||
1127 (send_type && receive_member) ||
1128 (send_type && receive_error) ||
1129 (send_type && receive_sender) ||
1130 (send_type && eavesdrop) ||
1131 (send_type && receive_requested_reply) ||
1132 (send_type && own) ||
1133 (send_type && user) ||
1134 (send_type && group)) ||
1136 ((send_path && receive_interface) ||
1137 (send_path && receive_member) ||
1138 (send_path && receive_error) ||
1139 (send_path && receive_sender) ||
1140 (send_path && eavesdrop) ||
1141 (send_path && receive_requested_reply) ||
1142 (send_path && own) ||
1143 (send_path && user) ||
1144 (send_path && group)) ||
1146 ((send_requested_reply && receive_interface) ||
1147 (send_requested_reply && receive_member) ||
1148 (send_requested_reply && receive_error) ||
1149 (send_requested_reply && receive_sender) ||
1150 (send_requested_reply && eavesdrop) ||
1151 (send_requested_reply && receive_requested_reply) ||
1152 (send_requested_reply && own) ||
1153 (send_requested_reply && user) ||
1154 (send_requested_reply && group)) ||
1156 ((receive_interface && receive_error) ||
1157 (receive_interface && own) ||
1158 (receive_interface && user) ||
1159 (receive_interface && group)) ||
1161 ((receive_member && receive_error) ||
1162 (receive_member && own) ||
1163 (receive_member && user) ||
1164 (receive_member && group)) ||
1166 ((receive_error && own) ||
1167 (receive_error && user) ||
1168 (receive_error && group)) ||
1170 ((eavesdrop && own) ||
1171 (eavesdrop && user) ||
1172 (eavesdrop && group)) ||
1174 ((receive_requested_reply && own) ||
1175 (receive_requested_reply && user) ||
1176 (receive_requested_reply && group)) ||
1183 dbus_set_error (error, DBUS_ERROR_FAILED,
1184 "Invalid combination of attributes on element <%s>",
1191 /* In BusPolicyRule, NULL represents wildcard.
1192 * In the config file, '*' represents it.
1194 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1196 if (send_interface || send_member || send_error || send_destination ||
1197 send_path || send_type || send_requested_reply)
1201 if (IS_WILDCARD (send_interface))
1202 send_interface = NULL;
1203 if (IS_WILDCARD (send_member))
1205 if (IS_WILDCARD (send_error))
1207 if (IS_WILDCARD (send_destination))
1208 send_destination = NULL;
1209 if (IS_WILDCARD (send_path))
1211 if (IS_WILDCARD (send_type))
1214 message_type = DBUS_MESSAGE_TYPE_INVALID;
1215 if (send_type != NULL)
1217 message_type = dbus_message_type_from_string (send_type);
1218 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1220 dbus_set_error (error, DBUS_ERROR_FAILED,
1221 "Bad message type \"%s\"",
1227 if (send_requested_reply &&
1228 !(strcmp (send_requested_reply, "true") == 0 ||
1229 strcmp (send_requested_reply, "false") == 0))
1231 dbus_set_error (error, DBUS_ERROR_FAILED,
1232 "Bad value \"%s\" for %s attribute, must be true or false",
1233 "send_requested_reply", send_requested_reply);
1237 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1241 if (send_requested_reply)
1242 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1244 rule->d.send.message_type = message_type;
1245 rule->d.send.path = _dbus_strdup (send_path);
1246 rule->d.send.interface = _dbus_strdup (send_interface);
1247 rule->d.send.member = _dbus_strdup (send_member);
1248 rule->d.send.error = _dbus_strdup (send_error);
1249 rule->d.send.destination = _dbus_strdup (send_destination);
1250 if (send_path && rule->d.send.path == NULL)
1252 if (send_interface && rule->d.send.interface == NULL)
1254 if (send_member && rule->d.send.member == NULL)
1256 if (send_error && rule->d.send.error == NULL)
1258 if (send_destination && rule->d.send.destination == NULL)
1261 else if (receive_interface || receive_member || receive_error || receive_sender ||
1262 receive_path || receive_type || eavesdrop || receive_requested_reply)
1266 if (IS_WILDCARD (receive_interface))
1267 receive_interface = NULL;
1268 if (IS_WILDCARD (receive_member))
1269 receive_member = NULL;
1270 if (IS_WILDCARD (receive_error))
1271 receive_error = NULL;
1272 if (IS_WILDCARD (receive_sender))
1273 receive_sender = NULL;
1274 if (IS_WILDCARD (receive_path))
1275 receive_path = NULL;
1276 if (IS_WILDCARD (receive_type))
1277 receive_type = NULL;
1279 message_type = DBUS_MESSAGE_TYPE_INVALID;
1280 if (receive_type != NULL)
1282 message_type = dbus_message_type_from_string (receive_type);
1283 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1285 dbus_set_error (error, DBUS_ERROR_FAILED,
1286 "Bad message type \"%s\"",
1294 !(strcmp (eavesdrop, "true") == 0 ||
1295 strcmp (eavesdrop, "false") == 0))
1297 dbus_set_error (error, DBUS_ERROR_FAILED,
1298 "Bad value \"%s\" for %s attribute, must be true or false",
1299 "eavesdrop", eavesdrop);
1303 if (receive_requested_reply &&
1304 !(strcmp (receive_requested_reply, "true") == 0 ||
1305 strcmp (receive_requested_reply, "false") == 0))
1307 dbus_set_error (error, DBUS_ERROR_FAILED,
1308 "Bad value \"%s\" for %s attribute, must be true or false",
1309 "receive_requested_reply", receive_requested_reply);
1313 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1318 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1320 if (receive_requested_reply)
1321 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1323 rule->d.receive.message_type = message_type;
1324 rule->d.receive.path = _dbus_strdup (receive_path);
1325 rule->d.receive.interface = _dbus_strdup (receive_interface);
1326 rule->d.receive.member = _dbus_strdup (receive_member);
1327 rule->d.receive.error = _dbus_strdup (receive_error);
1328 rule->d.receive.origin = _dbus_strdup (receive_sender);
1330 if (receive_path && rule->d.receive.path == NULL)
1332 if (receive_interface && rule->d.receive.interface == NULL)
1334 if (receive_member && rule->d.receive.member == NULL)
1336 if (receive_error && rule->d.receive.error == NULL)
1338 if (receive_sender && rule->d.receive.origin == NULL)
1343 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1347 if (IS_WILDCARD (own))
1350 rule->d.own.service_name = _dbus_strdup (own);
1351 if (own && rule->d.own.service_name == NULL)
1356 if (IS_WILDCARD (user))
1358 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1362 rule->d.user.uid = DBUS_UID_UNSET;
1366 DBusString username;
1369 _dbus_string_init_const (&username, user);
1371 if (_dbus_get_user_id (&username, &uid))
1373 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1377 rule->d.user.uid = uid;
1381 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1382 user, element_name);
1388 if (IS_WILDCARD (group))
1390 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1394 rule->d.group.gid = DBUS_GID_UNSET;
1398 DBusString groupname;
1401 _dbus_string_init_const (&groupname, group);
1403 if (_dbus_get_user_id (&groupname, &gid))
1405 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1409 rule->d.group.gid = gid;
1413 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1414 group, element_name);
1419 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1425 pe = peek_element (parser);
1426 _dbus_assert (pe != NULL);
1427 _dbus_assert (pe->type == ELEMENT_POLICY);
1429 switch (pe->d.policy.type)
1431 case POLICY_IGNORED:
1432 /* drop the rule on the floor */
1435 case POLICY_DEFAULT:
1436 if (!bus_policy_append_default_rule (parser->policy, rule))
1439 case POLICY_MANDATORY:
1440 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1444 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1446 dbus_set_error (error, DBUS_ERROR_FAILED,
1447 "<%s> rule cannot be per-user because it has bus-global semantics",
1452 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1457 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1459 dbus_set_error (error, DBUS_ERROR_FAILED,
1460 "<%s> rule cannot be per-group because it has bus-global semantics",
1465 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1471 case POLICY_CONSOLE:
1472 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1478 bus_policy_rule_unref (rule);
1485 BUS_SET_OOM (error);
1488 bus_policy_rule_unref (rule);
1493 start_policy_child (BusConfigParser *parser,
1494 const char *element_name,
1495 const char **attribute_names,
1496 const char **attribute_values,
1499 if (strcmp (element_name, "allow") == 0)
1501 if (!append_rule_from_element (parser, element_name,
1502 attribute_names, attribute_values,
1506 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1508 BUS_SET_OOM (error);
1514 else if (strcmp (element_name, "deny") == 0)
1516 if (!append_rule_from_element (parser, element_name,
1517 attribute_names, attribute_values,
1521 if (push_element (parser, ELEMENT_DENY) == NULL)
1523 BUS_SET_OOM (error);
1531 dbus_set_error (error, DBUS_ERROR_FAILED,
1532 "Element <%s> not allowed inside <%s> in configuration file",
1533 element_name, "policy");
1539 start_selinux_child (BusConfigParser *parser,
1540 const char *element_name,
1541 const char **attribute_names,
1542 const char **attribute_values,
1545 if (strcmp (element_name, "associate") == 0)
1548 const char *context;
1552 if (!locate_attributes (parser, "associate",
1557 "context", &context,
1561 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1563 BUS_SET_OOM (error);
1567 if (own == NULL || context == NULL)
1569 dbus_set_error (error, DBUS_ERROR_FAILED,
1570 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1574 own_copy = _dbus_strdup (own);
1575 if (own_copy == NULL)
1577 context_copy = _dbus_strdup (context);
1578 if (context_copy == NULL)
1581 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1582 own_copy, context_copy))
1584 BUS_SET_OOM (error);
1592 dbus_set_error (error, DBUS_ERROR_FAILED,
1593 "Element <%s> not allowed inside <%s> in configuration file",
1594 element_name, "selinux");
1600 bus_config_parser_start_element (BusConfigParser *parser,
1601 const char *element_name,
1602 const char **attribute_names,
1603 const char **attribute_values,
1608 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1610 /* printf ("START: %s\n", element_name); */
1612 t = top_element_type (parser);
1614 if (t == ELEMENT_NONE)
1616 if (strcmp (element_name, "busconfig") == 0)
1618 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1621 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1623 BUS_SET_OOM (error);
1631 dbus_set_error (error, DBUS_ERROR_FAILED,
1632 "Unknown element <%s> at root of configuration file",
1637 else if (t == ELEMENT_BUSCONFIG)
1639 return start_busconfig_child (parser, element_name,
1640 attribute_names, attribute_values,
1643 else if (t == ELEMENT_POLICY)
1645 return start_policy_child (parser, element_name,
1646 attribute_names, attribute_values,
1649 else if (t == ELEMENT_SELINUX)
1651 return start_selinux_child (parser, element_name,
1652 attribute_names, attribute_values,
1657 dbus_set_error (error, DBUS_ERROR_FAILED,
1658 "Element <%s> is not allowed in this context",
1665 set_limit (BusConfigParser *parser,
1670 dbus_bool_t must_be_positive;
1671 dbus_bool_t must_be_int;
1673 must_be_int = FALSE;
1674 must_be_positive = FALSE;
1676 if (strcmp (name, "max_incoming_bytes") == 0)
1678 must_be_positive = TRUE;
1679 parser->limits.max_incoming_bytes = value;
1681 else if (strcmp (name, "max_outgoing_bytes") == 0)
1683 must_be_positive = TRUE;
1684 parser->limits.max_outgoing_bytes = value;
1686 else if (strcmp (name, "max_message_size") == 0)
1688 must_be_positive = TRUE;
1689 parser->limits.max_message_size = value;
1691 else if (strcmp (name, "service_start_timeout") == 0)
1693 must_be_positive = TRUE;
1695 parser->limits.activation_timeout = value;
1697 else if (strcmp (name, "auth_timeout") == 0)
1699 must_be_positive = TRUE;
1701 parser->limits.auth_timeout = value;
1703 else if (strcmp (name, "reply_timeout") == 0)
1705 must_be_positive = TRUE;
1707 parser->limits.reply_timeout = value;
1709 else if (strcmp (name, "max_completed_connections") == 0)
1711 must_be_positive = TRUE;
1713 parser->limits.max_completed_connections = value;
1715 else if (strcmp (name, "max_incomplete_connections") == 0)
1717 must_be_positive = TRUE;
1719 parser->limits.max_incomplete_connections = value;
1721 else if (strcmp (name, "max_connections_per_user") == 0)
1723 must_be_positive = TRUE;
1725 parser->limits.max_connections_per_user = value;
1727 else if (strcmp (name, "max_pending_service_starts") == 0)
1729 must_be_positive = TRUE;
1731 parser->limits.max_pending_activations = value;
1733 else if (strcmp (name, "max_names_per_connection") == 0)
1735 must_be_positive = TRUE;
1737 parser->limits.max_services_per_connection = value;
1739 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1741 must_be_positive = TRUE;
1743 parser->limits.max_match_rules_per_connection = value;
1745 else if (strcmp (name, "max_replies_per_connection") == 0)
1747 must_be_positive = TRUE;
1749 parser->limits.max_replies_per_connection = value;
1753 dbus_set_error (error, DBUS_ERROR_FAILED,
1754 "There is no limit called \"%s\"\n",
1759 if (must_be_positive && value < 0)
1761 dbus_set_error (error, DBUS_ERROR_FAILED,
1762 "<limit name=\"%s\"> must be a positive number\n",
1768 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1770 dbus_set_error (error, DBUS_ERROR_FAILED,
1771 "<limit name=\"%s\"> value is too large\n",
1780 bus_config_parser_end_element (BusConfigParser *parser,
1781 const char *element_name,
1788 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1790 /* printf ("END: %s\n", element_name); */
1792 t = top_element_type (parser);
1794 if (t == ELEMENT_NONE)
1796 /* should probably be an assertion failure but
1797 * being paranoid about XML parsers
1799 dbus_set_error (error, DBUS_ERROR_FAILED,
1800 "XML parser ended element with no element on the stack");
1804 n = element_type_to_name (t);
1805 _dbus_assert (n != NULL);
1806 if (strcmp (n, element_name) != 0)
1808 /* should probably be an assertion failure but
1809 * being paranoid about XML parsers
1811 dbus_set_error (error, DBUS_ERROR_FAILED,
1812 "XML element <%s> ended but topmost element on the stack was <%s>",
1817 e = peek_element (parser);
1818 _dbus_assert (e != NULL);
1823 _dbus_assert_not_reached ("element in stack has no type");
1826 case ELEMENT_INCLUDE:
1829 case ELEMENT_LISTEN:
1830 case ELEMENT_PIDFILE:
1832 case ELEMENT_SERVICEDIR:
1833 case ELEMENT_INCLUDEDIR:
1835 if (!e->had_content)
1837 dbus_set_error (error, DBUS_ERROR_FAILED,
1838 "XML element <%s> was expected to have content inside it",
1839 element_type_to_name (e->type));
1843 if (e->type == ELEMENT_LIMIT)
1845 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1851 case ELEMENT_BUSCONFIG:
1852 case ELEMENT_POLICY:
1856 case ELEMENT_SELINUX:
1857 case ELEMENT_ASSOCIATE:
1861 pop_element (parser);
1867 all_whitespace (const DBusString *str)
1871 _dbus_string_skip_white (str, 0, &i);
1873 return i == _dbus_string_get_length (str);
1877 make_full_path (const DBusString *basedir,
1878 const DBusString *filename,
1879 DBusString *full_path)
1881 if (_dbus_path_is_absolute (filename))
1883 return _dbus_string_copy (filename, 0, full_path, 0);
1887 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1890 if (!_dbus_concat_dir_and_file (full_path, filename))
1898 include_file (BusConfigParser *parser,
1899 const DBusString *filename,
1900 dbus_bool_t ignore_missing,
1903 /* FIXME good test case for this would load each config file in the
1904 * test suite both alone, and as an include, and check
1905 * that the result is the same
1907 BusConfigParser *included;
1908 const char *filename_str;
1909 DBusError tmp_error;
1911 dbus_error_init (&tmp_error);
1913 filename_str = _dbus_string_get_const_data (filename);
1915 /* Check to make sure this file hasn't already been included. */
1916 if (seen_include (parser, filename))
1918 dbus_set_error (error, DBUS_ERROR_FAILED,
1919 "Circular inclusion of file '%s'",
1924 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
1926 BUS_SET_OOM (error);
1930 /* Since parser is passed in as the parent, included
1931 inherits parser's limits. */
1932 included = bus_config_load (filename, FALSE, parser, &tmp_error);
1934 _dbus_list_pop_last (&parser->included_files);
1936 if (included == NULL)
1938 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1940 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1943 dbus_error_free (&tmp_error);
1948 dbus_move_error (&tmp_error, error);
1954 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1956 if (!merge_included (parser, included, error))
1958 bus_config_parser_unref (included);
1962 /* Copy included's limits back to parser. */
1963 parser->limits = included->limits;
1965 bus_config_parser_unref (included);
1971 include_dir (BusConfigParser *parser,
1972 const DBusString *dirname,
1975 DBusString filename;
1977 DBusError tmp_error;
1981 if (!_dbus_string_init (&filename))
1983 BUS_SET_OOM (error);
1989 dir = _dbus_directory_open (dirname, error);
1994 dbus_error_init (&tmp_error);
1995 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1997 DBusString full_path;
1999 if (!_dbus_string_init (&full_path))
2001 BUS_SET_OOM (error);
2005 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2007 BUS_SET_OOM (error);
2008 _dbus_string_free (&full_path);
2012 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2014 BUS_SET_OOM (error);
2015 _dbus_string_free (&full_path);
2019 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2021 if (!include_file (parser, &full_path, TRUE, error))
2023 _dbus_string_free (&full_path);
2028 _dbus_string_free (&full_path);
2031 if (dbus_error_is_set (&tmp_error))
2033 dbus_move_error (&tmp_error, error);
2038 if (!_dbus_string_copy_data (dirname, &s))
2040 BUS_SET_OOM (error);
2044 if (!_dbus_list_append (&parser->conf_dirs, s))
2047 BUS_SET_OOM (error);
2054 _dbus_string_free (&filename);
2057 _dbus_directory_close (dir);
2063 bus_config_parser_content (BusConfigParser *parser,
2064 const DBusString *content,
2069 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2075 _dbus_string_get_const_data (content, &c_str);
2077 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2081 e = peek_element (parser);
2084 dbus_set_error (error, DBUS_ERROR_FAILED,
2085 "Text content outside of any XML element in configuration file");
2088 else if (e->had_content)
2090 _dbus_assert_not_reached ("Element had multiple content blocks");
2094 switch (top_element_type (parser))
2097 _dbus_assert_not_reached ("element at top of stack has no type");
2100 case ELEMENT_BUSCONFIG:
2101 case ELEMENT_POLICY:
2105 case ELEMENT_SELINUX:
2106 case ELEMENT_ASSOCIATE:
2107 if (all_whitespace (content))
2111 dbus_set_error (error, DBUS_ERROR_FAILED,
2112 "No text content expected inside XML element %s in configuration file",
2113 element_type_to_name (top_element_type (parser)));
2117 case ELEMENT_PIDFILE:
2121 e->had_content = TRUE;
2123 if (!_dbus_string_copy_data (content, &s))
2126 dbus_free (parser->pidfile);
2127 parser->pidfile = s;
2131 case ELEMENT_INCLUDE:
2133 DBusString full_path, selinux_policy_root;
2135 e->had_content = TRUE;
2137 if (e->d.include.if_selinux_enabled
2138 && !bus_selinux_enabled ())
2141 if (!_dbus_string_init (&full_path))
2144 if (e->d.include.selinux_root_relative)
2146 if (!bus_selinux_get_policy_root ())
2148 dbus_set_error (error, DBUS_ERROR_FAILED,
2149 "Could not determine SELinux policy root for relative inclusion");
2150 _dbus_string_free (&full_path);
2153 _dbus_string_init_const (&selinux_policy_root,
2154 bus_selinux_get_policy_root ());
2155 if (!make_full_path (&selinux_policy_root, content, &full_path))
2157 _dbus_string_free (&full_path);
2161 else if (!make_full_path (&parser->basedir, content, &full_path))
2163 _dbus_string_free (&full_path);
2167 if (!include_file (parser, &full_path,
2168 e->d.include.ignore_missing, error))
2170 _dbus_string_free (&full_path);
2174 _dbus_string_free (&full_path);
2178 case ELEMENT_INCLUDEDIR:
2180 DBusString full_path;
2182 e->had_content = TRUE;
2184 if (!_dbus_string_init (&full_path))
2187 if (!make_full_path (&parser->basedir, content, &full_path))
2189 _dbus_string_free (&full_path);
2193 if (!include_dir (parser, &full_path, error))
2195 _dbus_string_free (&full_path);
2199 _dbus_string_free (&full_path);
2207 e->had_content = TRUE;
2209 if (!_dbus_string_copy_data (content, &s))
2212 dbus_free (parser->user);
2221 e->had_content = TRUE;
2223 if (!_dbus_string_copy_data (content, &s))
2226 dbus_free (parser->bus_type);
2227 parser->bus_type = s;
2231 case ELEMENT_LISTEN:
2235 e->had_content = TRUE;
2237 if (!_dbus_string_copy_data (content, &s))
2240 if (!_dbus_list_append (&parser->listen_on,
2253 e->had_content = TRUE;
2255 if (!_dbus_string_copy_data (content, &s))
2258 if (!_dbus_list_append (&parser->mechanisms,
2267 case ELEMENT_SERVICEDIR:
2270 DBusString full_path;
2272 e->had_content = TRUE;
2274 if (!_dbus_string_init (&full_path))
2277 if (!make_full_path (&parser->basedir, content, &full_path))
2279 _dbus_string_free (&full_path);
2283 if (!_dbus_string_copy_data (&full_path, &s))
2285 _dbus_string_free (&full_path);
2289 if (!_dbus_list_append (&parser->service_dirs, s))
2291 _dbus_string_free (&full_path);
2296 _dbus_string_free (&full_path);
2304 e->had_content = TRUE;
2307 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2309 dbus_set_error (error, DBUS_ERROR_FAILED,
2310 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2315 e->d.limit.value = val;
2317 _dbus_verbose ("Loaded value %ld for limit %s\n",
2324 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2328 BUS_SET_OOM (error);
2333 bus_config_parser_finished (BusConfigParser *parser,
2336 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2338 if (parser->stack != NULL)
2340 dbus_set_error (error, DBUS_ERROR_FAILED,
2341 "Element <%s> was not closed in configuration file",
2342 element_type_to_name (top_element_type (parser)));
2347 if (parser->is_toplevel && parser->listen_on == NULL)
2349 dbus_set_error (error, DBUS_ERROR_FAILED,
2350 "Configuration file needs one or more <listen> elements giving addresses");
2358 bus_config_parser_get_user (BusConfigParser *parser)
2360 return parser->user;
2364 bus_config_parser_get_type (BusConfigParser *parser)
2366 return parser->bus_type;
2370 bus_config_parser_get_addresses (BusConfigParser *parser)
2372 return &parser->listen_on;
2376 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2378 return &parser->mechanisms;
2382 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2384 return &parser->service_dirs;
2388 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2390 return &parser->conf_dirs;
2394 bus_config_parser_get_fork (BusConfigParser *parser)
2396 return parser->fork;
2400 bus_config_parser_get_pidfile (BusConfigParser *parser)
2402 return parser->pidfile;
2406 bus_config_parser_steal_policy (BusConfigParser *parser)
2410 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2412 policy = parser->policy;
2414 parser->policy = NULL;
2419 /* Overwrite any limits that were set in the configuration file */
2421 bus_config_parser_get_limits (BusConfigParser *parser,
2424 *limits = parser->limits;
2428 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2430 DBusHashTable *table;
2432 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2434 table = parser->service_context_table;
2436 parser->service_context_table = NULL;
2441 #ifdef DBUS_BUILD_TESTS
2452 do_load (const DBusString *full_path,
2454 dbus_bool_t oom_possible)
2456 BusConfigParser *parser;
2459 dbus_error_init (&error);
2461 parser = bus_config_load (full_path, TRUE, NULL, &error);
2464 _DBUS_ASSERT_ERROR_IS_SET (&error);
2467 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2469 _dbus_verbose ("Failed to load valid file due to OOM\n");
2470 dbus_error_free (&error);
2473 else if (validity == VALID)
2475 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2478 dbus_error_free (&error);
2483 dbus_error_free (&error);
2489 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2491 bus_config_parser_unref (parser);
2493 if (validity == INVALID)
2495 _dbus_warn ("Accepted invalid file\n");
2505 const DBusString *full_path;
2510 check_loader_oom_func (void *data)
2512 LoaderOomData *d = data;
2514 return do_load (d->full_path, d->validity, TRUE);
2518 process_test_valid_subdir (const DBusString *test_base_dir,
2522 DBusString test_directory;
2523 DBusString filename;
2531 if (!_dbus_string_init (&test_directory))
2532 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2534 _dbus_string_init_const (&filename, subdir);
2536 if (!_dbus_string_copy (test_base_dir, 0,
2537 &test_directory, 0))
2538 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2540 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2541 _dbus_assert_not_reached ("couldn't allocate full path");
2543 _dbus_string_free (&filename);
2544 if (!_dbus_string_init (&filename))
2545 _dbus_assert_not_reached ("didn't allocate filename string\n");
2547 dbus_error_init (&error);
2548 dir = _dbus_directory_open (&test_directory, &error);
2551 _dbus_warn ("Could not open %s: %s\n",
2552 _dbus_string_get_const_data (&test_directory),
2554 dbus_error_free (&error);
2558 if (validity == VALID)
2559 printf ("Testing valid files:\n");
2560 else if (validity == INVALID)
2561 printf ("Testing invalid files:\n");
2563 printf ("Testing unknown files:\n");
2566 while (_dbus_directory_get_next_file (dir, &filename, &error))
2568 DBusString full_path;
2571 if (!_dbus_string_init (&full_path))
2572 _dbus_assert_not_reached ("couldn't init string");
2574 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2575 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2577 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2578 _dbus_assert_not_reached ("couldn't concat file to dir");
2580 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2582 _dbus_verbose ("Skipping non-.conf file %s\n",
2583 _dbus_string_get_const_data (&filename));
2584 _dbus_string_free (&full_path);
2588 printf (" %s\n", _dbus_string_get_const_data (&filename));
2590 _dbus_verbose (" expecting %s\n",
2591 validity == VALID ? "valid" :
2592 (validity == INVALID ? "invalid" :
2593 (validity == UNKNOWN ? "unknown" : "???")));
2595 d.full_path = &full_path;
2596 d.validity = validity;
2598 /* FIXME hackaround for an expat problem, see
2599 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2600 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2602 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2603 if (!check_loader_oom_func (&d))
2604 _dbus_assert_not_reached ("test failed");
2606 _dbus_string_free (&full_path);
2609 if (dbus_error_is_set (&error))
2611 _dbus_warn ("Could not get next file in %s: %s\n",
2612 _dbus_string_get_const_data (&test_directory),
2614 dbus_error_free (&error);
2623 _dbus_directory_close (dir);
2624 _dbus_string_free (&test_directory);
2625 _dbus_string_free (&filename);
2631 bools_equal (dbus_bool_t a,
2638 strings_equal_or_both_null (const char *a,
2641 if (a == NULL || b == NULL)
2644 return !strcmp (a, b);
2648 elements_equal (const Element *a,
2651 if (a->type != b->type)
2654 if (!bools_equal (a->had_content, b->had_content))
2660 case ELEMENT_INCLUDE:
2661 if (!bools_equal (a->d.include.ignore_missing,
2662 b->d.include.ignore_missing))
2666 case ELEMENT_POLICY:
2667 if (a->d.policy.type != b->d.policy.type)
2669 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
2674 if (strcmp (a->d.limit.name, b->d.limit.name))
2676 if (a->d.limit.value != b->d.limit.value)
2690 lists_of_elements_equal (DBusList *a,
2699 while (ia != NULL && ib != NULL)
2701 if (elements_equal (ia->data, ib->data))
2703 ia = _dbus_list_get_next_link (&a, ia);
2704 ib = _dbus_list_get_next_link (&b, ib);
2707 return ia == NULL && ib == NULL;
2711 lists_of_c_strings_equal (DBusList *a,
2720 while (ia != NULL && ib != NULL)
2722 if (strcmp (ia->data, ib->data))
2724 ia = _dbus_list_get_next_link (&a, ia);
2725 ib = _dbus_list_get_next_link (&b, ib);
2728 return ia == NULL && ib == NULL;
2732 limits_equal (const BusLimits *a,
2736 (a->max_incoming_bytes == b->max_incoming_bytes
2737 || a->max_outgoing_bytes == b->max_outgoing_bytes
2738 || a->max_message_size == b->max_message_size
2739 || a->activation_timeout == b->activation_timeout
2740 || a->auth_timeout == b->auth_timeout
2741 || a->max_completed_connections == b->max_completed_connections
2742 || a->max_incomplete_connections == b->max_incomplete_connections
2743 || a->max_connections_per_user == b->max_connections_per_user
2744 || a->max_pending_activations == b->max_pending_activations
2745 || a->max_services_per_connection == b->max_services_per_connection
2746 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2747 || a->max_replies_per_connection == b->max_replies_per_connection
2748 || a->reply_timeout == b->reply_timeout);
2752 config_parsers_equal (const BusConfigParser *a,
2753 const BusConfigParser *b)
2755 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2758 if (!lists_of_elements_equal (a->stack, b->stack))
2761 if (!strings_equal_or_both_null (a->user, b->user))
2764 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2767 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2770 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2773 /* FIXME: compare policy */
2775 /* FIXME: compare service selinux ID table */
2777 if (! limits_equal (&a->limits, &b->limits))
2780 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2783 if (! bools_equal (a->fork, b->fork))
2786 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2793 all_are_equiv (const DBusString *target_directory)
2795 DBusString filename;
2797 BusConfigParser *first_parser;
2798 BusConfigParser *parser;
2804 first_parser = NULL;
2808 if (!_dbus_string_init (&filename))
2809 _dbus_assert_not_reached ("didn't allocate filename string");
2811 dbus_error_init (&error);
2812 dir = _dbus_directory_open (target_directory, &error);
2815 _dbus_warn ("Could not open %s: %s\n",
2816 _dbus_string_get_const_data (target_directory),
2818 dbus_error_free (&error);
2822 printf ("Comparing equivalent files:\n");
2825 while (_dbus_directory_get_next_file (dir, &filename, &error))
2827 DBusString full_path;
2829 if (!_dbus_string_init (&full_path))
2830 _dbus_assert_not_reached ("couldn't init string");
2832 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2833 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2835 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2836 _dbus_assert_not_reached ("couldn't concat file to dir");
2838 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2840 _dbus_verbose ("Skipping non-.conf file %s\n",
2841 _dbus_string_get_const_data (&filename));
2842 _dbus_string_free (&full_path);
2846 printf (" %s\n", _dbus_string_get_const_data (&filename));
2848 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2852 _dbus_warn ("Could not load file %s: %s\n",
2853 _dbus_string_get_const_data (&full_path),
2855 _dbus_string_free (&full_path);
2856 dbus_error_free (&error);
2859 else if (first_parser == NULL)
2861 _dbus_string_free (&full_path);
2862 first_parser = parser;
2866 _dbus_string_free (&full_path);
2867 equal = config_parsers_equal (first_parser, parser);
2868 bus_config_parser_unref (parser);
2877 _dbus_string_free (&filename);
2879 bus_config_parser_unref (first_parser);
2881 _dbus_directory_close (dir);
2888 process_test_equiv_subdir (const DBusString *test_base_dir,
2891 DBusString test_directory;
2892 DBusString filename;
2901 if (!_dbus_string_init (&test_directory))
2902 _dbus_assert_not_reached ("didn't allocate test_directory");
2904 _dbus_string_init_const (&filename, subdir);
2906 if (!_dbus_string_copy (test_base_dir, 0,
2907 &test_directory, 0))
2908 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2910 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2911 _dbus_assert_not_reached ("couldn't allocate full path");
2913 _dbus_string_free (&filename);
2914 if (!_dbus_string_init (&filename))
2915 _dbus_assert_not_reached ("didn't allocate filename string");
2917 dbus_error_init (&error);
2918 dir = _dbus_directory_open (&test_directory, &error);
2921 _dbus_warn ("Could not open %s: %s\n",
2922 _dbus_string_get_const_data (&test_directory),
2924 dbus_error_free (&error);
2928 while (_dbus_directory_get_next_file (dir, &filename, &error))
2930 DBusString full_path;
2932 /* Skip CVS's magic directories! */
2933 if (_dbus_string_equal_c_str (&filename, "CVS"))
2936 if (!_dbus_string_init (&full_path))
2937 _dbus_assert_not_reached ("couldn't init string");
2939 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2940 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2942 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2943 _dbus_assert_not_reached ("couldn't concat file to dir");
2945 equal = all_are_equiv (&full_path);
2946 _dbus_string_free (&full_path);
2955 _dbus_string_free (&test_directory);
2956 _dbus_string_free (&filename);
2958 _dbus_directory_close (dir);
2965 bus_config_parser_test (const DBusString *test_data_dir)
2967 if (test_data_dir == NULL ||
2968 _dbus_string_get_length (test_data_dir) == 0)
2970 printf ("No test data\n");
2974 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
2977 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
2980 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
2986 #endif /* DBUS_BUILD_TESTS */