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 */
69 unsigned int had_content : 1;
75 unsigned int ignore_missing : 1;
76 unsigned int selinux_root_relative : 1;
82 unsigned long gid_or_uid;
96 * Parser for bus configuration file.
98 struct BusConfigParser
100 int refcount; /**< Reference count */
102 DBusString basedir; /**< Directory we resolve paths relative to */
104 DBusList *stack; /**< stack of Element */
106 char *user; /**< user to run as */
108 char *bus_type; /**< Message bus type */
110 DBusList *listen_on; /**< List of addresses to listen to */
112 DBusList *mechanisms; /**< Auth mechanisms */
114 DBusList *service_dirs; /**< Directories to look for services in */
116 BusPolicy *policy; /**< Security policy */
118 BusLimits limits; /**< Limits */
120 char *pidfile; /**< PID file */
122 DBusList *included_files; /**< Included files stack */
124 DBusHashTable *service_sid_table; /**< Map service names to SELinux contexts */
126 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
128 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
132 element_type_to_name (ElementType type)
138 case ELEMENT_BUSCONFIG:
140 case ELEMENT_INCLUDE:
158 case ELEMENT_PIDFILE:
160 case ELEMENT_SERVICEDIR:
162 case ELEMENT_INCLUDEDIR:
166 case ELEMENT_SELINUX:
168 case ELEMENT_ASSOCIATE:
172 _dbus_assert_not_reached ("bad element type");
178 push_element (BusConfigParser *parser,
183 _dbus_assert (type != ELEMENT_NONE);
185 e = dbus_new0 (Element, 1);
189 if (!_dbus_list_append (&parser->stack, e))
201 element_free (Element *e)
203 if (e->type == ELEMENT_LIMIT)
204 dbus_free (e->d.limit.name);
210 pop_element (BusConfigParser *parser)
214 e = _dbus_list_pop_last (&parser->stack);
220 peek_element (BusConfigParser *parser)
224 e = _dbus_list_get_last (&parser->stack);
230 top_element_type (BusConfigParser *parser)
234 e = _dbus_list_get_last (&parser->stack);
243 merge_included (BusConfigParser *parser,
244 BusConfigParser *included,
248 DBusHashTable *table;
250 if (!bus_policy_merge (parser->policy,
257 table = bus_selinux_id_table_union (parser->service_sid_table,
258 included->service_sid_table);
265 _dbus_hash_table_unref (parser->service_sid_table);
266 parser->service_sid_table = table;
268 if (included->user != NULL)
270 dbus_free (parser->user);
271 parser->user = included->user;
272 included->user = NULL;
275 if (included->bus_type != NULL)
277 dbus_free (parser->bus_type);
278 parser->bus_type = included->bus_type;
279 included->bus_type = NULL;
285 if (included->pidfile != NULL)
287 dbus_free (parser->pidfile);
288 parser->pidfile = included->pidfile;
289 included->pidfile = NULL;
292 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
293 _dbus_list_append_link (&parser->listen_on, link);
295 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
296 _dbus_list_append_link (&parser->mechanisms, link);
298 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
299 _dbus_list_append_link (&parser->service_dirs, link);
305 seen_include (BusConfigParser *parser,
306 const DBusString *file)
310 iter = parser->included_files;
313 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
316 iter = _dbus_list_get_next_link (&parser->included_files, iter);
323 bus_config_parser_new (const DBusString *basedir,
324 dbus_bool_t is_toplevel,
325 const BusConfigParser *parent)
327 BusConfigParser *parser;
329 parser = dbus_new0 (BusConfigParser, 1);
333 parser->is_toplevel = !!is_toplevel;
335 if (!_dbus_string_init (&parser->basedir))
341 if (((parser->policy = bus_policy_new ()) == NULL) ||
342 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
343 ((parser->service_sid_table = bus_selinux_id_table_new ()) == NULL))
346 bus_policy_unref (parser->policy);
348 _dbus_string_free (&parser->basedir);
356 /* Initialize the parser's limits from the parent. */
357 parser->limits = parent->limits;
359 /* Use the parent's list of included_files to avoid
360 circular inclusions. */
361 parser->included_files = parent->included_files;
366 /* Make up some numbers! woot! */
367 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
368 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
369 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
371 /* Making this long means the user has to wait longer for an error
372 * message if something screws up, but making it too short means
373 * they might see a false failure.
375 parser->limits.activation_timeout = 25000; /* 25 seconds */
377 /* Making this long risks making a DOS attack easier, but too short
378 * and legitimate auth will fail. If interactive auth (ask user for
379 * password) is allowed, then potentially it has to be quite long.
381 parser->limits.auth_timeout = 30000; /* 30 seconds */
383 parser->limits.max_incomplete_connections = 32;
384 parser->limits.max_connections_per_user = 128;
386 /* Note that max_completed_connections / max_connections_per_user
387 * is the number of users that would have to work together to
388 * DOS all the other users.
390 parser->limits.max_completed_connections = 1024;
392 parser->limits.max_pending_activations = 256;
393 parser->limits.max_services_per_connection = 256;
395 parser->limits.max_match_rules_per_connection = 128;
397 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
398 parser->limits.max_replies_per_connection = 32;
401 parser->refcount = 1;
407 bus_config_parser_ref (BusConfigParser *parser)
409 _dbus_assert (parser->refcount > 0);
411 parser->refcount += 1;
417 bus_config_parser_unref (BusConfigParser *parser)
419 _dbus_assert (parser->refcount > 0);
421 parser->refcount -= 1;
423 if (parser->refcount == 0)
425 while (parser->stack != NULL)
426 pop_element (parser);
428 dbus_free (parser->user);
429 dbus_free (parser->bus_type);
430 dbus_free (parser->pidfile);
432 _dbus_list_foreach (&parser->listen_on,
433 (DBusForeachFunction) dbus_free,
436 _dbus_list_clear (&parser->listen_on);
438 _dbus_list_foreach (&parser->service_dirs,
439 (DBusForeachFunction) dbus_free,
442 _dbus_list_clear (&parser->service_dirs);
444 _dbus_list_foreach (&parser->mechanisms,
445 (DBusForeachFunction) dbus_free,
448 _dbus_list_clear (&parser->mechanisms);
450 _dbus_string_free (&parser->basedir);
453 bus_policy_unref (parser->policy);
455 if (parser->service_sid_table)
456 _dbus_hash_table_unref (parser->service_sid_table);
463 bus_config_parser_check_doctype (BusConfigParser *parser,
467 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
469 if (strcmp (doctype, "busconfig") != 0)
471 dbus_set_error (error,
473 "Configuration file has the wrong document type %s",
488 locate_attributes (BusConfigParser *parser,
489 const char *element_name,
490 const char **attribute_names,
491 const char **attribute_values,
493 const char *first_attribute_name,
494 const char **first_attribute_retloc,
502 LocateAttr attrs[MAX_ATTRS];
506 _dbus_assert (first_attribute_name != NULL);
507 _dbus_assert (first_attribute_retloc != NULL);
512 attrs[0].name = first_attribute_name;
513 attrs[0].retloc = first_attribute_retloc;
514 *first_attribute_retloc = NULL;
516 va_start (args, first_attribute_retloc);
518 name = va_arg (args, const char*);
519 retloc = va_arg (args, const char**);
523 _dbus_assert (retloc != NULL);
524 _dbus_assert (n_attrs < MAX_ATTRS);
526 attrs[n_attrs].name = name;
527 attrs[n_attrs].retloc = retloc;
531 name = va_arg (args, const char*);
532 retloc = va_arg (args, const char**);
541 while (attribute_names[i])
550 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
552 retloc = attrs[j].retloc;
556 dbus_set_error (error, DBUS_ERROR_FAILED,
557 "Attribute \"%s\" repeated twice on the same <%s> element",
558 attrs[j].name, element_name);
563 *retloc = attribute_values[i];
572 dbus_set_error (error, DBUS_ERROR_FAILED,
573 "Attribute \"%s\" is invalid on <%s> element in this context",
574 attribute_names[i], element_name);
587 check_no_attributes (BusConfigParser *parser,
588 const char *element_name,
589 const char **attribute_names,
590 const char **attribute_values,
593 if (attribute_names[0] != NULL)
595 dbus_set_error (error, DBUS_ERROR_FAILED,
596 "Attribute \"%s\" is invalid on <%s> element in this context",
597 attribute_names[0], element_name);
605 start_busconfig_child (BusConfigParser *parser,
606 const char *element_name,
607 const char **attribute_names,
608 const char **attribute_values,
611 if (strcmp (element_name, "user") == 0)
613 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
616 if (push_element (parser, ELEMENT_USER) == NULL)
624 else if (strcmp (element_name, "type") == 0)
626 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
629 if (push_element (parser, ELEMENT_TYPE) == NULL)
637 else if (strcmp (element_name, "fork") == 0)
639 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
642 if (push_element (parser, ELEMENT_FORK) == NULL)
652 else if (strcmp (element_name, "pidfile") == 0)
654 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
657 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
665 else if (strcmp (element_name, "listen") == 0)
667 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
670 if (push_element (parser, ELEMENT_LISTEN) == NULL)
678 else if (strcmp (element_name, "auth") == 0)
680 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
683 if (push_element (parser, ELEMENT_AUTH) == NULL)
691 else if (strcmp (element_name, "includedir") == 0)
693 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
696 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
704 else if (strcmp (element_name, "servicedir") == 0)
706 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
709 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
717 else if (strcmp (element_name, "include") == 0)
720 const char *ignore_missing;
721 const char *selinux_root_relative;
723 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
729 e->d.include.ignore_missing = FALSE;
730 e->d.include.selinux_root_relative = FALSE;
732 if (!locate_attributes (parser, "include",
736 "ignore_missing", &ignore_missing,
737 "selinux_root_relative", &selinux_root_relative,
741 if (ignore_missing != NULL)
743 if (strcmp (ignore_missing, "yes") == 0)
744 e->d.include.ignore_missing = TRUE;
745 else if (strcmp (ignore_missing, "no") == 0)
746 e->d.include.ignore_missing = FALSE;
749 dbus_set_error (error, DBUS_ERROR_FAILED,
750 "ignore_missing attribute must have value \"yes\" or \"no\"");
755 if (selinux_root_relative != NULL)
757 if (strcmp (selinux_root_relative, "yes") == 0)
758 e->d.include.selinux_root_relative = TRUE;
759 else if (strcmp (selinux_root_relative, "no") == 0)
760 e->d.include.selinux_root_relative = FALSE;
763 dbus_set_error (error, DBUS_ERROR_FAILED,
764 "selinux_root_relative attribute must have value"
765 " \"yes\" or \"no\"");
772 else if (strcmp (element_name, "policy") == 0)
779 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
785 e->d.policy.type = POLICY_IGNORED;
787 if (!locate_attributes (parser, "policy",
797 if (((context && user) ||
798 (context && group)) ||
800 !(context || user || group))
802 dbus_set_error (error, DBUS_ERROR_FAILED,
803 "<policy> element must have exactly one of (context|user|group) attributes");
809 if (strcmp (context, "default") == 0)
811 e->d.policy.type = POLICY_DEFAULT;
813 else if (strcmp (context, "mandatory") == 0)
815 e->d.policy.type = POLICY_MANDATORY;
819 dbus_set_error (error, DBUS_ERROR_FAILED,
820 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
825 else if (user != NULL)
828 _dbus_string_init_const (&username, user);
830 if (_dbus_get_user_id (&username,
831 &e->d.policy.gid_or_uid))
832 e->d.policy.type = POLICY_USER;
834 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
837 else if (group != NULL)
839 DBusString group_name;
840 _dbus_string_init_const (&group_name, group);
842 if (_dbus_get_group_id (&group_name,
843 &e->d.policy.gid_or_uid))
844 e->d.policy.type = POLICY_GROUP;
846 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
851 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
856 else if (strcmp (element_name, "limit") == 0)
861 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
867 if (!locate_attributes (parser, "limit",
877 dbus_set_error (error, DBUS_ERROR_FAILED,
878 "<limit> element must have a \"name\" attribute");
882 e->d.limit.name = _dbus_strdup (name);
883 if (e->d.limit.name == NULL)
891 else if (strcmp (element_name, "selinux") == 0)
893 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
896 if (push_element (parser, ELEMENT_SELINUX) == NULL)
906 dbus_set_error (error, DBUS_ERROR_FAILED,
907 "Element <%s> not allowed inside <%s> in configuration file",
908 element_name, "busconfig");
914 append_rule_from_element (BusConfigParser *parser,
915 const char *element_name,
916 const char **attribute_names,
917 const char **attribute_values,
921 const char *send_interface;
922 const char *send_member;
923 const char *send_error;
924 const char *send_destination;
925 const char *send_path;
926 const char *send_type;
927 const char *receive_interface;
928 const char *receive_member;
929 const char *receive_error;
930 const char *receive_sender;
931 const char *receive_path;
932 const char *receive_type;
933 const char *eavesdrop;
934 const char *send_requested_reply;
935 const char *receive_requested_reply;
941 if (!locate_attributes (parser, element_name,
945 "send_interface", &send_interface,
946 "send_member", &send_member,
947 "send_error", &send_error,
948 "send_destination", &send_destination,
949 "send_path", &send_path,
950 "send_type", &send_type,
951 "receive_interface", &receive_interface,
952 "receive_member", &receive_member,
953 "receive_error", &receive_error,
954 "receive_sender", &receive_sender,
955 "receive_path", &receive_path,
956 "receive_type", &receive_type,
957 "eavesdrop", &eavesdrop,
958 "send_requested_reply", &send_requested_reply,
959 "receive_requested_reply", &receive_requested_reply,
966 if (!(send_interface || send_member || send_error || send_destination ||
967 send_type || send_path ||
968 receive_interface || receive_member || receive_error || receive_sender ||
969 receive_type || receive_path || eavesdrop ||
970 send_requested_reply || receive_requested_reply ||
971 own || user || group))
973 dbus_set_error (error, DBUS_ERROR_FAILED,
974 "Element <%s> must have one or more attributes",
979 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
980 (receive_member && (receive_interface == NULL && receive_path == NULL)))
982 dbus_set_error (error, DBUS_ERROR_FAILED,
983 "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.",
988 /* Allowed combinations of elements are:
990 * base, must be all send or all receive:
996 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
997 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
999 * user, group, own must occur alone
1001 * Pretty sure the below stuff is broken, FIXME think about it more.
1004 if (((send_interface && send_error) ||
1005 (send_interface && receive_interface) ||
1006 (send_interface && receive_member) ||
1007 (send_interface && receive_error) ||
1008 (send_interface && receive_sender) ||
1009 (send_interface && eavesdrop) ||
1010 (send_interface && receive_requested_reply) ||
1011 (send_interface && own) ||
1012 (send_interface && user) ||
1013 (send_interface && group)) ||
1015 ((send_member && send_error) ||
1016 (send_member && receive_interface) ||
1017 (send_member && receive_member) ||
1018 (send_member && receive_error) ||
1019 (send_member && receive_sender) ||
1020 (send_member && eavesdrop) ||
1021 (send_member && receive_requested_reply) ||
1022 (send_member && own) ||
1023 (send_member && user) ||
1024 (send_member && group)) ||
1026 ((send_error && receive_interface) ||
1027 (send_error && receive_member) ||
1028 (send_error && receive_error) ||
1029 (send_error && receive_sender) ||
1030 (send_error && eavesdrop) ||
1031 (send_error && receive_requested_reply) ||
1032 (send_error && own) ||
1033 (send_error && user) ||
1034 (send_error && group)) ||
1036 ((send_destination && receive_interface) ||
1037 (send_destination && receive_member) ||
1038 (send_destination && receive_error) ||
1039 (send_destination && receive_sender) ||
1040 (send_destination && eavesdrop) ||
1041 (send_destination && receive_requested_reply) ||
1042 (send_destination && own) ||
1043 (send_destination && user) ||
1044 (send_destination && group)) ||
1046 ((send_type && receive_interface) ||
1047 (send_type && receive_member) ||
1048 (send_type && receive_error) ||
1049 (send_type && receive_sender) ||
1050 (send_type && eavesdrop) ||
1051 (send_type && receive_requested_reply) ||
1052 (send_type && own) ||
1053 (send_type && user) ||
1054 (send_type && group)) ||
1056 ((send_path && receive_interface) ||
1057 (send_path && receive_member) ||
1058 (send_path && receive_error) ||
1059 (send_path && receive_sender) ||
1060 (send_path && eavesdrop) ||
1061 (send_path && receive_requested_reply) ||
1062 (send_path && own) ||
1063 (send_path && user) ||
1064 (send_path && group)) ||
1066 ((send_requested_reply && receive_interface) ||
1067 (send_requested_reply && receive_member) ||
1068 (send_requested_reply && receive_error) ||
1069 (send_requested_reply && receive_sender) ||
1070 (send_requested_reply && eavesdrop) ||
1071 (send_requested_reply && receive_requested_reply) ||
1072 (send_requested_reply && own) ||
1073 (send_requested_reply && user) ||
1074 (send_requested_reply && group)) ||
1076 ((receive_interface && receive_error) ||
1077 (receive_interface && own) ||
1078 (receive_interface && user) ||
1079 (receive_interface && group)) ||
1081 ((receive_member && receive_error) ||
1082 (receive_member && own) ||
1083 (receive_member && user) ||
1084 (receive_member && group)) ||
1086 ((receive_error && own) ||
1087 (receive_error && user) ||
1088 (receive_error && group)) ||
1090 ((eavesdrop && own) ||
1091 (eavesdrop && user) ||
1092 (eavesdrop && group)) ||
1094 ((receive_requested_reply && own) ||
1095 (receive_requested_reply && user) ||
1096 (receive_requested_reply && group)) ||
1103 dbus_set_error (error, DBUS_ERROR_FAILED,
1104 "Invalid combination of attributes on element <%s>",
1111 /* In BusPolicyRule, NULL represents wildcard.
1112 * In the config file, '*' represents it.
1114 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1116 if (send_interface || send_member || send_error || send_destination ||
1117 send_path || send_type || send_requested_reply)
1121 if (IS_WILDCARD (send_interface))
1122 send_interface = NULL;
1123 if (IS_WILDCARD (send_member))
1125 if (IS_WILDCARD (send_error))
1127 if (IS_WILDCARD (send_destination))
1128 send_destination = NULL;
1129 if (IS_WILDCARD (send_path))
1131 if (IS_WILDCARD (send_type))
1134 message_type = DBUS_MESSAGE_TYPE_INVALID;
1135 if (send_type != NULL)
1137 message_type = dbus_message_type_from_string (send_type);
1138 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1140 dbus_set_error (error, DBUS_ERROR_FAILED,
1141 "Bad message type \"%s\"",
1147 if (send_requested_reply &&
1148 !(strcmp (send_requested_reply, "true") == 0 ||
1149 strcmp (send_requested_reply, "false") == 0))
1151 dbus_set_error (error, DBUS_ERROR_FAILED,
1152 "Bad value \"%s\" for %s attribute, must be true or false",
1153 "send_requested_reply", send_requested_reply);
1157 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1161 if (send_requested_reply)
1162 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1164 rule->d.send.message_type = message_type;
1165 rule->d.send.path = _dbus_strdup (send_path);
1166 rule->d.send.interface = _dbus_strdup (send_interface);
1167 rule->d.send.member = _dbus_strdup (send_member);
1168 rule->d.send.error = _dbus_strdup (send_error);
1169 rule->d.send.destination = _dbus_strdup (send_destination);
1170 if (send_path && rule->d.send.path == NULL)
1172 if (send_interface && rule->d.send.interface == NULL)
1174 if (send_member && rule->d.send.member == NULL)
1176 if (send_error && rule->d.send.error == NULL)
1178 if (send_destination && rule->d.send.destination == NULL)
1181 else if (receive_interface || receive_member || receive_error || receive_sender ||
1182 receive_path || receive_type || eavesdrop || receive_requested_reply)
1186 if (IS_WILDCARD (receive_interface))
1187 receive_interface = NULL;
1188 if (IS_WILDCARD (receive_member))
1189 receive_member = NULL;
1190 if (IS_WILDCARD (receive_error))
1191 receive_error = NULL;
1192 if (IS_WILDCARD (receive_sender))
1193 receive_sender = NULL;
1194 if (IS_WILDCARD (receive_path))
1195 receive_path = NULL;
1196 if (IS_WILDCARD (receive_type))
1197 receive_type = NULL;
1199 message_type = DBUS_MESSAGE_TYPE_INVALID;
1200 if (receive_type != NULL)
1202 message_type = dbus_message_type_from_string (receive_type);
1203 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1205 dbus_set_error (error, DBUS_ERROR_FAILED,
1206 "Bad message type \"%s\"",
1214 !(strcmp (eavesdrop, "true") == 0 ||
1215 strcmp (eavesdrop, "false") == 0))
1217 dbus_set_error (error, DBUS_ERROR_FAILED,
1218 "Bad value \"%s\" for %s attribute, must be true or false",
1219 "eavesdrop", eavesdrop);
1223 if (receive_requested_reply &&
1224 !(strcmp (receive_requested_reply, "true") == 0 ||
1225 strcmp (receive_requested_reply, "false") == 0))
1227 dbus_set_error (error, DBUS_ERROR_FAILED,
1228 "Bad value \"%s\" for %s attribute, must be true or false",
1229 "receive_requested_reply", receive_requested_reply);
1233 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1238 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1240 if (receive_requested_reply)
1241 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1243 rule->d.receive.message_type = message_type;
1244 rule->d.receive.path = _dbus_strdup (receive_path);
1245 rule->d.receive.interface = _dbus_strdup (receive_interface);
1246 rule->d.receive.member = _dbus_strdup (receive_member);
1247 rule->d.receive.error = _dbus_strdup (receive_error);
1248 rule->d.receive.origin = _dbus_strdup (receive_sender);
1250 if (receive_path && rule->d.receive.path == NULL)
1252 if (receive_interface && rule->d.receive.interface == NULL)
1254 if (receive_member && rule->d.receive.member == NULL)
1256 if (receive_error && rule->d.receive.error == NULL)
1258 if (receive_sender && rule->d.receive.origin == NULL)
1263 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1267 if (IS_WILDCARD (own))
1270 rule->d.own.service_name = _dbus_strdup (own);
1271 if (own && rule->d.own.service_name == NULL)
1276 if (IS_WILDCARD (user))
1278 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1282 rule->d.user.uid = DBUS_UID_UNSET;
1286 DBusString username;
1289 _dbus_string_init_const (&username, user);
1291 if (_dbus_get_user_id (&username, &uid))
1293 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1297 rule->d.user.uid = uid;
1301 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1302 user, element_name);
1308 if (IS_WILDCARD (group))
1310 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1314 rule->d.group.gid = DBUS_GID_UNSET;
1318 DBusString groupname;
1321 _dbus_string_init_const (&groupname, group);
1323 if (_dbus_get_user_id (&groupname, &gid))
1325 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1329 rule->d.group.gid = gid;
1333 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1334 group, element_name);
1339 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1345 pe = peek_element (parser);
1346 _dbus_assert (pe != NULL);
1347 _dbus_assert (pe->type == ELEMENT_POLICY);
1349 switch (pe->d.policy.type)
1351 case POLICY_IGNORED:
1352 /* drop the rule on the floor */
1355 case POLICY_DEFAULT:
1356 if (!bus_policy_append_default_rule (parser->policy, rule))
1359 case POLICY_MANDATORY:
1360 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1364 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1366 dbus_set_error (error, DBUS_ERROR_FAILED,
1367 "<%s> rule cannot be per-user because it has bus-global semantics",
1372 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
1377 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1379 dbus_set_error (error, DBUS_ERROR_FAILED,
1380 "<%s> rule cannot be per-group because it has bus-global semantics",
1385 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
1391 bus_policy_rule_unref (rule);
1398 BUS_SET_OOM (error);
1401 bus_policy_rule_unref (rule);
1406 start_policy_child (BusConfigParser *parser,
1407 const char *element_name,
1408 const char **attribute_names,
1409 const char **attribute_values,
1412 if (strcmp (element_name, "allow") == 0)
1414 if (!append_rule_from_element (parser, element_name,
1415 attribute_names, attribute_values,
1419 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1421 BUS_SET_OOM (error);
1427 else if (strcmp (element_name, "deny") == 0)
1429 if (!append_rule_from_element (parser, element_name,
1430 attribute_names, attribute_values,
1434 if (push_element (parser, ELEMENT_DENY) == NULL)
1436 BUS_SET_OOM (error);
1444 dbus_set_error (error, DBUS_ERROR_FAILED,
1445 "Element <%s> not allowed inside <%s> in configuration file",
1446 element_name, "policy");
1452 start_selinux_child (BusConfigParser *parser,
1453 const char *element_name,
1454 const char **attribute_names,
1455 const char **attribute_values,
1458 if (strcmp (element_name, "associate") == 0)
1461 const char *context;
1463 if (!locate_attributes (parser, "associate",
1468 "context", &context,
1472 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1474 BUS_SET_OOM (error);
1478 if (own == NULL || context == NULL)
1480 dbus_set_error (error, DBUS_ERROR_FAILED,
1481 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1485 if (!bus_selinux_id_table_insert (parser->service_sid_table,
1488 BUS_SET_OOM (error);
1496 dbus_set_error (error, DBUS_ERROR_FAILED,
1497 "Element <%s> not allowed inside <%s> in configuration file",
1498 element_name, "selinux");
1504 bus_config_parser_start_element (BusConfigParser *parser,
1505 const char *element_name,
1506 const char **attribute_names,
1507 const char **attribute_values,
1512 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1514 /* printf ("START: %s\n", element_name); */
1516 t = top_element_type (parser);
1518 if (t == ELEMENT_NONE)
1520 if (strcmp (element_name, "busconfig") == 0)
1522 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1525 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1527 BUS_SET_OOM (error);
1535 dbus_set_error (error, DBUS_ERROR_FAILED,
1536 "Unknown element <%s> at root of configuration file",
1541 else if (t == ELEMENT_BUSCONFIG)
1543 return start_busconfig_child (parser, element_name,
1544 attribute_names, attribute_values,
1547 else if (t == ELEMENT_POLICY)
1549 return start_policy_child (parser, element_name,
1550 attribute_names, attribute_values,
1553 else if (t == ELEMENT_SELINUX)
1555 return start_selinux_child (parser, element_name,
1556 attribute_names, attribute_values,
1561 dbus_set_error (error, DBUS_ERROR_FAILED,
1562 "Element <%s> is not allowed in this context",
1569 set_limit (BusConfigParser *parser,
1574 dbus_bool_t must_be_positive;
1575 dbus_bool_t must_be_int;
1577 must_be_int = FALSE;
1578 must_be_positive = FALSE;
1580 if (strcmp (name, "max_incoming_bytes") == 0)
1582 must_be_positive = TRUE;
1583 parser->limits.max_incoming_bytes = value;
1585 else if (strcmp (name, "max_outgoing_bytes") == 0)
1587 must_be_positive = TRUE;
1588 parser->limits.max_outgoing_bytes = value;
1590 else if (strcmp (name, "max_message_size") == 0)
1592 must_be_positive = TRUE;
1593 parser->limits.max_message_size = value;
1595 else if (strcmp (name, "activation_timeout") == 0)
1597 must_be_positive = TRUE;
1599 parser->limits.activation_timeout = value;
1601 else if (strcmp (name, "auth_timeout") == 0)
1603 must_be_positive = TRUE;
1605 parser->limits.auth_timeout = value;
1607 else if (strcmp (name, "reply_timeout") == 0)
1609 must_be_positive = TRUE;
1611 parser->limits.reply_timeout = value;
1613 else if (strcmp (name, "max_completed_connections") == 0)
1615 must_be_positive = TRUE;
1617 parser->limits.max_completed_connections = value;
1619 else if (strcmp (name, "max_incomplete_connections") == 0)
1621 must_be_positive = TRUE;
1623 parser->limits.max_incomplete_connections = value;
1625 else if (strcmp (name, "max_connections_per_user") == 0)
1627 must_be_positive = TRUE;
1629 parser->limits.max_connections_per_user = value;
1631 else if (strcmp (name, "max_pending_activations") == 0)
1633 must_be_positive = TRUE;
1635 parser->limits.max_pending_activations = value;
1637 else if (strcmp (name, "max_services_per_connection") == 0)
1639 must_be_positive = TRUE;
1641 parser->limits.max_services_per_connection = value;
1643 else if (strcmp (name, "max_replies_per_connection") == 0)
1645 must_be_positive = TRUE;
1647 parser->limits.max_replies_per_connection = value;
1651 dbus_set_error (error, DBUS_ERROR_FAILED,
1652 "There is no limit called \"%s\"\n",
1657 if (must_be_positive && value < 0)
1659 dbus_set_error (error, DBUS_ERROR_FAILED,
1660 "<limit name=\"%s\"> must be a positive number\n",
1666 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1668 dbus_set_error (error, DBUS_ERROR_FAILED,
1669 "<limit name=\"%s\"> value is too large\n",
1678 bus_config_parser_end_element (BusConfigParser *parser,
1679 const char *element_name,
1686 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1688 /* printf ("END: %s\n", element_name); */
1690 t = top_element_type (parser);
1692 if (t == ELEMENT_NONE)
1694 /* should probably be an assertion failure but
1695 * being paranoid about XML parsers
1697 dbus_set_error (error, DBUS_ERROR_FAILED,
1698 "XML parser ended element with no element on the stack");
1702 n = element_type_to_name (t);
1703 _dbus_assert (n != NULL);
1704 if (strcmp (n, element_name) != 0)
1706 /* should probably be an assertion failure but
1707 * being paranoid about XML parsers
1709 dbus_set_error (error, DBUS_ERROR_FAILED,
1710 "XML element <%s> ended but topmost element on the stack was <%s>",
1715 e = peek_element (parser);
1716 _dbus_assert (e != NULL);
1721 _dbus_assert_not_reached ("element in stack has no type");
1724 case ELEMENT_INCLUDE:
1727 case ELEMENT_LISTEN:
1728 case ELEMENT_PIDFILE:
1730 case ELEMENT_SERVICEDIR:
1731 case ELEMENT_INCLUDEDIR:
1733 if (!e->had_content)
1735 dbus_set_error (error, DBUS_ERROR_FAILED,
1736 "XML element <%s> was expected to have content inside it",
1737 element_type_to_name (e->type));
1741 if (e->type == ELEMENT_LIMIT)
1743 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1749 case ELEMENT_BUSCONFIG:
1750 case ELEMENT_POLICY:
1754 case ELEMENT_SELINUX:
1755 case ELEMENT_ASSOCIATE:
1759 pop_element (parser);
1765 all_whitespace (const DBusString *str)
1769 _dbus_string_skip_white (str, 0, &i);
1771 return i == _dbus_string_get_length (str);
1775 make_full_path (const DBusString *basedir,
1776 const DBusString *filename,
1777 DBusString *full_path)
1779 if (_dbus_path_is_absolute (filename))
1781 return _dbus_string_copy (filename, 0, full_path, 0);
1785 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1788 if (!_dbus_concat_dir_and_file (full_path, filename))
1796 include_file (BusConfigParser *parser,
1797 const DBusString *filename,
1798 dbus_bool_t ignore_missing,
1801 /* FIXME good test case for this would load each config file in the
1802 * test suite both alone, and as an include, and check
1803 * that the result is the same
1805 BusConfigParser *included;
1806 const char *filename_str;
1807 DBusError tmp_error;
1809 dbus_error_init (&tmp_error);
1811 filename_str = _dbus_string_get_const_data (filename);
1813 /* Check to make sure this file hasn't already been included. */
1814 if (seen_include (parser, filename))
1816 dbus_set_error (error, DBUS_ERROR_FAILED,
1817 "Circular inclusion of file '%s'",
1822 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
1824 BUS_SET_OOM (error);
1828 /* Since parser is passed in as the parent, included
1829 inherits parser's limits. */
1830 included = bus_config_load (filename, FALSE, parser, &tmp_error);
1832 _dbus_list_pop_last (&parser->included_files);
1834 if (included == NULL)
1836 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1838 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1841 dbus_error_free (&tmp_error);
1846 dbus_move_error (&tmp_error, error);
1852 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1854 if (!merge_included (parser, included, error))
1856 bus_config_parser_unref (included);
1860 /* Copy included's limits back to parser. */
1861 parser->limits = included->limits;
1863 bus_config_parser_unref (included);
1869 include_dir (BusConfigParser *parser,
1870 const DBusString *dirname,
1873 DBusString filename;
1875 DBusError tmp_error;
1878 if (!_dbus_string_init (&filename))
1880 BUS_SET_OOM (error);
1886 dir = _dbus_directory_open (dirname, error);
1891 dbus_error_init (&tmp_error);
1892 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1894 DBusString full_path;
1896 if (!_dbus_string_init (&full_path))
1898 BUS_SET_OOM (error);
1902 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1904 BUS_SET_OOM (error);
1905 _dbus_string_free (&full_path);
1909 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1911 BUS_SET_OOM (error);
1912 _dbus_string_free (&full_path);
1916 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1918 if (!include_file (parser, &full_path, TRUE, error))
1920 _dbus_string_free (&full_path);
1925 _dbus_string_free (&full_path);
1928 if (dbus_error_is_set (&tmp_error))
1930 dbus_move_error (&tmp_error, error);
1937 _dbus_string_free (&filename);
1940 _dbus_directory_close (dir);
1946 bus_config_parser_content (BusConfigParser *parser,
1947 const DBusString *content,
1952 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1958 _dbus_string_get_const_data (content, &c_str);
1960 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1964 e = peek_element (parser);
1967 dbus_set_error (error, DBUS_ERROR_FAILED,
1968 "Text content outside of any XML element in configuration file");
1971 else if (e->had_content)
1973 _dbus_assert_not_reached ("Element had multiple content blocks");
1977 switch (top_element_type (parser))
1980 _dbus_assert_not_reached ("element at top of stack has no type");
1983 case ELEMENT_BUSCONFIG:
1984 case ELEMENT_POLICY:
1988 case ELEMENT_SELINUX:
1989 case ELEMENT_ASSOCIATE:
1990 if (all_whitespace (content))
1994 dbus_set_error (error, DBUS_ERROR_FAILED,
1995 "No text content expected inside XML element %s in configuration file",
1996 element_type_to_name (top_element_type (parser)));
2000 case ELEMENT_PIDFILE:
2004 e->had_content = TRUE;
2006 if (!_dbus_string_copy_data (content, &s))
2009 dbus_free (parser->pidfile);
2010 parser->pidfile = s;
2014 case ELEMENT_INCLUDE:
2016 DBusString full_path, selinux_policy_root;
2018 e->had_content = TRUE;
2020 if (!_dbus_string_init (&full_path))
2023 if (e->d.include.selinux_root_relative)
2025 if (!bus_selinux_get_policy_root ())
2027 dbus_set_error (error, DBUS_ERROR_FAILED,
2028 "Could not determine SELinux policy root for relative inclusion");
2029 _dbus_string_free (&full_path);
2032 _dbus_string_init_const (&selinux_policy_root,
2033 bus_selinux_get_policy_root ());
2034 if (!make_full_path (&selinux_policy_root, content, &full_path))
2036 _dbus_string_free (&full_path);
2040 else if (!make_full_path (&parser->basedir, content, &full_path))
2042 _dbus_string_free (&full_path);
2046 if (!include_file (parser, &full_path,
2047 e->d.include.ignore_missing, error))
2049 _dbus_string_free (&full_path);
2053 _dbus_string_free (&full_path);
2057 case ELEMENT_INCLUDEDIR:
2059 DBusString full_path;
2061 e->had_content = TRUE;
2063 if (!_dbus_string_init (&full_path))
2066 if (!make_full_path (&parser->basedir, content, &full_path))
2068 _dbus_string_free (&full_path);
2072 if (!include_dir (parser, &full_path, error))
2074 _dbus_string_free (&full_path);
2078 _dbus_string_free (&full_path);
2086 e->had_content = TRUE;
2088 if (!_dbus_string_copy_data (content, &s))
2091 dbus_free (parser->user);
2100 e->had_content = TRUE;
2102 if (!_dbus_string_copy_data (content, &s))
2105 dbus_free (parser->bus_type);
2106 parser->bus_type = s;
2110 case ELEMENT_LISTEN:
2114 e->had_content = TRUE;
2116 if (!_dbus_string_copy_data (content, &s))
2119 if (!_dbus_list_append (&parser->listen_on,
2132 e->had_content = TRUE;
2134 if (!_dbus_string_copy_data (content, &s))
2137 if (!_dbus_list_append (&parser->mechanisms,
2146 case ELEMENT_SERVICEDIR:
2149 DBusString full_path;
2151 e->had_content = TRUE;
2153 if (!_dbus_string_init (&full_path))
2156 if (!make_full_path (&parser->basedir, content, &full_path))
2158 _dbus_string_free (&full_path);
2162 if (!_dbus_string_copy_data (&full_path, &s))
2164 _dbus_string_free (&full_path);
2168 if (!_dbus_list_append (&parser->service_dirs, s))
2170 _dbus_string_free (&full_path);
2175 _dbus_string_free (&full_path);
2183 e->had_content = TRUE;
2186 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2188 dbus_set_error (error, DBUS_ERROR_FAILED,
2189 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2194 e->d.limit.value = val;
2196 _dbus_verbose ("Loaded value %ld for limit %s\n",
2203 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2207 BUS_SET_OOM (error);
2212 bus_config_parser_finished (BusConfigParser *parser,
2215 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2217 if (parser->stack != NULL)
2219 dbus_set_error (error, DBUS_ERROR_FAILED,
2220 "Element <%s> was not closed in configuration file",
2221 element_type_to_name (top_element_type (parser)));
2226 if (parser->is_toplevel && parser->listen_on == NULL)
2228 dbus_set_error (error, DBUS_ERROR_FAILED,
2229 "Configuration file needs one or more <listen> elements giving addresses");
2237 bus_config_parser_get_user (BusConfigParser *parser)
2239 return parser->user;
2243 bus_config_parser_get_type (BusConfigParser *parser)
2245 return parser->bus_type;
2249 bus_config_parser_get_addresses (BusConfigParser *parser)
2251 return &parser->listen_on;
2255 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2257 return &parser->mechanisms;
2261 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2263 return &parser->service_dirs;
2267 bus_config_parser_get_fork (BusConfigParser *parser)
2269 return parser->fork;
2273 bus_config_parser_get_pidfile (BusConfigParser *parser)
2275 return parser->pidfile;
2279 bus_config_parser_steal_policy (BusConfigParser *parser)
2283 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2285 policy = parser->policy;
2287 parser->policy = NULL;
2292 /* Overwrite any limits that were set in the configuration file */
2294 bus_config_parser_get_limits (BusConfigParser *parser,
2297 *limits = parser->limits;
2301 bus_config_parser_steal_service_sid_table (BusConfigParser *parser)
2303 DBusHashTable *table;
2305 _dbus_assert (parser->service_sid_table != NULL); /* can only steal once */
2307 table = parser->service_sid_table;
2309 parser->service_sid_table = NULL;
2314 #ifdef DBUS_BUILD_TESTS
2325 do_load (const DBusString *full_path,
2327 dbus_bool_t oom_possible)
2329 BusConfigParser *parser;
2332 dbus_error_init (&error);
2334 parser = bus_config_load (full_path, TRUE, NULL, &error);
2337 _DBUS_ASSERT_ERROR_IS_SET (&error);
2340 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2342 _dbus_verbose ("Failed to load valid file due to OOM\n");
2343 dbus_error_free (&error);
2346 else if (validity == VALID)
2348 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2351 dbus_error_free (&error);
2356 dbus_error_free (&error);
2362 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2364 bus_config_parser_unref (parser);
2366 if (validity == INVALID)
2368 _dbus_warn ("Accepted invalid file\n");
2378 const DBusString *full_path;
2383 check_loader_oom_func (void *data)
2385 LoaderOomData *d = data;
2387 return do_load (d->full_path, d->validity, TRUE);
2391 process_test_valid_subdir (const DBusString *test_base_dir,
2395 DBusString test_directory;
2396 DBusString filename;
2404 if (!_dbus_string_init (&test_directory))
2405 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2407 _dbus_string_init_const (&filename, subdir);
2409 if (!_dbus_string_copy (test_base_dir, 0,
2410 &test_directory, 0))
2411 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2413 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2414 _dbus_assert_not_reached ("couldn't allocate full path");
2416 _dbus_string_free (&filename);
2417 if (!_dbus_string_init (&filename))
2418 _dbus_assert_not_reached ("didn't allocate filename string\n");
2420 dbus_error_init (&error);
2421 dir = _dbus_directory_open (&test_directory, &error);
2424 _dbus_warn ("Could not open %s: %s\n",
2425 _dbus_string_get_const_data (&test_directory),
2427 dbus_error_free (&error);
2431 if (validity == VALID)
2432 printf ("Testing valid files:\n");
2433 else if (validity == INVALID)
2434 printf ("Testing invalid files:\n");
2436 printf ("Testing unknown files:\n");
2439 while (_dbus_directory_get_next_file (dir, &filename, &error))
2441 DBusString full_path;
2444 if (!_dbus_string_init (&full_path))
2445 _dbus_assert_not_reached ("couldn't init string");
2447 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2448 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2450 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2451 _dbus_assert_not_reached ("couldn't concat file to dir");
2453 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2455 _dbus_verbose ("Skipping non-.conf file %s\n",
2456 _dbus_string_get_const_data (&filename));
2457 _dbus_string_free (&full_path);
2461 printf (" %s\n", _dbus_string_get_const_data (&filename));
2463 _dbus_verbose (" expecting %s\n",
2464 validity == VALID ? "valid" :
2465 (validity == INVALID ? "invalid" :
2466 (validity == UNKNOWN ? "unknown" : "???")));
2468 d.full_path = &full_path;
2469 d.validity = validity;
2471 /* FIXME hackaround for an expat problem, see
2472 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2473 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2475 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2476 if (!check_loader_oom_func (&d))
2477 _dbus_assert_not_reached ("test failed");
2479 _dbus_string_free (&full_path);
2482 if (dbus_error_is_set (&error))
2484 _dbus_warn ("Could not get next file in %s: %s\n",
2485 _dbus_string_get_const_data (&test_directory),
2487 dbus_error_free (&error);
2496 _dbus_directory_close (dir);
2497 _dbus_string_free (&test_directory);
2498 _dbus_string_free (&filename);
2504 bools_equal (dbus_bool_t a,
2511 strings_equal_or_both_null (const char *a,
2514 if (a == NULL || b == NULL)
2517 return !strcmp (a, b);
2521 elements_equal (const Element *a,
2524 if (a->type != b->type)
2527 if (!bools_equal (a->had_content, b->had_content))
2533 case ELEMENT_INCLUDE:
2534 if (!bools_equal (a->d.include.ignore_missing,
2535 b->d.include.ignore_missing))
2539 case ELEMENT_POLICY:
2540 if (a->d.policy.type != b->d.policy.type)
2542 if (a->d.policy.gid_or_uid != b->d.policy.gid_or_uid)
2547 if (strcmp (a->d.limit.name, b->d.limit.name))
2549 if (a->d.limit.value != b->d.limit.value)
2563 lists_of_elements_equal (DBusList *a,
2572 while (ia != NULL && ib != NULL)
2574 if (elements_equal (ia->data, ib->data))
2576 ia = _dbus_list_get_next_link (&a, ia);
2577 ib = _dbus_list_get_next_link (&b, ib);
2580 return ia == NULL && ib == NULL;
2584 lists_of_c_strings_equal (DBusList *a,
2593 while (ia != NULL && ib != NULL)
2595 if (strcmp (ia->data, ib->data))
2597 ia = _dbus_list_get_next_link (&a, ia);
2598 ib = _dbus_list_get_next_link (&b, ib);
2601 return ia == NULL && ib == NULL;
2605 limits_equal (const BusLimits *a,
2609 (a->max_incoming_bytes == b->max_incoming_bytes
2610 || a->max_outgoing_bytes == b->max_outgoing_bytes
2611 || a->max_message_size == b->max_message_size
2612 || a->activation_timeout == b->activation_timeout
2613 || a->auth_timeout == b->auth_timeout
2614 || a->max_completed_connections == b->max_completed_connections
2615 || a->max_incomplete_connections == b->max_incomplete_connections
2616 || a->max_connections_per_user == b->max_connections_per_user
2617 || a->max_pending_activations == b->max_pending_activations
2618 || a->max_services_per_connection == b->max_services_per_connection
2619 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2620 || a->max_replies_per_connection == b->max_replies_per_connection
2621 || a->reply_timeout == b->reply_timeout);
2625 config_parsers_equal (const BusConfigParser *a,
2626 const BusConfigParser *b)
2628 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2631 if (!lists_of_elements_equal (a->stack, b->stack))
2634 if (!strings_equal_or_both_null (a->user, b->user))
2637 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2640 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2643 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2646 /* FIXME: compare policy */
2648 /* FIXME: compare service selinux ID table */
2650 if (! limits_equal (&a->limits, &b->limits))
2653 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2656 if (! bools_equal (a->fork, b->fork))
2659 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2666 all_are_equiv (const DBusString *target_directory)
2668 DBusString filename;
2670 BusConfigParser *first_parser;
2671 BusConfigParser *parser;
2677 first_parser = NULL;
2681 if (!_dbus_string_init (&filename))
2682 _dbus_assert_not_reached ("didn't allocate filename string");
2684 dbus_error_init (&error);
2685 dir = _dbus_directory_open (target_directory, &error);
2688 _dbus_warn ("Could not open %s: %s\n",
2689 _dbus_string_get_const_data (target_directory),
2691 dbus_error_free (&error);
2695 printf ("Comparing equivalent files:\n");
2698 while (_dbus_directory_get_next_file (dir, &filename, &error))
2700 DBusString full_path;
2702 if (!_dbus_string_init (&full_path))
2703 _dbus_assert_not_reached ("couldn't init string");
2705 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2706 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2708 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2709 _dbus_assert_not_reached ("couldn't concat file to dir");
2711 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2713 _dbus_verbose ("Skipping non-.conf file %s\n",
2714 _dbus_string_get_const_data (&filename));
2715 _dbus_string_free (&full_path);
2719 printf (" %s\n", _dbus_string_get_const_data (&filename));
2721 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2722 _dbus_string_free (&full_path);
2726 _dbus_warn ("Could not load file %s: %s\n",
2727 _dbus_string_get_const_data (&full_path),
2729 dbus_error_free (&error);
2732 else if (first_parser == NULL)
2734 first_parser = parser;
2738 equal = config_parsers_equal (first_parser, parser);
2739 bus_config_parser_unref (parser);
2749 _dbus_string_free (&filename);
2751 bus_config_parser_unref (first_parser);
2753 _dbus_directory_close (dir);
2760 process_test_equiv_subdir (const DBusString *test_base_dir,
2763 DBusString test_directory;
2764 DBusString filename;
2773 if (!_dbus_string_init (&test_directory))
2774 _dbus_assert_not_reached ("didn't allocate test_directory");
2776 _dbus_string_init_const (&filename, subdir);
2778 if (!_dbus_string_copy (test_base_dir, 0,
2779 &test_directory, 0))
2780 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2782 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2783 _dbus_assert_not_reached ("couldn't allocate full path");
2785 _dbus_string_free (&filename);
2786 if (!_dbus_string_init (&filename))
2787 _dbus_assert_not_reached ("didn't allocate filename string");
2789 dbus_error_init (&error);
2790 dir = _dbus_directory_open (&test_directory, &error);
2793 _dbus_warn ("Could not open %s: %s\n",
2794 _dbus_string_get_const_data (&test_directory),
2796 dbus_error_free (&error);
2800 while (_dbus_directory_get_next_file (dir, &filename, &error))
2802 DBusString full_path;
2804 /* Skip CVS's magic directories! */
2805 if (_dbus_string_equal_c_str (&filename, "CVS"))
2808 if (!_dbus_string_init (&full_path))
2809 _dbus_assert_not_reached ("couldn't init string");
2811 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2812 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2814 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2815 _dbus_assert_not_reached ("couldn't concat file to dir");
2817 equal = all_are_equiv (&full_path);
2818 _dbus_string_free (&full_path);
2827 _dbus_string_free (&test_directory);
2828 _dbus_string_free (&filename);
2830 _dbus_directory_close (dir);
2837 bus_config_parser_test (const DBusString *test_data_dir)
2839 if (test_data_dir == NULL ||
2840 _dbus_string_get_length (test_data_dir) == 0)
2842 printf ("No test data\n");
2846 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
2849 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
2852 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
2858 #endif /* DBUS_BUILD_TESTS */