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;
81 unsigned long gid_or_uid;
95 * Parser for bus configuration file.
97 struct BusConfigParser
99 int refcount; /**< Reference count */
101 DBusString basedir; /**< Directory we resolve paths relative to */
103 DBusList *stack; /**< stack of Element */
105 char *user; /**< user to run as */
107 char *bus_type; /**< Message bus type */
109 DBusList *listen_on; /**< List of addresses to listen to */
111 DBusList *mechanisms; /**< Auth mechanisms */
113 DBusList *service_dirs; /**< Directories to look for services in */
115 BusPolicy *policy; /**< Security policy */
117 BusLimits limits; /**< Limits */
119 char *pidfile; /**< PID file */
121 DBusList *included_files; /**< Included files stack */
123 DBusHashTable *service_sid_table; /**< Map service names to SELinux contexts */
125 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
127 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
131 element_type_to_name (ElementType type)
137 case ELEMENT_BUSCONFIG:
139 case ELEMENT_INCLUDE:
157 case ELEMENT_PIDFILE:
159 case ELEMENT_SERVICEDIR:
161 case ELEMENT_INCLUDEDIR:
165 case ELEMENT_SELINUX:
167 case ELEMENT_ASSOCIATE:
171 _dbus_assert_not_reached ("bad element type");
177 push_element (BusConfigParser *parser,
182 _dbus_assert (type != ELEMENT_NONE);
184 e = dbus_new0 (Element, 1);
188 if (!_dbus_list_append (&parser->stack, e))
200 element_free (Element *e)
202 if (e->type == ELEMENT_LIMIT)
203 dbus_free (e->d.limit.name);
209 pop_element (BusConfigParser *parser)
213 e = _dbus_list_pop_last (&parser->stack);
219 peek_element (BusConfigParser *parser)
223 e = _dbus_list_get_last (&parser->stack);
229 top_element_type (BusConfigParser *parser)
233 e = _dbus_list_get_last (&parser->stack);
242 merge_included (BusConfigParser *parser,
243 BusConfigParser *included,
247 DBusHashTable *table;
249 if (!bus_policy_merge (parser->policy,
256 table = bus_selinux_id_table_union (parser->service_sid_table,
257 included->service_sid_table);
264 _dbus_hash_table_unref (parser->service_sid_table);
265 parser->service_sid_table = table;
267 if (included->user != NULL)
269 dbus_free (parser->user);
270 parser->user = included->user;
271 included->user = NULL;
274 if (included->bus_type != NULL)
276 dbus_free (parser->bus_type);
277 parser->bus_type = included->bus_type;
278 included->bus_type = NULL;
284 if (included->pidfile != NULL)
286 dbus_free (parser->pidfile);
287 parser->pidfile = included->pidfile;
288 included->pidfile = NULL;
291 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
292 _dbus_list_append_link (&parser->listen_on, link);
294 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
295 _dbus_list_append_link (&parser->mechanisms, link);
297 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
298 _dbus_list_append_link (&parser->service_dirs, link);
304 seen_include (BusConfigParser *parser,
305 const DBusString *file)
309 iter = parser->included_files;
312 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
315 iter = _dbus_list_get_next_link (&parser->included_files, iter);
322 bus_config_parser_new (const DBusString *basedir,
323 dbus_bool_t is_toplevel,
324 const BusConfigParser *parent)
326 BusConfigParser *parser;
328 parser = dbus_new0 (BusConfigParser, 1);
332 parser->is_toplevel = !!is_toplevel;
334 if (!_dbus_string_init (&parser->basedir))
340 if (((parser->policy = bus_policy_new ()) == NULL) ||
341 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
342 ((parser->service_sid_table = bus_selinux_id_table_new ()) == NULL))
345 bus_policy_unref (parser->policy);
347 _dbus_string_free (&parser->basedir);
355 /* Initialize the parser's limits from the parent. */
356 parser->limits = parent->limits;
358 /* Use the parent's list of included_files to avoid
359 circular inclusions. */
360 parser->included_files = parent->included_files;
365 /* Make up some numbers! woot! */
366 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
367 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
368 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
370 /* Making this long means the user has to wait longer for an error
371 * message if something screws up, but making it too short means
372 * they might see a false failure.
374 parser->limits.activation_timeout = 25000; /* 25 seconds */
376 /* Making this long risks making a DOS attack easier, but too short
377 * and legitimate auth will fail. If interactive auth (ask user for
378 * password) is allowed, then potentially it has to be quite long.
380 parser->limits.auth_timeout = 30000; /* 30 seconds */
382 parser->limits.max_incomplete_connections = 32;
383 parser->limits.max_connections_per_user = 128;
385 /* Note that max_completed_connections / max_connections_per_user
386 * is the number of users that would have to work together to
387 * DOS all the other users.
389 parser->limits.max_completed_connections = 1024;
391 parser->limits.max_pending_activations = 256;
392 parser->limits.max_services_per_connection = 256;
394 parser->limits.max_match_rules_per_connection = 128;
396 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
397 parser->limits.max_replies_per_connection = 32;
400 parser->refcount = 1;
406 bus_config_parser_ref (BusConfigParser *parser)
408 _dbus_assert (parser->refcount > 0);
410 parser->refcount += 1;
416 bus_config_parser_unref (BusConfigParser *parser)
418 _dbus_assert (parser->refcount > 0);
420 parser->refcount -= 1;
422 if (parser->refcount == 0)
424 while (parser->stack != NULL)
425 pop_element (parser);
427 dbus_free (parser->user);
428 dbus_free (parser->bus_type);
429 dbus_free (parser->pidfile);
431 _dbus_list_foreach (&parser->listen_on,
432 (DBusForeachFunction) dbus_free,
435 _dbus_list_clear (&parser->listen_on);
437 _dbus_list_foreach (&parser->service_dirs,
438 (DBusForeachFunction) dbus_free,
441 _dbus_list_clear (&parser->service_dirs);
443 _dbus_list_foreach (&parser->mechanisms,
444 (DBusForeachFunction) dbus_free,
447 _dbus_list_clear (&parser->mechanisms);
449 _dbus_string_free (&parser->basedir);
452 bus_policy_unref (parser->policy);
454 if (parser->service_sid_table)
455 _dbus_hash_table_unref (parser->service_sid_table);
462 bus_config_parser_check_doctype (BusConfigParser *parser,
466 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
468 if (strcmp (doctype, "busconfig") != 0)
470 dbus_set_error (error,
472 "Configuration file has the wrong document type %s",
487 locate_attributes (BusConfigParser *parser,
488 const char *element_name,
489 const char **attribute_names,
490 const char **attribute_values,
492 const char *first_attribute_name,
493 const char **first_attribute_retloc,
501 LocateAttr attrs[MAX_ATTRS];
505 _dbus_assert (first_attribute_name != NULL);
506 _dbus_assert (first_attribute_retloc != NULL);
511 attrs[0].name = first_attribute_name;
512 attrs[0].retloc = first_attribute_retloc;
513 *first_attribute_retloc = NULL;
515 va_start (args, first_attribute_retloc);
517 name = va_arg (args, const char*);
518 retloc = va_arg (args, const char**);
522 _dbus_assert (retloc != NULL);
523 _dbus_assert (n_attrs < MAX_ATTRS);
525 attrs[n_attrs].name = name;
526 attrs[n_attrs].retloc = retloc;
530 name = va_arg (args, const char*);
531 retloc = va_arg (args, const char**);
540 while (attribute_names[i])
549 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
551 retloc = attrs[j].retloc;
555 dbus_set_error (error, DBUS_ERROR_FAILED,
556 "Attribute \"%s\" repeated twice on the same <%s> element",
557 attrs[j].name, element_name);
562 *retloc = attribute_values[i];
571 dbus_set_error (error, DBUS_ERROR_FAILED,
572 "Attribute \"%s\" is invalid on <%s> element in this context",
573 attribute_names[i], element_name);
586 check_no_attributes (BusConfigParser *parser,
587 const char *element_name,
588 const char **attribute_names,
589 const char **attribute_values,
592 if (attribute_names[0] != NULL)
594 dbus_set_error (error, DBUS_ERROR_FAILED,
595 "Attribute \"%s\" is invalid on <%s> element in this context",
596 attribute_names[0], element_name);
604 start_busconfig_child (BusConfigParser *parser,
605 const char *element_name,
606 const char **attribute_names,
607 const char **attribute_values,
610 if (strcmp (element_name, "user") == 0)
612 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
615 if (push_element (parser, ELEMENT_USER) == NULL)
623 else if (strcmp (element_name, "type") == 0)
625 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
628 if (push_element (parser, ELEMENT_TYPE) == NULL)
636 else if (strcmp (element_name, "fork") == 0)
638 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
641 if (push_element (parser, ELEMENT_FORK) == NULL)
651 else if (strcmp (element_name, "pidfile") == 0)
653 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
656 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
664 else if (strcmp (element_name, "listen") == 0)
666 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
669 if (push_element (parser, ELEMENT_LISTEN) == NULL)
677 else if (strcmp (element_name, "auth") == 0)
679 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
682 if (push_element (parser, ELEMENT_AUTH) == NULL)
690 else if (strcmp (element_name, "includedir") == 0)
692 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
695 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
703 else if (strcmp (element_name, "servicedir") == 0)
705 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
708 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
716 else if (strcmp (element_name, "include") == 0)
719 const char *ignore_missing;
721 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
727 e->d.include.ignore_missing = FALSE;
729 if (!locate_attributes (parser, "include",
733 "ignore_missing", &ignore_missing,
737 if (ignore_missing != NULL)
739 if (strcmp (ignore_missing, "yes") == 0)
740 e->d.include.ignore_missing = TRUE;
741 else if (strcmp (ignore_missing, "no") == 0)
742 e->d.include.ignore_missing = FALSE;
745 dbus_set_error (error, DBUS_ERROR_FAILED,
746 "ignore_missing attribute must have value \"yes\" or \"no\"");
753 else if (strcmp (element_name, "policy") == 0)
760 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
766 e->d.policy.type = POLICY_IGNORED;
768 if (!locate_attributes (parser, "policy",
778 if (((context && user) ||
779 (context && group)) ||
781 !(context || user || group))
783 dbus_set_error (error, DBUS_ERROR_FAILED,
784 "<policy> element must have exactly one of (context|user|group) attributes");
790 if (strcmp (context, "default") == 0)
792 e->d.policy.type = POLICY_DEFAULT;
794 else if (strcmp (context, "mandatory") == 0)
796 e->d.policy.type = POLICY_MANDATORY;
800 dbus_set_error (error, DBUS_ERROR_FAILED,
801 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
806 else if (user != NULL)
809 _dbus_string_init_const (&username, user);
811 if (_dbus_get_user_id (&username,
812 &e->d.policy.gid_or_uid))
813 e->d.policy.type = POLICY_USER;
815 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
818 else if (group != NULL)
820 DBusString group_name;
821 _dbus_string_init_const (&group_name, group);
823 if (_dbus_get_group_id (&group_name,
824 &e->d.policy.gid_or_uid))
825 e->d.policy.type = POLICY_GROUP;
827 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
832 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
837 else if (strcmp (element_name, "limit") == 0)
842 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
848 if (!locate_attributes (parser, "limit",
858 dbus_set_error (error, DBUS_ERROR_FAILED,
859 "<limit> element must have a \"name\" attribute");
863 e->d.limit.name = _dbus_strdup (name);
864 if (e->d.limit.name == NULL)
872 else if (strcmp (element_name, "selinux") == 0)
877 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
880 if (push_element (parser, ELEMENT_SELINUX) == NULL)
890 dbus_set_error (error, DBUS_ERROR_FAILED,
891 "Element <%s> not allowed inside <%s> in configuration file",
892 element_name, "busconfig");
898 append_rule_from_element (BusConfigParser *parser,
899 const char *element_name,
900 const char **attribute_names,
901 const char **attribute_values,
905 const char *send_interface;
906 const char *send_member;
907 const char *send_error;
908 const char *send_destination;
909 const char *send_path;
910 const char *send_type;
911 const char *receive_interface;
912 const char *receive_member;
913 const char *receive_error;
914 const char *receive_sender;
915 const char *receive_path;
916 const char *receive_type;
917 const char *eavesdrop;
918 const char *send_requested_reply;
919 const char *receive_requested_reply;
925 if (!locate_attributes (parser, element_name,
929 "send_interface", &send_interface,
930 "send_member", &send_member,
931 "send_error", &send_error,
932 "send_destination", &send_destination,
933 "send_path", &send_path,
934 "send_type", &send_type,
935 "receive_interface", &receive_interface,
936 "receive_member", &receive_member,
937 "receive_error", &receive_error,
938 "receive_sender", &receive_sender,
939 "receive_path", &receive_path,
940 "receive_type", &receive_type,
941 "eavesdrop", &eavesdrop,
942 "send_requested_reply", &send_requested_reply,
943 "receive_requested_reply", &receive_requested_reply,
950 if (!(send_interface || send_member || send_error || send_destination ||
951 send_type || send_path ||
952 receive_interface || receive_member || receive_error || receive_sender ||
953 receive_type || receive_path || eavesdrop ||
954 send_requested_reply || receive_requested_reply ||
955 own || user || group))
957 dbus_set_error (error, DBUS_ERROR_FAILED,
958 "Element <%s> must have one or more attributes",
963 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
964 (receive_member && (receive_interface == NULL && receive_path == NULL)))
966 dbus_set_error (error, DBUS_ERROR_FAILED,
967 "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.",
972 /* Allowed combinations of elements are:
974 * base, must be all send or all receive:
980 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
981 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
983 * user, group, own must occur alone
985 * Pretty sure the below stuff is broken, FIXME think about it more.
988 if (((send_interface && send_error) ||
989 (send_interface && receive_interface) ||
990 (send_interface && receive_member) ||
991 (send_interface && receive_error) ||
992 (send_interface && receive_sender) ||
993 (send_interface && eavesdrop) ||
994 (send_interface && receive_requested_reply) ||
995 (send_interface && own) ||
996 (send_interface && user) ||
997 (send_interface && group)) ||
999 ((send_member && send_error) ||
1000 (send_member && receive_interface) ||
1001 (send_member && receive_member) ||
1002 (send_member && receive_error) ||
1003 (send_member && receive_sender) ||
1004 (send_member && eavesdrop) ||
1005 (send_member && receive_requested_reply) ||
1006 (send_member && own) ||
1007 (send_member && user) ||
1008 (send_member && group)) ||
1010 ((send_error && receive_interface) ||
1011 (send_error && receive_member) ||
1012 (send_error && receive_error) ||
1013 (send_error && receive_sender) ||
1014 (send_error && eavesdrop) ||
1015 (send_error && receive_requested_reply) ||
1016 (send_error && own) ||
1017 (send_error && user) ||
1018 (send_error && group)) ||
1020 ((send_destination && receive_interface) ||
1021 (send_destination && receive_member) ||
1022 (send_destination && receive_error) ||
1023 (send_destination && receive_sender) ||
1024 (send_destination && eavesdrop) ||
1025 (send_destination && receive_requested_reply) ||
1026 (send_destination && own) ||
1027 (send_destination && user) ||
1028 (send_destination && group)) ||
1030 ((send_type && receive_interface) ||
1031 (send_type && receive_member) ||
1032 (send_type && receive_error) ||
1033 (send_type && receive_sender) ||
1034 (send_type && eavesdrop) ||
1035 (send_type && receive_requested_reply) ||
1036 (send_type && own) ||
1037 (send_type && user) ||
1038 (send_type && group)) ||
1040 ((send_path && receive_interface) ||
1041 (send_path && receive_member) ||
1042 (send_path && receive_error) ||
1043 (send_path && receive_sender) ||
1044 (send_path && eavesdrop) ||
1045 (send_path && receive_requested_reply) ||
1046 (send_path && own) ||
1047 (send_path && user) ||
1048 (send_path && group)) ||
1050 ((send_requested_reply && receive_interface) ||
1051 (send_requested_reply && receive_member) ||
1052 (send_requested_reply && receive_error) ||
1053 (send_requested_reply && receive_sender) ||
1054 (send_requested_reply && eavesdrop) ||
1055 (send_requested_reply && receive_requested_reply) ||
1056 (send_requested_reply && own) ||
1057 (send_requested_reply && user) ||
1058 (send_requested_reply && group)) ||
1060 ((receive_interface && receive_error) ||
1061 (receive_interface && own) ||
1062 (receive_interface && user) ||
1063 (receive_interface && group)) ||
1065 ((receive_member && receive_error) ||
1066 (receive_member && own) ||
1067 (receive_member && user) ||
1068 (receive_member && group)) ||
1070 ((receive_error && own) ||
1071 (receive_error && user) ||
1072 (receive_error && group)) ||
1074 ((eavesdrop && own) ||
1075 (eavesdrop && user) ||
1076 (eavesdrop && group)) ||
1078 ((receive_requested_reply && own) ||
1079 (receive_requested_reply && user) ||
1080 (receive_requested_reply && group)) ||
1087 dbus_set_error (error, DBUS_ERROR_FAILED,
1088 "Invalid combination of attributes on element <%s>",
1095 /* In BusPolicyRule, NULL represents wildcard.
1096 * In the config file, '*' represents it.
1098 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1100 if (send_interface || send_member || send_error || send_destination ||
1101 send_path || send_type || send_requested_reply)
1105 if (IS_WILDCARD (send_interface))
1106 send_interface = NULL;
1107 if (IS_WILDCARD (send_member))
1109 if (IS_WILDCARD (send_error))
1111 if (IS_WILDCARD (send_destination))
1112 send_destination = NULL;
1113 if (IS_WILDCARD (send_path))
1115 if (IS_WILDCARD (send_type))
1118 message_type = DBUS_MESSAGE_TYPE_INVALID;
1119 if (send_type != NULL)
1121 message_type = dbus_message_type_from_string (send_type);
1122 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1124 dbus_set_error (error, DBUS_ERROR_FAILED,
1125 "Bad message type \"%s\"",
1131 if (send_requested_reply &&
1132 !(strcmp (send_requested_reply, "true") == 0 ||
1133 strcmp (send_requested_reply, "false") == 0))
1135 dbus_set_error (error, DBUS_ERROR_FAILED,
1136 "Bad value \"%s\" for %s attribute, must be true or false",
1137 "send_requested_reply", send_requested_reply);
1141 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1145 if (send_requested_reply)
1146 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1148 rule->d.send.message_type = message_type;
1149 rule->d.send.path = _dbus_strdup (send_path);
1150 rule->d.send.interface = _dbus_strdup (send_interface);
1151 rule->d.send.member = _dbus_strdup (send_member);
1152 rule->d.send.error = _dbus_strdup (send_error);
1153 rule->d.send.destination = _dbus_strdup (send_destination);
1154 if (send_path && rule->d.send.path == NULL)
1156 if (send_interface && rule->d.send.interface == NULL)
1158 if (send_member && rule->d.send.member == NULL)
1160 if (send_error && rule->d.send.error == NULL)
1162 if (send_destination && rule->d.send.destination == NULL)
1165 else if (receive_interface || receive_member || receive_error || receive_sender ||
1166 receive_path || receive_type || eavesdrop || receive_requested_reply)
1170 if (IS_WILDCARD (receive_interface))
1171 receive_interface = NULL;
1172 if (IS_WILDCARD (receive_member))
1173 receive_member = NULL;
1174 if (IS_WILDCARD (receive_error))
1175 receive_error = NULL;
1176 if (IS_WILDCARD (receive_sender))
1177 receive_sender = NULL;
1178 if (IS_WILDCARD (receive_path))
1179 receive_path = NULL;
1180 if (IS_WILDCARD (receive_type))
1181 receive_type = NULL;
1183 message_type = DBUS_MESSAGE_TYPE_INVALID;
1184 if (receive_type != NULL)
1186 message_type = dbus_message_type_from_string (receive_type);
1187 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1189 dbus_set_error (error, DBUS_ERROR_FAILED,
1190 "Bad message type \"%s\"",
1198 !(strcmp (eavesdrop, "true") == 0 ||
1199 strcmp (eavesdrop, "false") == 0))
1201 dbus_set_error (error, DBUS_ERROR_FAILED,
1202 "Bad value \"%s\" for %s attribute, must be true or false",
1203 "eavesdrop", eavesdrop);
1207 if (receive_requested_reply &&
1208 !(strcmp (receive_requested_reply, "true") == 0 ||
1209 strcmp (receive_requested_reply, "false") == 0))
1211 dbus_set_error (error, DBUS_ERROR_FAILED,
1212 "Bad value \"%s\" for %s attribute, must be true or false",
1213 "receive_requested_reply", receive_requested_reply);
1217 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1222 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1224 if (receive_requested_reply)
1225 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1227 rule->d.receive.message_type = message_type;
1228 rule->d.receive.path = _dbus_strdup (receive_path);
1229 rule->d.receive.interface = _dbus_strdup (receive_interface);
1230 rule->d.receive.member = _dbus_strdup (receive_member);
1231 rule->d.receive.error = _dbus_strdup (receive_error);
1232 rule->d.receive.origin = _dbus_strdup (receive_sender);
1234 if (receive_path && rule->d.receive.path == NULL)
1236 if (receive_interface && rule->d.receive.interface == NULL)
1238 if (receive_member && rule->d.receive.member == NULL)
1240 if (receive_error && rule->d.receive.error == NULL)
1242 if (receive_sender && rule->d.receive.origin == NULL)
1247 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1251 if (IS_WILDCARD (own))
1254 rule->d.own.service_name = _dbus_strdup (own);
1255 if (own && rule->d.own.service_name == NULL)
1260 if (IS_WILDCARD (user))
1262 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1266 rule->d.user.uid = DBUS_UID_UNSET;
1270 DBusString username;
1273 _dbus_string_init_const (&username, user);
1275 if (_dbus_get_user_id (&username, &uid))
1277 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1281 rule->d.user.uid = uid;
1285 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1286 user, element_name);
1292 if (IS_WILDCARD (group))
1294 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1298 rule->d.group.gid = DBUS_GID_UNSET;
1302 DBusString groupname;
1305 _dbus_string_init_const (&groupname, group);
1307 if (_dbus_get_user_id (&groupname, &gid))
1309 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1313 rule->d.group.gid = gid;
1317 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1318 group, element_name);
1323 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1329 pe = peek_element (parser);
1330 _dbus_assert (pe != NULL);
1331 _dbus_assert (pe->type == ELEMENT_POLICY);
1333 switch (pe->d.policy.type)
1335 case POLICY_IGNORED:
1336 /* drop the rule on the floor */
1339 case POLICY_DEFAULT:
1340 if (!bus_policy_append_default_rule (parser->policy, rule))
1343 case POLICY_MANDATORY:
1344 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1348 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1350 dbus_set_error (error, DBUS_ERROR_FAILED,
1351 "<%s> rule cannot be per-user because it has bus-global semantics",
1356 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
1361 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1363 dbus_set_error (error, DBUS_ERROR_FAILED,
1364 "<%s> rule cannot be per-group because it has bus-global semantics",
1369 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
1375 bus_policy_rule_unref (rule);
1382 BUS_SET_OOM (error);
1385 bus_policy_rule_unref (rule);
1390 start_policy_child (BusConfigParser *parser,
1391 const char *element_name,
1392 const char **attribute_names,
1393 const char **attribute_values,
1396 if (strcmp (element_name, "allow") == 0)
1398 if (!append_rule_from_element (parser, element_name,
1399 attribute_names, attribute_values,
1403 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1405 BUS_SET_OOM (error);
1411 else if (strcmp (element_name, "deny") == 0)
1413 if (!append_rule_from_element (parser, element_name,
1414 attribute_names, attribute_values,
1418 if (push_element (parser, ELEMENT_DENY) == NULL)
1420 BUS_SET_OOM (error);
1428 dbus_set_error (error, DBUS_ERROR_FAILED,
1429 "Element <%s> not allowed inside <%s> in configuration file",
1430 element_name, "policy");
1436 start_selinux_child (BusConfigParser *parser,
1437 const char *element_name,
1438 const char **attribute_names,
1439 const char **attribute_values,
1442 if (strcmp (element_name, "associate") == 0)
1445 const char *context;
1447 if (!locate_attributes (parser, "associate",
1452 "context", &context,
1456 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1458 BUS_SET_OOM (error);
1462 if (own == NULL || context == NULL)
1464 dbus_set_error (error, DBUS_ERROR_FAILED,
1465 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1469 if (!bus_selinux_id_table_insert (parser->service_sid_table,
1472 BUS_SET_OOM (error);
1480 dbus_set_error (error, DBUS_ERROR_FAILED,
1481 "Element <%s> not allowed inside <%s> in configuration file",
1482 element_name, "selinux");
1488 bus_config_parser_start_element (BusConfigParser *parser,
1489 const char *element_name,
1490 const char **attribute_names,
1491 const char **attribute_values,
1496 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1498 /* printf ("START: %s\n", element_name); */
1500 t = top_element_type (parser);
1502 if (t == ELEMENT_NONE)
1504 if (strcmp (element_name, "busconfig") == 0)
1506 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1509 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1511 BUS_SET_OOM (error);
1519 dbus_set_error (error, DBUS_ERROR_FAILED,
1520 "Unknown element <%s> at root of configuration file",
1525 else if (t == ELEMENT_BUSCONFIG)
1527 return start_busconfig_child (parser, element_name,
1528 attribute_names, attribute_values,
1531 else if (t == ELEMENT_POLICY)
1533 return start_policy_child (parser, element_name,
1534 attribute_names, attribute_values,
1537 else if (t == ELEMENT_SELINUX)
1539 return start_selinux_child (parser, element_name,
1540 attribute_names, attribute_values,
1545 dbus_set_error (error, DBUS_ERROR_FAILED,
1546 "Element <%s> is not allowed in this context",
1553 set_limit (BusConfigParser *parser,
1558 dbus_bool_t must_be_positive;
1559 dbus_bool_t must_be_int;
1561 must_be_int = FALSE;
1562 must_be_positive = FALSE;
1564 if (strcmp (name, "max_incoming_bytes") == 0)
1566 must_be_positive = TRUE;
1567 parser->limits.max_incoming_bytes = value;
1569 else if (strcmp (name, "max_outgoing_bytes") == 0)
1571 must_be_positive = TRUE;
1572 parser->limits.max_outgoing_bytes = value;
1574 else if (strcmp (name, "max_message_size") == 0)
1576 must_be_positive = TRUE;
1577 parser->limits.max_message_size = value;
1579 else if (strcmp (name, "activation_timeout") == 0)
1581 must_be_positive = TRUE;
1583 parser->limits.activation_timeout = value;
1585 else if (strcmp (name, "auth_timeout") == 0)
1587 must_be_positive = TRUE;
1589 parser->limits.auth_timeout = value;
1591 else if (strcmp (name, "reply_timeout") == 0)
1593 must_be_positive = TRUE;
1595 parser->limits.reply_timeout = value;
1597 else if (strcmp (name, "max_completed_connections") == 0)
1599 must_be_positive = TRUE;
1601 parser->limits.max_completed_connections = value;
1603 else if (strcmp (name, "max_incomplete_connections") == 0)
1605 must_be_positive = TRUE;
1607 parser->limits.max_incomplete_connections = value;
1609 else if (strcmp (name, "max_connections_per_user") == 0)
1611 must_be_positive = TRUE;
1613 parser->limits.max_connections_per_user = value;
1615 else if (strcmp (name, "max_pending_activations") == 0)
1617 must_be_positive = TRUE;
1619 parser->limits.max_pending_activations = value;
1621 else if (strcmp (name, "max_services_per_connection") == 0)
1623 must_be_positive = TRUE;
1625 parser->limits.max_services_per_connection = value;
1627 else if (strcmp (name, "max_replies_per_connection") == 0)
1629 must_be_positive = TRUE;
1631 parser->limits.max_replies_per_connection = value;
1635 dbus_set_error (error, DBUS_ERROR_FAILED,
1636 "There is no limit called \"%s\"\n",
1641 if (must_be_positive && value < 0)
1643 dbus_set_error (error, DBUS_ERROR_FAILED,
1644 "<limit name=\"%s\"> must be a positive number\n",
1650 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1652 dbus_set_error (error, DBUS_ERROR_FAILED,
1653 "<limit name=\"%s\"> value is too large\n",
1662 bus_config_parser_end_element (BusConfigParser *parser,
1663 const char *element_name,
1670 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1672 /* printf ("END: %s\n", element_name); */
1674 t = top_element_type (parser);
1676 if (t == ELEMENT_NONE)
1678 /* should probably be an assertion failure but
1679 * being paranoid about XML parsers
1681 dbus_set_error (error, DBUS_ERROR_FAILED,
1682 "XML parser ended element with no element on the stack");
1686 n = element_type_to_name (t);
1687 _dbus_assert (n != NULL);
1688 if (strcmp (n, element_name) != 0)
1690 /* should probably be an assertion failure but
1691 * being paranoid about XML parsers
1693 dbus_set_error (error, DBUS_ERROR_FAILED,
1694 "XML element <%s> ended but topmost element on the stack was <%s>",
1699 e = peek_element (parser);
1700 _dbus_assert (e != NULL);
1705 _dbus_assert_not_reached ("element in stack has no type");
1708 case ELEMENT_INCLUDE:
1711 case ELEMENT_LISTEN:
1712 case ELEMENT_PIDFILE:
1714 case ELEMENT_SERVICEDIR:
1715 case ELEMENT_INCLUDEDIR:
1717 if (!e->had_content)
1719 dbus_set_error (error, DBUS_ERROR_FAILED,
1720 "XML element <%s> was expected to have content inside it",
1721 element_type_to_name (e->type));
1725 if (e->type == ELEMENT_LIMIT)
1727 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1733 case ELEMENT_BUSCONFIG:
1734 case ELEMENT_POLICY:
1738 case ELEMENT_SELINUX:
1739 case ELEMENT_ASSOCIATE:
1743 pop_element (parser);
1749 all_whitespace (const DBusString *str)
1753 _dbus_string_skip_white (str, 0, &i);
1755 return i == _dbus_string_get_length (str);
1759 make_full_path (const DBusString *basedir,
1760 const DBusString *filename,
1761 DBusString *full_path)
1763 if (_dbus_path_is_absolute (filename))
1765 return _dbus_string_copy (filename, 0, full_path, 0);
1769 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1772 if (!_dbus_concat_dir_and_file (full_path, filename))
1780 include_file (BusConfigParser *parser,
1781 const DBusString *filename,
1782 dbus_bool_t ignore_missing,
1785 /* FIXME good test case for this would load each config file in the
1786 * test suite both alone, and as an include, and check
1787 * that the result is the same
1789 BusConfigParser *included;
1790 const char *filename_str;
1791 DBusError tmp_error;
1793 dbus_error_init (&tmp_error);
1795 filename_str = _dbus_string_get_const_data (filename);
1797 /* Check to make sure this file hasn't already been included. */
1798 if (seen_include (parser, filename))
1800 dbus_set_error (error, DBUS_ERROR_FAILED,
1801 "Circular inclusion of file '%s'",
1806 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
1808 BUS_SET_OOM (error);
1812 /* Since parser is passed in as the parent, included
1813 inherits parser's limits. */
1814 included = bus_config_load (filename, FALSE, parser, &tmp_error);
1816 _dbus_list_pop_last (&parser->included_files);
1818 if (included == NULL)
1820 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1822 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1825 dbus_error_free (&tmp_error);
1830 dbus_move_error (&tmp_error, error);
1836 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1838 if (!merge_included (parser, included, error))
1840 bus_config_parser_unref (included);
1844 /* Copy included's limits back to parser. */
1845 parser->limits = included->limits;
1847 bus_config_parser_unref (included);
1853 include_dir (BusConfigParser *parser,
1854 const DBusString *dirname,
1857 DBusString filename;
1859 DBusError tmp_error;
1862 if (!_dbus_string_init (&filename))
1864 BUS_SET_OOM (error);
1870 dir = _dbus_directory_open (dirname, error);
1875 dbus_error_init (&tmp_error);
1876 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1878 DBusString full_path;
1880 if (!_dbus_string_init (&full_path))
1882 BUS_SET_OOM (error);
1886 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1888 BUS_SET_OOM (error);
1889 _dbus_string_free (&full_path);
1893 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1895 BUS_SET_OOM (error);
1896 _dbus_string_free (&full_path);
1900 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1902 if (!include_file (parser, &full_path, TRUE, error))
1904 _dbus_string_free (&full_path);
1909 _dbus_string_free (&full_path);
1912 if (dbus_error_is_set (&tmp_error))
1914 dbus_move_error (&tmp_error, error);
1921 _dbus_string_free (&filename);
1924 _dbus_directory_close (dir);
1930 bus_config_parser_content (BusConfigParser *parser,
1931 const DBusString *content,
1936 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1942 _dbus_string_get_const_data (content, &c_str);
1944 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1948 e = peek_element (parser);
1951 dbus_set_error (error, DBUS_ERROR_FAILED,
1952 "Text content outside of any XML element in configuration file");
1955 else if (e->had_content)
1957 _dbus_assert_not_reached ("Element had multiple content blocks");
1961 switch (top_element_type (parser))
1964 _dbus_assert_not_reached ("element at top of stack has no type");
1967 case ELEMENT_BUSCONFIG:
1968 case ELEMENT_POLICY:
1972 case ELEMENT_SELINUX:
1973 case ELEMENT_ASSOCIATE:
1974 if (all_whitespace (content))
1978 dbus_set_error (error, DBUS_ERROR_FAILED,
1979 "No text content expected inside XML element %s in configuration file",
1980 element_type_to_name (top_element_type (parser)));
1984 case ELEMENT_PIDFILE:
1988 e->had_content = TRUE;
1990 if (!_dbus_string_copy_data (content, &s))
1993 dbus_free (parser->pidfile);
1994 parser->pidfile = s;
1998 case ELEMENT_INCLUDE:
2000 DBusString full_path;
2002 e->had_content = TRUE;
2004 if (!_dbus_string_init (&full_path))
2007 if (!make_full_path (&parser->basedir, content, &full_path))
2009 _dbus_string_free (&full_path);
2013 if (!include_file (parser, &full_path,
2014 e->d.include.ignore_missing, error))
2016 _dbus_string_free (&full_path);
2020 _dbus_string_free (&full_path);
2024 case ELEMENT_INCLUDEDIR:
2026 DBusString full_path;
2028 e->had_content = TRUE;
2030 if (!_dbus_string_init (&full_path))
2033 if (!make_full_path (&parser->basedir, content, &full_path))
2035 _dbus_string_free (&full_path);
2039 if (!include_dir (parser, &full_path, error))
2041 _dbus_string_free (&full_path);
2045 _dbus_string_free (&full_path);
2053 e->had_content = TRUE;
2055 if (!_dbus_string_copy_data (content, &s))
2058 dbus_free (parser->user);
2067 e->had_content = TRUE;
2069 if (!_dbus_string_copy_data (content, &s))
2072 dbus_free (parser->bus_type);
2073 parser->bus_type = s;
2077 case ELEMENT_LISTEN:
2081 e->had_content = TRUE;
2083 if (!_dbus_string_copy_data (content, &s))
2086 if (!_dbus_list_append (&parser->listen_on,
2099 e->had_content = TRUE;
2101 if (!_dbus_string_copy_data (content, &s))
2104 if (!_dbus_list_append (&parser->mechanisms,
2113 case ELEMENT_SERVICEDIR:
2116 DBusString full_path;
2118 e->had_content = TRUE;
2120 if (!_dbus_string_init (&full_path))
2123 if (!make_full_path (&parser->basedir, content, &full_path))
2125 _dbus_string_free (&full_path);
2129 if (!_dbus_string_copy_data (&full_path, &s))
2131 _dbus_string_free (&full_path);
2135 if (!_dbus_list_append (&parser->service_dirs, s))
2137 _dbus_string_free (&full_path);
2142 _dbus_string_free (&full_path);
2150 e->had_content = TRUE;
2153 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2155 dbus_set_error (error, DBUS_ERROR_FAILED,
2156 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2161 e->d.limit.value = val;
2163 _dbus_verbose ("Loaded value %ld for limit %s\n",
2170 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2174 BUS_SET_OOM (error);
2179 bus_config_parser_finished (BusConfigParser *parser,
2182 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2184 if (parser->stack != NULL)
2186 dbus_set_error (error, DBUS_ERROR_FAILED,
2187 "Element <%s> was not closed in configuration file",
2188 element_type_to_name (top_element_type (parser)));
2193 if (parser->is_toplevel && parser->listen_on == NULL)
2195 dbus_set_error (error, DBUS_ERROR_FAILED,
2196 "Configuration file needs one or more <listen> elements giving addresses");
2204 bus_config_parser_get_user (BusConfigParser *parser)
2206 return parser->user;
2210 bus_config_parser_get_type (BusConfigParser *parser)
2212 return parser->bus_type;
2216 bus_config_parser_get_addresses (BusConfigParser *parser)
2218 return &parser->listen_on;
2222 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2224 return &parser->mechanisms;
2228 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2230 return &parser->service_dirs;
2234 bus_config_parser_get_fork (BusConfigParser *parser)
2236 return parser->fork;
2240 bus_config_parser_get_pidfile (BusConfigParser *parser)
2242 return parser->pidfile;
2246 bus_config_parser_steal_policy (BusConfigParser *parser)
2250 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2252 policy = parser->policy;
2254 parser->policy = NULL;
2259 /* Overwrite any limits that were set in the configuration file */
2261 bus_config_parser_get_limits (BusConfigParser *parser,
2264 *limits = parser->limits;
2268 bus_config_parser_steal_service_sid_table (BusConfigParser *parser)
2270 DBusHashTable *table;
2272 _dbus_assert (parser->service_sid_table != NULL); /* can only steal once */
2274 table = parser->service_sid_table;
2276 parser->service_sid_table = NULL;
2281 #ifdef DBUS_BUILD_TESTS
2292 do_load (const DBusString *full_path,
2294 dbus_bool_t oom_possible)
2296 BusConfigParser *parser;
2299 dbus_error_init (&error);
2301 parser = bus_config_load (full_path, TRUE, NULL, &error);
2304 _DBUS_ASSERT_ERROR_IS_SET (&error);
2307 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2309 _dbus_verbose ("Failed to load valid file due to OOM\n");
2310 dbus_error_free (&error);
2313 else if (validity == VALID)
2315 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2318 dbus_error_free (&error);
2323 dbus_error_free (&error);
2329 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2331 bus_config_parser_unref (parser);
2333 if (validity == INVALID)
2335 _dbus_warn ("Accepted invalid file\n");
2345 const DBusString *full_path;
2350 check_loader_oom_func (void *data)
2352 LoaderOomData *d = data;
2354 return do_load (d->full_path, d->validity, TRUE);
2358 process_test_valid_subdir (const DBusString *test_base_dir,
2362 DBusString test_directory;
2363 DBusString filename;
2371 if (!_dbus_string_init (&test_directory))
2372 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2374 _dbus_string_init_const (&filename, subdir);
2376 if (!_dbus_string_copy (test_base_dir, 0,
2377 &test_directory, 0))
2378 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2380 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2381 _dbus_assert_not_reached ("couldn't allocate full path");
2383 _dbus_string_free (&filename);
2384 if (!_dbus_string_init (&filename))
2385 _dbus_assert_not_reached ("didn't allocate filename string\n");
2387 dbus_error_init (&error);
2388 dir = _dbus_directory_open (&test_directory, &error);
2391 _dbus_warn ("Could not open %s: %s\n",
2392 _dbus_string_get_const_data (&test_directory),
2394 dbus_error_free (&error);
2398 if (validity == VALID)
2399 printf ("Testing valid files:\n");
2400 else if (validity == INVALID)
2401 printf ("Testing invalid files:\n");
2403 printf ("Testing unknown files:\n");
2406 while (_dbus_directory_get_next_file (dir, &filename, &error))
2408 DBusString full_path;
2411 if (!_dbus_string_init (&full_path))
2412 _dbus_assert_not_reached ("couldn't init string");
2414 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2415 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2417 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2418 _dbus_assert_not_reached ("couldn't concat file to dir");
2420 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2422 _dbus_verbose ("Skipping non-.conf file %s\n",
2423 _dbus_string_get_const_data (&filename));
2424 _dbus_string_free (&full_path);
2428 printf (" %s\n", _dbus_string_get_const_data (&filename));
2430 _dbus_verbose (" expecting %s\n",
2431 validity == VALID ? "valid" :
2432 (validity == INVALID ? "invalid" :
2433 (validity == UNKNOWN ? "unknown" : "???")));
2435 d.full_path = &full_path;
2436 d.validity = validity;
2438 /* FIXME hackaround for an expat problem, see
2439 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2440 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2442 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2443 if (!check_loader_oom_func (&d))
2444 _dbus_assert_not_reached ("test failed");
2446 _dbus_string_free (&full_path);
2449 if (dbus_error_is_set (&error))
2451 _dbus_warn ("Could not get next file in %s: %s\n",
2452 _dbus_string_get_const_data (&test_directory),
2454 dbus_error_free (&error);
2463 _dbus_directory_close (dir);
2464 _dbus_string_free (&test_directory);
2465 _dbus_string_free (&filename);
2471 bools_equal (dbus_bool_t a,
2478 strings_equal_or_both_null (const char *a,
2481 if (a == NULL || b == NULL)
2484 return !strcmp (a, b);
2488 elements_equal (const Element *a,
2491 if (a->type != b->type)
2494 if (!bools_equal (a->had_content, b->had_content))
2500 case ELEMENT_INCLUDE:
2501 if (!bools_equal (a->d.include.ignore_missing,
2502 b->d.include.ignore_missing))
2506 case ELEMENT_POLICY:
2507 if (a->d.policy.type != b->d.policy.type)
2509 if (a->d.policy.gid_or_uid != b->d.policy.gid_or_uid)
2514 if (strcmp (a->d.limit.name, b->d.limit.name))
2516 if (a->d.limit.value != b->d.limit.value)
2530 lists_of_elements_equal (DBusList *a,
2539 while (ia != NULL && ib != NULL)
2541 if (elements_equal (ia->data, ib->data))
2543 ia = _dbus_list_get_next_link (&a, ia);
2544 ib = _dbus_list_get_next_link (&b, ib);
2547 return ia == NULL && ib == NULL;
2551 lists_of_c_strings_equal (DBusList *a,
2560 while (ia != NULL && ib != NULL)
2562 if (strcmp (ia->data, ib->data))
2564 ia = _dbus_list_get_next_link (&a, ia);
2565 ib = _dbus_list_get_next_link (&b, ib);
2568 return ia == NULL && ib == NULL;
2572 limits_equal (const BusLimits *a,
2576 (a->max_incoming_bytes == b->max_incoming_bytes
2577 || a->max_outgoing_bytes == b->max_outgoing_bytes
2578 || a->max_message_size == b->max_message_size
2579 || a->activation_timeout == b->activation_timeout
2580 || a->auth_timeout == b->auth_timeout
2581 || a->max_completed_connections == b->max_completed_connections
2582 || a->max_incomplete_connections == b->max_incomplete_connections
2583 || a->max_connections_per_user == b->max_connections_per_user
2584 || a->max_pending_activations == b->max_pending_activations
2585 || a->max_services_per_connection == b->max_services_per_connection
2586 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2587 || a->max_replies_per_connection == b->max_replies_per_connection
2588 || a->reply_timeout == b->reply_timeout);
2592 config_parsers_equal (const BusConfigParser *a,
2593 const BusConfigParser *b)
2595 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2598 if (!lists_of_elements_equal (a->stack, b->stack))
2601 if (!strings_equal_or_both_null (a->user, b->user))
2604 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2607 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2610 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2613 /* FIXME: compare policy */
2615 /* FIXME: compare service selinux ID table */
2617 if (! limits_equal (&a->limits, &b->limits))
2620 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2623 if (! bools_equal (a->fork, b->fork))
2626 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2633 all_are_equiv (const DBusString *target_directory)
2635 DBusString filename;
2637 BusConfigParser *first_parser;
2638 BusConfigParser *parser;
2644 first_parser = NULL;
2648 if (!_dbus_string_init (&filename))
2649 _dbus_assert_not_reached ("didn't allocate filename string");
2651 dbus_error_init (&error);
2652 dir = _dbus_directory_open (target_directory, &error);
2655 _dbus_warn ("Could not open %s: %s\n",
2656 _dbus_string_get_const_data (target_directory),
2658 dbus_error_free (&error);
2662 printf ("Comparing equivalent files:\n");
2665 while (_dbus_directory_get_next_file (dir, &filename, &error))
2667 DBusString full_path;
2669 if (!_dbus_string_init (&full_path))
2670 _dbus_assert_not_reached ("couldn't init string");
2672 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2673 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2675 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2676 _dbus_assert_not_reached ("couldn't concat file to dir");
2678 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2680 _dbus_verbose ("Skipping non-.conf file %s\n",
2681 _dbus_string_get_const_data (&filename));
2682 _dbus_string_free (&full_path);
2686 printf (" %s\n", _dbus_string_get_const_data (&filename));
2688 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2689 _dbus_string_free (&full_path);
2693 _dbus_warn ("Could not load file %s: %s\n",
2694 _dbus_string_get_const_data (&full_path),
2696 dbus_error_free (&error);
2699 else if (first_parser == NULL)
2701 first_parser = parser;
2705 equal = config_parsers_equal (first_parser, parser);
2706 bus_config_parser_unref (parser);
2716 _dbus_string_free (&filename);
2718 bus_config_parser_unref (first_parser);
2720 _dbus_directory_close (dir);
2727 process_test_equiv_subdir (const DBusString *test_base_dir,
2730 DBusString test_directory;
2731 DBusString filename;
2740 if (!_dbus_string_init (&test_directory))
2741 _dbus_assert_not_reached ("didn't allocate test_directory");
2743 _dbus_string_init_const (&filename, subdir);
2745 if (!_dbus_string_copy (test_base_dir, 0,
2746 &test_directory, 0))
2747 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2749 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2750 _dbus_assert_not_reached ("couldn't allocate full path");
2752 _dbus_string_free (&filename);
2753 if (!_dbus_string_init (&filename))
2754 _dbus_assert_not_reached ("didn't allocate filename string");
2756 dbus_error_init (&error);
2757 dir = _dbus_directory_open (&test_directory, &error);
2760 _dbus_warn ("Could not open %s: %s\n",
2761 _dbus_string_get_const_data (&test_directory),
2763 dbus_error_free (&error);
2767 while (_dbus_directory_get_next_file (dir, &filename, &error))
2769 DBusString full_path;
2771 /* Skip CVS's magic directories! */
2772 if (_dbus_string_equal_c_str (&filename, "CVS"))
2775 if (!_dbus_string_init (&full_path))
2776 _dbus_assert_not_reached ("couldn't init string");
2778 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2779 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2781 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2782 _dbus_assert_not_reached ("couldn't concat file to dir");
2784 equal = all_are_equiv (&full_path);
2785 _dbus_string_free (&full_path);
2794 _dbus_string_free (&test_directory);
2795 _dbus_string_free (&filename);
2797 _dbus_directory_close (dir);
2804 bus_config_parser_test (const DBusString *test_data_dir)
2806 if (test_data_dir == NULL ||
2807 _dbus_string_get_length (test_data_dir) == 0)
2809 printf ("No test data\n");
2813 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
2816 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
2819 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
2825 #endif /* DBUS_BUILD_TESTS */