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)
874 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
877 if (push_element (parser, ELEMENT_SELINUX) == NULL)
887 dbus_set_error (error, DBUS_ERROR_FAILED,
888 "Element <%s> not allowed inside <%s> in configuration file",
889 element_name, "busconfig");
895 append_rule_from_element (BusConfigParser *parser,
896 const char *element_name,
897 const char **attribute_names,
898 const char **attribute_values,
902 const char *send_interface;
903 const char *send_member;
904 const char *send_error;
905 const char *send_destination;
906 const char *send_path;
907 const char *send_type;
908 const char *receive_interface;
909 const char *receive_member;
910 const char *receive_error;
911 const char *receive_sender;
912 const char *receive_path;
913 const char *receive_type;
914 const char *eavesdrop;
915 const char *send_requested_reply;
916 const char *receive_requested_reply;
922 if (!locate_attributes (parser, element_name,
926 "send_interface", &send_interface,
927 "send_member", &send_member,
928 "send_error", &send_error,
929 "send_destination", &send_destination,
930 "send_path", &send_path,
931 "send_type", &send_type,
932 "receive_interface", &receive_interface,
933 "receive_member", &receive_member,
934 "receive_error", &receive_error,
935 "receive_sender", &receive_sender,
936 "receive_path", &receive_path,
937 "receive_type", &receive_type,
938 "eavesdrop", &eavesdrop,
939 "send_requested_reply", &send_requested_reply,
940 "receive_requested_reply", &receive_requested_reply,
947 if (!(send_interface || send_member || send_error || send_destination ||
948 send_type || send_path ||
949 receive_interface || receive_member || receive_error || receive_sender ||
950 receive_type || receive_path || eavesdrop ||
951 send_requested_reply || receive_requested_reply ||
952 own || user || group))
954 dbus_set_error (error, DBUS_ERROR_FAILED,
955 "Element <%s> must have one or more attributes",
960 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
961 (receive_member && (receive_interface == NULL && receive_path == NULL)))
963 dbus_set_error (error, DBUS_ERROR_FAILED,
964 "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.",
969 /* Allowed combinations of elements are:
971 * base, must be all send or all receive:
977 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
978 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
980 * user, group, own must occur alone
982 * Pretty sure the below stuff is broken, FIXME think about it more.
985 if (((send_interface && send_error) ||
986 (send_interface && receive_interface) ||
987 (send_interface && receive_member) ||
988 (send_interface && receive_error) ||
989 (send_interface && receive_sender) ||
990 (send_interface && eavesdrop) ||
991 (send_interface && receive_requested_reply) ||
992 (send_interface && own) ||
993 (send_interface && user) ||
994 (send_interface && group)) ||
996 ((send_member && send_error) ||
997 (send_member && receive_interface) ||
998 (send_member && receive_member) ||
999 (send_member && receive_error) ||
1000 (send_member && receive_sender) ||
1001 (send_member && eavesdrop) ||
1002 (send_member && receive_requested_reply) ||
1003 (send_member && own) ||
1004 (send_member && user) ||
1005 (send_member && group)) ||
1007 ((send_error && receive_interface) ||
1008 (send_error && receive_member) ||
1009 (send_error && receive_error) ||
1010 (send_error && receive_sender) ||
1011 (send_error && eavesdrop) ||
1012 (send_error && receive_requested_reply) ||
1013 (send_error && own) ||
1014 (send_error && user) ||
1015 (send_error && group)) ||
1017 ((send_destination && receive_interface) ||
1018 (send_destination && receive_member) ||
1019 (send_destination && receive_error) ||
1020 (send_destination && receive_sender) ||
1021 (send_destination && eavesdrop) ||
1022 (send_destination && receive_requested_reply) ||
1023 (send_destination && own) ||
1024 (send_destination && user) ||
1025 (send_destination && group)) ||
1027 ((send_type && receive_interface) ||
1028 (send_type && receive_member) ||
1029 (send_type && receive_error) ||
1030 (send_type && receive_sender) ||
1031 (send_type && eavesdrop) ||
1032 (send_type && receive_requested_reply) ||
1033 (send_type && own) ||
1034 (send_type && user) ||
1035 (send_type && group)) ||
1037 ((send_path && receive_interface) ||
1038 (send_path && receive_member) ||
1039 (send_path && receive_error) ||
1040 (send_path && receive_sender) ||
1041 (send_path && eavesdrop) ||
1042 (send_path && receive_requested_reply) ||
1043 (send_path && own) ||
1044 (send_path && user) ||
1045 (send_path && group)) ||
1047 ((send_requested_reply && receive_interface) ||
1048 (send_requested_reply && receive_member) ||
1049 (send_requested_reply && receive_error) ||
1050 (send_requested_reply && receive_sender) ||
1051 (send_requested_reply && eavesdrop) ||
1052 (send_requested_reply && receive_requested_reply) ||
1053 (send_requested_reply && own) ||
1054 (send_requested_reply && user) ||
1055 (send_requested_reply && group)) ||
1057 ((receive_interface && receive_error) ||
1058 (receive_interface && own) ||
1059 (receive_interface && user) ||
1060 (receive_interface && group)) ||
1062 ((receive_member && receive_error) ||
1063 (receive_member && own) ||
1064 (receive_member && user) ||
1065 (receive_member && group)) ||
1067 ((receive_error && own) ||
1068 (receive_error && user) ||
1069 (receive_error && group)) ||
1071 ((eavesdrop && own) ||
1072 (eavesdrop && user) ||
1073 (eavesdrop && group)) ||
1075 ((receive_requested_reply && own) ||
1076 (receive_requested_reply && user) ||
1077 (receive_requested_reply && group)) ||
1084 dbus_set_error (error, DBUS_ERROR_FAILED,
1085 "Invalid combination of attributes on element <%s>",
1092 /* In BusPolicyRule, NULL represents wildcard.
1093 * In the config file, '*' represents it.
1095 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1097 if (send_interface || send_member || send_error || send_destination ||
1098 send_path || send_type || send_requested_reply)
1102 if (IS_WILDCARD (send_interface))
1103 send_interface = NULL;
1104 if (IS_WILDCARD (send_member))
1106 if (IS_WILDCARD (send_error))
1108 if (IS_WILDCARD (send_destination))
1109 send_destination = NULL;
1110 if (IS_WILDCARD (send_path))
1112 if (IS_WILDCARD (send_type))
1115 message_type = DBUS_MESSAGE_TYPE_INVALID;
1116 if (send_type != NULL)
1118 message_type = dbus_message_type_from_string (send_type);
1119 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1121 dbus_set_error (error, DBUS_ERROR_FAILED,
1122 "Bad message type \"%s\"",
1128 if (send_requested_reply &&
1129 !(strcmp (send_requested_reply, "true") == 0 ||
1130 strcmp (send_requested_reply, "false") == 0))
1132 dbus_set_error (error, DBUS_ERROR_FAILED,
1133 "Bad value \"%s\" for %s attribute, must be true or false",
1134 "send_requested_reply", send_requested_reply);
1138 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1142 if (send_requested_reply)
1143 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1145 rule->d.send.message_type = message_type;
1146 rule->d.send.path = _dbus_strdup (send_path);
1147 rule->d.send.interface = _dbus_strdup (send_interface);
1148 rule->d.send.member = _dbus_strdup (send_member);
1149 rule->d.send.error = _dbus_strdup (send_error);
1150 rule->d.send.destination = _dbus_strdup (send_destination);
1151 if (send_path && rule->d.send.path == NULL)
1153 if (send_interface && rule->d.send.interface == NULL)
1155 if (send_member && rule->d.send.member == NULL)
1157 if (send_error && rule->d.send.error == NULL)
1159 if (send_destination && rule->d.send.destination == NULL)
1162 else if (receive_interface || receive_member || receive_error || receive_sender ||
1163 receive_path || receive_type || eavesdrop || receive_requested_reply)
1167 if (IS_WILDCARD (receive_interface))
1168 receive_interface = NULL;
1169 if (IS_WILDCARD (receive_member))
1170 receive_member = NULL;
1171 if (IS_WILDCARD (receive_error))
1172 receive_error = NULL;
1173 if (IS_WILDCARD (receive_sender))
1174 receive_sender = NULL;
1175 if (IS_WILDCARD (receive_path))
1176 receive_path = NULL;
1177 if (IS_WILDCARD (receive_type))
1178 receive_type = NULL;
1180 message_type = DBUS_MESSAGE_TYPE_INVALID;
1181 if (receive_type != NULL)
1183 message_type = dbus_message_type_from_string (receive_type);
1184 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1186 dbus_set_error (error, DBUS_ERROR_FAILED,
1187 "Bad message type \"%s\"",
1195 !(strcmp (eavesdrop, "true") == 0 ||
1196 strcmp (eavesdrop, "false") == 0))
1198 dbus_set_error (error, DBUS_ERROR_FAILED,
1199 "Bad value \"%s\" for %s attribute, must be true or false",
1200 "eavesdrop", eavesdrop);
1204 if (receive_requested_reply &&
1205 !(strcmp (receive_requested_reply, "true") == 0 ||
1206 strcmp (receive_requested_reply, "false") == 0))
1208 dbus_set_error (error, DBUS_ERROR_FAILED,
1209 "Bad value \"%s\" for %s attribute, must be true or false",
1210 "receive_requested_reply", receive_requested_reply);
1214 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1219 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1221 if (receive_requested_reply)
1222 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1224 rule->d.receive.message_type = message_type;
1225 rule->d.receive.path = _dbus_strdup (receive_path);
1226 rule->d.receive.interface = _dbus_strdup (receive_interface);
1227 rule->d.receive.member = _dbus_strdup (receive_member);
1228 rule->d.receive.error = _dbus_strdup (receive_error);
1229 rule->d.receive.origin = _dbus_strdup (receive_sender);
1231 if (receive_path && rule->d.receive.path == NULL)
1233 if (receive_interface && rule->d.receive.interface == NULL)
1235 if (receive_member && rule->d.receive.member == NULL)
1237 if (receive_error && rule->d.receive.error == NULL)
1239 if (receive_sender && rule->d.receive.origin == NULL)
1244 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1248 if (IS_WILDCARD (own))
1251 rule->d.own.service_name = _dbus_strdup (own);
1252 if (own && rule->d.own.service_name == NULL)
1257 if (IS_WILDCARD (user))
1259 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1263 rule->d.user.uid = DBUS_UID_UNSET;
1267 DBusString username;
1270 _dbus_string_init_const (&username, user);
1272 if (_dbus_get_user_id (&username, &uid))
1274 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1278 rule->d.user.uid = uid;
1282 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1283 user, element_name);
1289 if (IS_WILDCARD (group))
1291 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1295 rule->d.group.gid = DBUS_GID_UNSET;
1299 DBusString groupname;
1302 _dbus_string_init_const (&groupname, group);
1304 if (_dbus_get_user_id (&groupname, &gid))
1306 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1310 rule->d.group.gid = gid;
1314 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1315 group, element_name);
1320 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1326 pe = peek_element (parser);
1327 _dbus_assert (pe != NULL);
1328 _dbus_assert (pe->type == ELEMENT_POLICY);
1330 switch (pe->d.policy.type)
1332 case POLICY_IGNORED:
1333 /* drop the rule on the floor */
1336 case POLICY_DEFAULT:
1337 if (!bus_policy_append_default_rule (parser->policy, rule))
1340 case POLICY_MANDATORY:
1341 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1345 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1347 dbus_set_error (error, DBUS_ERROR_FAILED,
1348 "<%s> rule cannot be per-user because it has bus-global semantics",
1353 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
1358 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1360 dbus_set_error (error, DBUS_ERROR_FAILED,
1361 "<%s> rule cannot be per-group because it has bus-global semantics",
1366 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
1372 bus_policy_rule_unref (rule);
1379 BUS_SET_OOM (error);
1382 bus_policy_rule_unref (rule);
1387 start_policy_child (BusConfigParser *parser,
1388 const char *element_name,
1389 const char **attribute_names,
1390 const char **attribute_values,
1393 if (strcmp (element_name, "allow") == 0)
1395 if (!append_rule_from_element (parser, element_name,
1396 attribute_names, attribute_values,
1400 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1402 BUS_SET_OOM (error);
1408 else if (strcmp (element_name, "deny") == 0)
1410 if (!append_rule_from_element (parser, element_name,
1411 attribute_names, attribute_values,
1415 if (push_element (parser, ELEMENT_DENY) == NULL)
1417 BUS_SET_OOM (error);
1425 dbus_set_error (error, DBUS_ERROR_FAILED,
1426 "Element <%s> not allowed inside <%s> in configuration file",
1427 element_name, "policy");
1433 start_selinux_child (BusConfigParser *parser,
1434 const char *element_name,
1435 const char **attribute_names,
1436 const char **attribute_values,
1439 if (strcmp (element_name, "associate") == 0)
1442 const char *context;
1444 if (!locate_attributes (parser, "associate",
1449 "context", &context,
1453 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1455 BUS_SET_OOM (error);
1459 if (own == NULL || context == NULL)
1461 dbus_set_error (error, DBUS_ERROR_FAILED,
1462 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1466 if (!bus_selinux_id_table_insert (parser->service_sid_table,
1469 BUS_SET_OOM (error);
1477 dbus_set_error (error, DBUS_ERROR_FAILED,
1478 "Element <%s> not allowed inside <%s> in configuration file",
1479 element_name, "selinux");
1485 bus_config_parser_start_element (BusConfigParser *parser,
1486 const char *element_name,
1487 const char **attribute_names,
1488 const char **attribute_values,
1493 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1495 /* printf ("START: %s\n", element_name); */
1497 t = top_element_type (parser);
1499 if (t == ELEMENT_NONE)
1501 if (strcmp (element_name, "busconfig") == 0)
1503 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1506 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1508 BUS_SET_OOM (error);
1516 dbus_set_error (error, DBUS_ERROR_FAILED,
1517 "Unknown element <%s> at root of configuration file",
1522 else if (t == ELEMENT_BUSCONFIG)
1524 return start_busconfig_child (parser, element_name,
1525 attribute_names, attribute_values,
1528 else if (t == ELEMENT_POLICY)
1530 return start_policy_child (parser, element_name,
1531 attribute_names, attribute_values,
1534 else if (t == ELEMENT_SELINUX)
1536 return start_selinux_child (parser, element_name,
1537 attribute_names, attribute_values,
1542 dbus_set_error (error, DBUS_ERROR_FAILED,
1543 "Element <%s> is not allowed in this context",
1550 set_limit (BusConfigParser *parser,
1555 dbus_bool_t must_be_positive;
1556 dbus_bool_t must_be_int;
1558 must_be_int = FALSE;
1559 must_be_positive = FALSE;
1561 if (strcmp (name, "max_incoming_bytes") == 0)
1563 must_be_positive = TRUE;
1564 parser->limits.max_incoming_bytes = value;
1566 else if (strcmp (name, "max_outgoing_bytes") == 0)
1568 must_be_positive = TRUE;
1569 parser->limits.max_outgoing_bytes = value;
1571 else if (strcmp (name, "max_message_size") == 0)
1573 must_be_positive = TRUE;
1574 parser->limits.max_message_size = value;
1576 else if (strcmp (name, "activation_timeout") == 0)
1578 must_be_positive = TRUE;
1580 parser->limits.activation_timeout = value;
1582 else if (strcmp (name, "auth_timeout") == 0)
1584 must_be_positive = TRUE;
1586 parser->limits.auth_timeout = value;
1588 else if (strcmp (name, "reply_timeout") == 0)
1590 must_be_positive = TRUE;
1592 parser->limits.reply_timeout = value;
1594 else if (strcmp (name, "max_completed_connections") == 0)
1596 must_be_positive = TRUE;
1598 parser->limits.max_completed_connections = value;
1600 else if (strcmp (name, "max_incomplete_connections") == 0)
1602 must_be_positive = TRUE;
1604 parser->limits.max_incomplete_connections = value;
1606 else if (strcmp (name, "max_connections_per_user") == 0)
1608 must_be_positive = TRUE;
1610 parser->limits.max_connections_per_user = value;
1612 else if (strcmp (name, "max_pending_activations") == 0)
1614 must_be_positive = TRUE;
1616 parser->limits.max_pending_activations = value;
1618 else if (strcmp (name, "max_services_per_connection") == 0)
1620 must_be_positive = TRUE;
1622 parser->limits.max_services_per_connection = value;
1624 else if (strcmp (name, "max_replies_per_connection") == 0)
1626 must_be_positive = TRUE;
1628 parser->limits.max_replies_per_connection = value;
1632 dbus_set_error (error, DBUS_ERROR_FAILED,
1633 "There is no limit called \"%s\"\n",
1638 if (must_be_positive && value < 0)
1640 dbus_set_error (error, DBUS_ERROR_FAILED,
1641 "<limit name=\"%s\"> must be a positive number\n",
1647 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1649 dbus_set_error (error, DBUS_ERROR_FAILED,
1650 "<limit name=\"%s\"> value is too large\n",
1659 bus_config_parser_end_element (BusConfigParser *parser,
1660 const char *element_name,
1667 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1669 /* printf ("END: %s\n", element_name); */
1671 t = top_element_type (parser);
1673 if (t == ELEMENT_NONE)
1675 /* should probably be an assertion failure but
1676 * being paranoid about XML parsers
1678 dbus_set_error (error, DBUS_ERROR_FAILED,
1679 "XML parser ended element with no element on the stack");
1683 n = element_type_to_name (t);
1684 _dbus_assert (n != NULL);
1685 if (strcmp (n, element_name) != 0)
1687 /* should probably be an assertion failure but
1688 * being paranoid about XML parsers
1690 dbus_set_error (error, DBUS_ERROR_FAILED,
1691 "XML element <%s> ended but topmost element on the stack was <%s>",
1696 e = peek_element (parser);
1697 _dbus_assert (e != NULL);
1702 _dbus_assert_not_reached ("element in stack has no type");
1705 case ELEMENT_INCLUDE:
1708 case ELEMENT_LISTEN:
1709 case ELEMENT_PIDFILE:
1711 case ELEMENT_SERVICEDIR:
1712 case ELEMENT_INCLUDEDIR:
1714 if (!e->had_content)
1716 dbus_set_error (error, DBUS_ERROR_FAILED,
1717 "XML element <%s> was expected to have content inside it",
1718 element_type_to_name (e->type));
1722 if (e->type == ELEMENT_LIMIT)
1724 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1730 case ELEMENT_BUSCONFIG:
1731 case ELEMENT_POLICY:
1735 case ELEMENT_SELINUX:
1736 case ELEMENT_ASSOCIATE:
1740 pop_element (parser);
1746 all_whitespace (const DBusString *str)
1750 _dbus_string_skip_white (str, 0, &i);
1752 return i == _dbus_string_get_length (str);
1756 make_full_path (const DBusString *basedir,
1757 const DBusString *filename,
1758 DBusString *full_path)
1760 if (_dbus_path_is_absolute (filename))
1762 return _dbus_string_copy (filename, 0, full_path, 0);
1766 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1769 if (!_dbus_concat_dir_and_file (full_path, filename))
1777 include_file (BusConfigParser *parser,
1778 const DBusString *filename,
1779 dbus_bool_t ignore_missing,
1782 /* FIXME good test case for this would load each config file in the
1783 * test suite both alone, and as an include, and check
1784 * that the result is the same
1786 BusConfigParser *included;
1787 const char *filename_str;
1788 DBusError tmp_error;
1790 dbus_error_init (&tmp_error);
1792 filename_str = _dbus_string_get_const_data (filename);
1794 /* Check to make sure this file hasn't already been included. */
1795 if (seen_include (parser, filename))
1797 dbus_set_error (error, DBUS_ERROR_FAILED,
1798 "Circular inclusion of file '%s'",
1803 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
1805 BUS_SET_OOM (error);
1809 /* Since parser is passed in as the parent, included
1810 inherits parser's limits. */
1811 included = bus_config_load (filename, FALSE, parser, &tmp_error);
1813 _dbus_list_pop_last (&parser->included_files);
1815 if (included == NULL)
1817 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1819 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1822 dbus_error_free (&tmp_error);
1827 dbus_move_error (&tmp_error, error);
1833 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1835 if (!merge_included (parser, included, error))
1837 bus_config_parser_unref (included);
1841 /* Copy included's limits back to parser. */
1842 parser->limits = included->limits;
1844 bus_config_parser_unref (included);
1850 include_dir (BusConfigParser *parser,
1851 const DBusString *dirname,
1854 DBusString filename;
1856 DBusError tmp_error;
1859 if (!_dbus_string_init (&filename))
1861 BUS_SET_OOM (error);
1867 dir = _dbus_directory_open (dirname, error);
1872 dbus_error_init (&tmp_error);
1873 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1875 DBusString full_path;
1877 if (!_dbus_string_init (&full_path))
1879 BUS_SET_OOM (error);
1883 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1885 BUS_SET_OOM (error);
1886 _dbus_string_free (&full_path);
1890 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1892 BUS_SET_OOM (error);
1893 _dbus_string_free (&full_path);
1897 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1899 if (!include_file (parser, &full_path, TRUE, error))
1901 _dbus_string_free (&full_path);
1906 _dbus_string_free (&full_path);
1909 if (dbus_error_is_set (&tmp_error))
1911 dbus_move_error (&tmp_error, error);
1918 _dbus_string_free (&filename);
1921 _dbus_directory_close (dir);
1927 bus_config_parser_content (BusConfigParser *parser,
1928 const DBusString *content,
1933 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1939 _dbus_string_get_const_data (content, &c_str);
1941 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1945 e = peek_element (parser);
1948 dbus_set_error (error, DBUS_ERROR_FAILED,
1949 "Text content outside of any XML element in configuration file");
1952 else if (e->had_content)
1954 _dbus_assert_not_reached ("Element had multiple content blocks");
1958 switch (top_element_type (parser))
1961 _dbus_assert_not_reached ("element at top of stack has no type");
1964 case ELEMENT_BUSCONFIG:
1965 case ELEMENT_POLICY:
1969 case ELEMENT_SELINUX:
1970 case ELEMENT_ASSOCIATE:
1971 if (all_whitespace (content))
1975 dbus_set_error (error, DBUS_ERROR_FAILED,
1976 "No text content expected inside XML element %s in configuration file",
1977 element_type_to_name (top_element_type (parser)));
1981 case ELEMENT_PIDFILE:
1985 e->had_content = TRUE;
1987 if (!_dbus_string_copy_data (content, &s))
1990 dbus_free (parser->pidfile);
1991 parser->pidfile = s;
1995 case ELEMENT_INCLUDE:
1997 DBusString full_path;
1999 e->had_content = TRUE;
2001 if (!_dbus_string_init (&full_path))
2004 if (!make_full_path (&parser->basedir, content, &full_path))
2006 _dbus_string_free (&full_path);
2010 if (!include_file (parser, &full_path,
2011 e->d.include.ignore_missing, error))
2013 _dbus_string_free (&full_path);
2017 _dbus_string_free (&full_path);
2021 case ELEMENT_INCLUDEDIR:
2023 DBusString full_path;
2025 e->had_content = TRUE;
2027 if (!_dbus_string_init (&full_path))
2030 if (!make_full_path (&parser->basedir, content, &full_path))
2032 _dbus_string_free (&full_path);
2036 if (!include_dir (parser, &full_path, error))
2038 _dbus_string_free (&full_path);
2042 _dbus_string_free (&full_path);
2050 e->had_content = TRUE;
2052 if (!_dbus_string_copy_data (content, &s))
2055 dbus_free (parser->user);
2064 e->had_content = TRUE;
2066 if (!_dbus_string_copy_data (content, &s))
2069 dbus_free (parser->bus_type);
2070 parser->bus_type = s;
2074 case ELEMENT_LISTEN:
2078 e->had_content = TRUE;
2080 if (!_dbus_string_copy_data (content, &s))
2083 if (!_dbus_list_append (&parser->listen_on,
2096 e->had_content = TRUE;
2098 if (!_dbus_string_copy_data (content, &s))
2101 if (!_dbus_list_append (&parser->mechanisms,
2110 case ELEMENT_SERVICEDIR:
2113 DBusString full_path;
2115 e->had_content = TRUE;
2117 if (!_dbus_string_init (&full_path))
2120 if (!make_full_path (&parser->basedir, content, &full_path))
2122 _dbus_string_free (&full_path);
2126 if (!_dbus_string_copy_data (&full_path, &s))
2128 _dbus_string_free (&full_path);
2132 if (!_dbus_list_append (&parser->service_dirs, s))
2134 _dbus_string_free (&full_path);
2139 _dbus_string_free (&full_path);
2147 e->had_content = TRUE;
2150 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2152 dbus_set_error (error, DBUS_ERROR_FAILED,
2153 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2158 e->d.limit.value = val;
2160 _dbus_verbose ("Loaded value %ld for limit %s\n",
2167 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2171 BUS_SET_OOM (error);
2176 bus_config_parser_finished (BusConfigParser *parser,
2179 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2181 if (parser->stack != NULL)
2183 dbus_set_error (error, DBUS_ERROR_FAILED,
2184 "Element <%s> was not closed in configuration file",
2185 element_type_to_name (top_element_type (parser)));
2190 if (parser->is_toplevel && parser->listen_on == NULL)
2192 dbus_set_error (error, DBUS_ERROR_FAILED,
2193 "Configuration file needs one or more <listen> elements giving addresses");
2201 bus_config_parser_get_user (BusConfigParser *parser)
2203 return parser->user;
2207 bus_config_parser_get_type (BusConfigParser *parser)
2209 return parser->bus_type;
2213 bus_config_parser_get_addresses (BusConfigParser *parser)
2215 return &parser->listen_on;
2219 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2221 return &parser->mechanisms;
2225 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2227 return &parser->service_dirs;
2231 bus_config_parser_get_fork (BusConfigParser *parser)
2233 return parser->fork;
2237 bus_config_parser_get_pidfile (BusConfigParser *parser)
2239 return parser->pidfile;
2243 bus_config_parser_steal_policy (BusConfigParser *parser)
2247 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2249 policy = parser->policy;
2251 parser->policy = NULL;
2256 /* Overwrite any limits that were set in the configuration file */
2258 bus_config_parser_get_limits (BusConfigParser *parser,
2261 *limits = parser->limits;
2265 bus_config_parser_steal_service_sid_table (BusConfigParser *parser)
2267 DBusHashTable *table;
2269 _dbus_assert (parser->service_sid_table != NULL); /* can only steal once */
2271 table = parser->service_sid_table;
2273 parser->service_sid_table = NULL;
2278 #ifdef DBUS_BUILD_TESTS
2289 do_load (const DBusString *full_path,
2291 dbus_bool_t oom_possible)
2293 BusConfigParser *parser;
2296 dbus_error_init (&error);
2298 parser = bus_config_load (full_path, TRUE, NULL, &error);
2301 _DBUS_ASSERT_ERROR_IS_SET (&error);
2304 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2306 _dbus_verbose ("Failed to load valid file due to OOM\n");
2307 dbus_error_free (&error);
2310 else if (validity == VALID)
2312 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2315 dbus_error_free (&error);
2320 dbus_error_free (&error);
2326 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2328 bus_config_parser_unref (parser);
2330 if (validity == INVALID)
2332 _dbus_warn ("Accepted invalid file\n");
2342 const DBusString *full_path;
2347 check_loader_oom_func (void *data)
2349 LoaderOomData *d = data;
2351 return do_load (d->full_path, d->validity, TRUE);
2355 process_test_valid_subdir (const DBusString *test_base_dir,
2359 DBusString test_directory;
2360 DBusString filename;
2368 if (!_dbus_string_init (&test_directory))
2369 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2371 _dbus_string_init_const (&filename, subdir);
2373 if (!_dbus_string_copy (test_base_dir, 0,
2374 &test_directory, 0))
2375 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2377 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2378 _dbus_assert_not_reached ("couldn't allocate full path");
2380 _dbus_string_free (&filename);
2381 if (!_dbus_string_init (&filename))
2382 _dbus_assert_not_reached ("didn't allocate filename string\n");
2384 dbus_error_init (&error);
2385 dir = _dbus_directory_open (&test_directory, &error);
2388 _dbus_warn ("Could not open %s: %s\n",
2389 _dbus_string_get_const_data (&test_directory),
2391 dbus_error_free (&error);
2395 if (validity == VALID)
2396 printf ("Testing valid files:\n");
2397 else if (validity == INVALID)
2398 printf ("Testing invalid files:\n");
2400 printf ("Testing unknown files:\n");
2403 while (_dbus_directory_get_next_file (dir, &filename, &error))
2405 DBusString full_path;
2408 if (!_dbus_string_init (&full_path))
2409 _dbus_assert_not_reached ("couldn't init string");
2411 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2412 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2414 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2415 _dbus_assert_not_reached ("couldn't concat file to dir");
2417 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2419 _dbus_verbose ("Skipping non-.conf file %s\n",
2420 _dbus_string_get_const_data (&filename));
2421 _dbus_string_free (&full_path);
2425 printf (" %s\n", _dbus_string_get_const_data (&filename));
2427 _dbus_verbose (" expecting %s\n",
2428 validity == VALID ? "valid" :
2429 (validity == INVALID ? "invalid" :
2430 (validity == UNKNOWN ? "unknown" : "???")));
2432 d.full_path = &full_path;
2433 d.validity = validity;
2435 /* FIXME hackaround for an expat problem, see
2436 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2437 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2439 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2440 if (!check_loader_oom_func (&d))
2441 _dbus_assert_not_reached ("test failed");
2443 _dbus_string_free (&full_path);
2446 if (dbus_error_is_set (&error))
2448 _dbus_warn ("Could not get next file in %s: %s\n",
2449 _dbus_string_get_const_data (&test_directory),
2451 dbus_error_free (&error);
2460 _dbus_directory_close (dir);
2461 _dbus_string_free (&test_directory);
2462 _dbus_string_free (&filename);
2468 bools_equal (dbus_bool_t a,
2475 strings_equal_or_both_null (const char *a,
2478 if (a == NULL || b == NULL)
2481 return !strcmp (a, b);
2485 elements_equal (const Element *a,
2488 if (a->type != b->type)
2491 if (!bools_equal (a->had_content, b->had_content))
2497 case ELEMENT_INCLUDE:
2498 if (!bools_equal (a->d.include.ignore_missing,
2499 b->d.include.ignore_missing))
2503 case ELEMENT_POLICY:
2504 if (a->d.policy.type != b->d.policy.type)
2506 if (a->d.policy.gid_or_uid != b->d.policy.gid_or_uid)
2511 if (strcmp (a->d.limit.name, b->d.limit.name))
2513 if (a->d.limit.value != b->d.limit.value)
2527 lists_of_elements_equal (DBusList *a,
2536 while (ia != NULL && ib != NULL)
2538 if (elements_equal (ia->data, ib->data))
2540 ia = _dbus_list_get_next_link (&a, ia);
2541 ib = _dbus_list_get_next_link (&b, ib);
2544 return ia == NULL && ib == NULL;
2548 lists_of_c_strings_equal (DBusList *a,
2557 while (ia != NULL && ib != NULL)
2559 if (strcmp (ia->data, ib->data))
2561 ia = _dbus_list_get_next_link (&a, ia);
2562 ib = _dbus_list_get_next_link (&b, ib);
2565 return ia == NULL && ib == NULL;
2569 limits_equal (const BusLimits *a,
2573 (a->max_incoming_bytes == b->max_incoming_bytes
2574 || a->max_outgoing_bytes == b->max_outgoing_bytes
2575 || a->max_message_size == b->max_message_size
2576 || a->activation_timeout == b->activation_timeout
2577 || a->auth_timeout == b->auth_timeout
2578 || a->max_completed_connections == b->max_completed_connections
2579 || a->max_incomplete_connections == b->max_incomplete_connections
2580 || a->max_connections_per_user == b->max_connections_per_user
2581 || a->max_pending_activations == b->max_pending_activations
2582 || a->max_services_per_connection == b->max_services_per_connection
2583 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2584 || a->max_replies_per_connection == b->max_replies_per_connection
2585 || a->reply_timeout == b->reply_timeout);
2589 config_parsers_equal (const BusConfigParser *a,
2590 const BusConfigParser *b)
2592 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2595 if (!lists_of_elements_equal (a->stack, b->stack))
2598 if (!strings_equal_or_both_null (a->user, b->user))
2601 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2604 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2607 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2610 /* FIXME: compare policy */
2612 /* FIXME: compare service selinux ID table */
2614 if (! limits_equal (&a->limits, &b->limits))
2617 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2620 if (! bools_equal (a->fork, b->fork))
2623 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2630 all_are_equiv (const DBusString *target_directory)
2632 DBusString filename;
2634 BusConfigParser *first_parser;
2635 BusConfigParser *parser;
2641 first_parser = NULL;
2645 if (!_dbus_string_init (&filename))
2646 _dbus_assert_not_reached ("didn't allocate filename string");
2648 dbus_error_init (&error);
2649 dir = _dbus_directory_open (target_directory, &error);
2652 _dbus_warn ("Could not open %s: %s\n",
2653 _dbus_string_get_const_data (target_directory),
2655 dbus_error_free (&error);
2659 printf ("Comparing equivalent files:\n");
2662 while (_dbus_directory_get_next_file (dir, &filename, &error))
2664 DBusString full_path;
2666 if (!_dbus_string_init (&full_path))
2667 _dbus_assert_not_reached ("couldn't init string");
2669 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2670 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2672 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2673 _dbus_assert_not_reached ("couldn't concat file to dir");
2675 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2677 _dbus_verbose ("Skipping non-.conf file %s\n",
2678 _dbus_string_get_const_data (&filename));
2679 _dbus_string_free (&full_path);
2683 printf (" %s\n", _dbus_string_get_const_data (&filename));
2685 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2686 _dbus_string_free (&full_path);
2690 _dbus_warn ("Could not load file %s: %s\n",
2691 _dbus_string_get_const_data (&full_path),
2693 dbus_error_free (&error);
2696 else if (first_parser == NULL)
2698 first_parser = parser;
2702 equal = config_parsers_equal (first_parser, parser);
2703 bus_config_parser_unref (parser);
2713 _dbus_string_free (&filename);
2715 bus_config_parser_unref (first_parser);
2717 _dbus_directory_close (dir);
2724 process_test_equiv_subdir (const DBusString *test_base_dir,
2727 DBusString test_directory;
2728 DBusString filename;
2737 if (!_dbus_string_init (&test_directory))
2738 _dbus_assert_not_reached ("didn't allocate test_directory");
2740 _dbus_string_init_const (&filename, subdir);
2742 if (!_dbus_string_copy (test_base_dir, 0,
2743 &test_directory, 0))
2744 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2746 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2747 _dbus_assert_not_reached ("couldn't allocate full path");
2749 _dbus_string_free (&filename);
2750 if (!_dbus_string_init (&filename))
2751 _dbus_assert_not_reached ("didn't allocate filename string");
2753 dbus_error_init (&error);
2754 dir = _dbus_directory_open (&test_directory, &error);
2757 _dbus_warn ("Could not open %s: %s\n",
2758 _dbus_string_get_const_data (&test_directory),
2760 dbus_error_free (&error);
2764 while (_dbus_directory_get_next_file (dir, &filename, &error))
2766 DBusString full_path;
2768 /* Skip CVS's magic directories! */
2769 if (_dbus_string_equal_c_str (&filename, "CVS"))
2772 if (!_dbus_string_init (&full_path))
2773 _dbus_assert_not_reached ("couldn't init string");
2775 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2776 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2778 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2779 _dbus_assert_not_reached ("couldn't concat file to dir");
2781 equal = all_are_equiv (&full_path);
2782 _dbus_string_free (&full_path);
2791 _dbus_string_free (&test_directory);
2792 _dbus_string_free (&filename);
2794 _dbus_directory_close (dir);
2801 bus_config_parser_test (const DBusString *test_data_dir)
2803 if (test_data_dir == NULL ||
2804 _dbus_string_get_length (test_data_dir) == 0)
2806 printf ("No test data\n");
2810 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
2813 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
2816 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
2822 #endif /* DBUS_BUILD_TESTS */