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.0
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);
349 if (parser->service_sid_table == NULL)
350 _dbus_hash_table_unref (parser->service_sid_table);
358 /* Initialize the parser's limits from the parent. */
359 parser->limits = parent->limits;
361 /* Use the parent's list of included_files to avoid
362 circular inclusions. */
363 parser->included_files = parent->included_files;
368 /* Make up some numbers! woot! */
369 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
370 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
371 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
373 /* Making this long means the user has to wait longer for an error
374 * message if something screws up, but making it too short means
375 * they might see a false failure.
377 parser->limits.activation_timeout = 25000; /* 25 seconds */
379 /* Making this long risks making a DOS attack easier, but too short
380 * and legitimate auth will fail. If interactive auth (ask user for
381 * password) is allowed, then potentially it has to be quite long.
383 parser->limits.auth_timeout = 30000; /* 30 seconds */
385 parser->limits.max_incomplete_connections = 32;
386 parser->limits.max_connections_per_user = 128;
388 /* Note that max_completed_connections / max_connections_per_user
389 * is the number of users that would have to work together to
390 * DOS all the other users.
392 parser->limits.max_completed_connections = 1024;
394 parser->limits.max_pending_activations = 256;
395 parser->limits.max_services_per_connection = 256;
397 parser->limits.max_match_rules_per_connection = 128;
399 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
400 parser->limits.max_replies_per_connection = 32;
403 parser->refcount = 1;
409 bus_config_parser_ref (BusConfigParser *parser)
411 _dbus_assert (parser->refcount > 0);
413 parser->refcount += 1;
419 bus_config_parser_unref (BusConfigParser *parser)
421 _dbus_assert (parser->refcount > 0);
423 parser->refcount -= 1;
425 if (parser->refcount == 0)
427 while (parser->stack != NULL)
428 pop_element (parser);
430 dbus_free (parser->user);
431 dbus_free (parser->bus_type);
432 dbus_free (parser->pidfile);
434 _dbus_list_foreach (&parser->listen_on,
435 (DBusForeachFunction) dbus_free,
438 _dbus_list_clear (&parser->listen_on);
440 _dbus_list_foreach (&parser->service_dirs,
441 (DBusForeachFunction) dbus_free,
444 _dbus_list_clear (&parser->service_dirs);
446 _dbus_list_foreach (&parser->mechanisms,
447 (DBusForeachFunction) dbus_free,
450 _dbus_list_clear (&parser->mechanisms);
452 _dbus_string_free (&parser->basedir);
455 bus_policy_unref (parser->policy);
457 if (parser->service_sid_table)
458 _dbus_hash_table_unref (parser->service_sid_table);
465 bus_config_parser_check_doctype (BusConfigParser *parser,
469 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
471 if (strcmp (doctype, "busconfig") != 0)
473 dbus_set_error (error,
475 "Configuration file has the wrong document type %s",
490 locate_attributes (BusConfigParser *parser,
491 const char *element_name,
492 const char **attribute_names,
493 const char **attribute_values,
495 const char *first_attribute_name,
496 const char **first_attribute_retloc,
504 LocateAttr attrs[MAX_ATTRS];
508 _dbus_assert (first_attribute_name != NULL);
509 _dbus_assert (first_attribute_retloc != NULL);
514 attrs[0].name = first_attribute_name;
515 attrs[0].retloc = first_attribute_retloc;
516 *first_attribute_retloc = NULL;
518 va_start (args, first_attribute_retloc);
520 name = va_arg (args, const char*);
521 retloc = va_arg (args, const char**);
525 _dbus_assert (retloc != NULL);
526 _dbus_assert (n_attrs < MAX_ATTRS);
528 attrs[n_attrs].name = name;
529 attrs[n_attrs].retloc = retloc;
533 name = va_arg (args, const char*);
534 retloc = va_arg (args, const char**);
543 while (attribute_names[i])
552 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
554 retloc = attrs[j].retloc;
558 dbus_set_error (error, DBUS_ERROR_FAILED,
559 "Attribute \"%s\" repeated twice on the same <%s> element",
560 attrs[j].name, element_name);
565 *retloc = attribute_values[i];
574 dbus_set_error (error, DBUS_ERROR_FAILED,
575 "Attribute \"%s\" is invalid on <%s> element in this context",
576 attribute_names[i], element_name);
589 check_no_attributes (BusConfigParser *parser,
590 const char *element_name,
591 const char **attribute_names,
592 const char **attribute_values,
595 if (attribute_names[0] != NULL)
597 dbus_set_error (error, DBUS_ERROR_FAILED,
598 "Attribute \"%s\" is invalid on <%s> element in this context",
599 attribute_names[0], element_name);
607 start_busconfig_child (BusConfigParser *parser,
608 const char *element_name,
609 const char **attribute_names,
610 const char **attribute_values,
613 if (strcmp (element_name, "user") == 0)
615 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
618 if (push_element (parser, ELEMENT_USER) == NULL)
626 else if (strcmp (element_name, "type") == 0)
628 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
631 if (push_element (parser, ELEMENT_TYPE) == NULL)
639 else if (strcmp (element_name, "fork") == 0)
641 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
644 if (push_element (parser, ELEMENT_FORK) == NULL)
654 else if (strcmp (element_name, "pidfile") == 0)
656 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
659 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
667 else if (strcmp (element_name, "listen") == 0)
669 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
672 if (push_element (parser, ELEMENT_LISTEN) == NULL)
680 else if (strcmp (element_name, "auth") == 0)
682 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
685 if (push_element (parser, ELEMENT_AUTH) == NULL)
693 else if (strcmp (element_name, "includedir") == 0)
695 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
698 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
706 else if (strcmp (element_name, "servicedir") == 0)
708 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
711 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
719 else if (strcmp (element_name, "include") == 0)
722 const char *ignore_missing;
724 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
730 e->d.include.ignore_missing = FALSE;
732 if (!locate_attributes (parser, "include",
736 "ignore_missing", &ignore_missing,
740 if (ignore_missing != NULL)
742 if (strcmp (ignore_missing, "yes") == 0)
743 e->d.include.ignore_missing = TRUE;
744 else if (strcmp (ignore_missing, "no") == 0)
745 e->d.include.ignore_missing = FALSE;
748 dbus_set_error (error, DBUS_ERROR_FAILED,
749 "ignore_missing attribute must have value \"yes\" or \"no\"");
756 else if (strcmp (element_name, "policy") == 0)
763 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
769 e->d.policy.type = POLICY_IGNORED;
771 if (!locate_attributes (parser, "policy",
781 if (((context && user) ||
782 (context && group)) ||
784 !(context || user || group))
786 dbus_set_error (error, DBUS_ERROR_FAILED,
787 "<policy> element must have exactly one of (context|user|group) attributes");
793 if (strcmp (context, "default") == 0)
795 e->d.policy.type = POLICY_DEFAULT;
797 else if (strcmp (context, "mandatory") == 0)
799 e->d.policy.type = POLICY_MANDATORY;
803 dbus_set_error (error, DBUS_ERROR_FAILED,
804 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
809 else if (user != NULL)
812 _dbus_string_init_const (&username, user);
814 if (_dbus_get_user_id (&username,
815 &e->d.policy.gid_or_uid))
816 e->d.policy.type = POLICY_USER;
818 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
821 else if (group != NULL)
823 DBusString group_name;
824 _dbus_string_init_const (&group_name, group);
826 if (_dbus_get_group_id (&group_name,
827 &e->d.policy.gid_or_uid))
828 e->d.policy.type = POLICY_GROUP;
830 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
835 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
840 else if (strcmp (element_name, "limit") == 0)
845 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
851 if (!locate_attributes (parser, "limit",
861 dbus_set_error (error, DBUS_ERROR_FAILED,
862 "<limit> element must have a \"name\" attribute");
866 e->d.limit.name = _dbus_strdup (name);
867 if (e->d.limit.name == NULL)
875 else if (strcmp (element_name, "selinux") == 0)
880 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
883 if (push_element (parser, ELEMENT_SELINUX) == NULL)
893 dbus_set_error (error, DBUS_ERROR_FAILED,
894 "Element <%s> not allowed inside <%s> in configuration file",
895 element_name, "busconfig");
901 append_rule_from_element (BusConfigParser *parser,
902 const char *element_name,
903 const char **attribute_names,
904 const char **attribute_values,
908 const char *send_interface;
909 const char *send_member;
910 const char *send_error;
911 const char *send_destination;
912 const char *send_path;
913 const char *send_type;
914 const char *receive_interface;
915 const char *receive_member;
916 const char *receive_error;
917 const char *receive_sender;
918 const char *receive_path;
919 const char *receive_type;
920 const char *eavesdrop;
921 const char *send_requested_reply;
922 const char *receive_requested_reply;
928 if (!locate_attributes (parser, element_name,
932 "send_interface", &send_interface,
933 "send_member", &send_member,
934 "send_error", &send_error,
935 "send_destination", &send_destination,
936 "send_path", &send_path,
937 "send_type", &send_type,
938 "receive_interface", &receive_interface,
939 "receive_member", &receive_member,
940 "receive_error", &receive_error,
941 "receive_sender", &receive_sender,
942 "receive_path", &receive_path,
943 "receive_type", &receive_type,
944 "eavesdrop", &eavesdrop,
945 "send_requested_reply", &send_requested_reply,
946 "receive_requested_reply", &receive_requested_reply,
953 if (!(send_interface || send_member || send_error || send_destination ||
954 send_type || send_path ||
955 receive_interface || receive_member || receive_error || receive_sender ||
956 receive_type || receive_path || eavesdrop ||
957 send_requested_reply || receive_requested_reply ||
958 own || user || group))
960 dbus_set_error (error, DBUS_ERROR_FAILED,
961 "Element <%s> must have one or more attributes",
966 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
967 (receive_member && (receive_interface == NULL && receive_path == NULL)))
969 dbus_set_error (error, DBUS_ERROR_FAILED,
970 "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.",
975 /* Allowed combinations of elements are:
977 * base, must be all send or all receive:
983 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
984 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
986 * user, group, own must occur alone
988 * Pretty sure the below stuff is broken, FIXME think about it more.
991 if (((send_interface && send_error) ||
992 (send_interface && receive_interface) ||
993 (send_interface && receive_member) ||
994 (send_interface && receive_error) ||
995 (send_interface && receive_sender) ||
996 (send_interface && eavesdrop) ||
997 (send_interface && receive_requested_reply) ||
998 (send_interface && own) ||
999 (send_interface && user) ||
1000 (send_interface && group)) ||
1002 ((send_member && send_error) ||
1003 (send_member && receive_interface) ||
1004 (send_member && receive_member) ||
1005 (send_member && receive_error) ||
1006 (send_member && receive_sender) ||
1007 (send_member && eavesdrop) ||
1008 (send_member && receive_requested_reply) ||
1009 (send_member && own) ||
1010 (send_member && user) ||
1011 (send_member && group)) ||
1013 ((send_error && receive_interface) ||
1014 (send_error && receive_member) ||
1015 (send_error && receive_error) ||
1016 (send_error && receive_sender) ||
1017 (send_error && eavesdrop) ||
1018 (send_error && receive_requested_reply) ||
1019 (send_error && own) ||
1020 (send_error && user) ||
1021 (send_error && group)) ||
1023 ((send_destination && receive_interface) ||
1024 (send_destination && receive_member) ||
1025 (send_destination && receive_error) ||
1026 (send_destination && receive_sender) ||
1027 (send_destination && eavesdrop) ||
1028 (send_destination && receive_requested_reply) ||
1029 (send_destination && own) ||
1030 (send_destination && user) ||
1031 (send_destination && group)) ||
1033 ((send_type && receive_interface) ||
1034 (send_type && receive_member) ||
1035 (send_type && receive_error) ||
1036 (send_type && receive_sender) ||
1037 (send_type && eavesdrop) ||
1038 (send_type && receive_requested_reply) ||
1039 (send_type && own) ||
1040 (send_type && user) ||
1041 (send_type && group)) ||
1043 ((send_path && receive_interface) ||
1044 (send_path && receive_member) ||
1045 (send_path && receive_error) ||
1046 (send_path && receive_sender) ||
1047 (send_path && eavesdrop) ||
1048 (send_path && receive_requested_reply) ||
1049 (send_path && own) ||
1050 (send_path && user) ||
1051 (send_path && group)) ||
1053 ((send_requested_reply && receive_interface) ||
1054 (send_requested_reply && receive_member) ||
1055 (send_requested_reply && receive_error) ||
1056 (send_requested_reply && receive_sender) ||
1057 (send_requested_reply && eavesdrop) ||
1058 (send_requested_reply && receive_requested_reply) ||
1059 (send_requested_reply && own) ||
1060 (send_requested_reply && user) ||
1061 (send_requested_reply && group)) ||
1063 ((receive_interface && receive_error) ||
1064 (receive_interface && own) ||
1065 (receive_interface && user) ||
1066 (receive_interface && group)) ||
1068 ((receive_member && receive_error) ||
1069 (receive_member && own) ||
1070 (receive_member && user) ||
1071 (receive_member && group)) ||
1073 ((receive_error && own) ||
1074 (receive_error && user) ||
1075 (receive_error && group)) ||
1077 ((eavesdrop && own) ||
1078 (eavesdrop && user) ||
1079 (eavesdrop && group)) ||
1081 ((receive_requested_reply && own) ||
1082 (receive_requested_reply && user) ||
1083 (receive_requested_reply && group)) ||
1090 dbus_set_error (error, DBUS_ERROR_FAILED,
1091 "Invalid combination of attributes on element <%s>",
1098 /* In BusPolicyRule, NULL represents wildcard.
1099 * In the config file, '*' represents it.
1101 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1103 if (send_interface || send_member || send_error || send_destination ||
1104 send_path || send_type || send_requested_reply)
1108 if (IS_WILDCARD (send_interface))
1109 send_interface = NULL;
1110 if (IS_WILDCARD (send_member))
1112 if (IS_WILDCARD (send_error))
1114 if (IS_WILDCARD (send_destination))
1115 send_destination = NULL;
1116 if (IS_WILDCARD (send_path))
1118 if (IS_WILDCARD (send_type))
1121 message_type = DBUS_MESSAGE_TYPE_INVALID;
1122 if (send_type != NULL)
1124 message_type = dbus_message_type_from_string (send_type);
1125 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1127 dbus_set_error (error, DBUS_ERROR_FAILED,
1128 "Bad message type \"%s\"",
1134 if (send_requested_reply &&
1135 !(strcmp (send_requested_reply, "true") == 0 ||
1136 strcmp (send_requested_reply, "false") == 0))
1138 dbus_set_error (error, DBUS_ERROR_FAILED,
1139 "Bad value \"%s\" for %s attribute, must be true or false",
1140 "send_requested_reply", send_requested_reply);
1144 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1148 if (send_requested_reply)
1149 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1151 rule->d.send.message_type = message_type;
1152 rule->d.send.path = _dbus_strdup (send_path);
1153 rule->d.send.interface = _dbus_strdup (send_interface);
1154 rule->d.send.member = _dbus_strdup (send_member);
1155 rule->d.send.error = _dbus_strdup (send_error);
1156 rule->d.send.destination = _dbus_strdup (send_destination);
1157 if (send_path && rule->d.send.path == NULL)
1159 if (send_interface && rule->d.send.interface == NULL)
1161 if (send_member && rule->d.send.member == NULL)
1163 if (send_error && rule->d.send.error == NULL)
1165 if (send_destination && rule->d.send.destination == NULL)
1168 else if (receive_interface || receive_member || receive_error || receive_sender ||
1169 receive_path || receive_type || eavesdrop || receive_requested_reply)
1173 if (IS_WILDCARD (receive_interface))
1174 receive_interface = NULL;
1175 if (IS_WILDCARD (receive_member))
1176 receive_member = NULL;
1177 if (IS_WILDCARD (receive_error))
1178 receive_error = NULL;
1179 if (IS_WILDCARD (receive_sender))
1180 receive_sender = NULL;
1181 if (IS_WILDCARD (receive_path))
1182 receive_path = NULL;
1183 if (IS_WILDCARD (receive_type))
1184 receive_type = NULL;
1186 message_type = DBUS_MESSAGE_TYPE_INVALID;
1187 if (receive_type != NULL)
1189 message_type = dbus_message_type_from_string (receive_type);
1190 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1192 dbus_set_error (error, DBUS_ERROR_FAILED,
1193 "Bad message type \"%s\"",
1201 !(strcmp (eavesdrop, "true") == 0 ||
1202 strcmp (eavesdrop, "false") == 0))
1204 dbus_set_error (error, DBUS_ERROR_FAILED,
1205 "Bad value \"%s\" for %s attribute, must be true or false",
1206 "eavesdrop", eavesdrop);
1210 if (receive_requested_reply &&
1211 !(strcmp (receive_requested_reply, "true") == 0 ||
1212 strcmp (receive_requested_reply, "false") == 0))
1214 dbus_set_error (error, DBUS_ERROR_FAILED,
1215 "Bad value \"%s\" for %s attribute, must be true or false",
1216 "receive_requested_reply", receive_requested_reply);
1220 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1225 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1227 if (receive_requested_reply)
1228 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1230 rule->d.receive.message_type = message_type;
1231 rule->d.receive.path = _dbus_strdup (receive_path);
1232 rule->d.receive.interface = _dbus_strdup (receive_interface);
1233 rule->d.receive.member = _dbus_strdup (receive_member);
1234 rule->d.receive.error = _dbus_strdup (receive_error);
1235 rule->d.receive.origin = _dbus_strdup (receive_sender);
1237 if (receive_path && rule->d.receive.path == NULL)
1239 if (receive_interface && rule->d.receive.interface == NULL)
1241 if (receive_member && rule->d.receive.member == NULL)
1243 if (receive_error && rule->d.receive.error == NULL)
1245 if (receive_sender && rule->d.receive.origin == NULL)
1250 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1254 if (IS_WILDCARD (own))
1257 rule->d.own.service_name = _dbus_strdup (own);
1258 if (own && rule->d.own.service_name == NULL)
1263 if (IS_WILDCARD (user))
1265 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1269 rule->d.user.uid = DBUS_UID_UNSET;
1273 DBusString username;
1276 _dbus_string_init_const (&username, user);
1278 if (_dbus_get_user_id (&username, &uid))
1280 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1284 rule->d.user.uid = uid;
1288 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1289 user, element_name);
1295 if (IS_WILDCARD (group))
1297 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1301 rule->d.group.gid = DBUS_GID_UNSET;
1305 DBusString groupname;
1308 _dbus_string_init_const (&groupname, group);
1310 if (_dbus_get_user_id (&groupname, &gid))
1312 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1316 rule->d.group.gid = gid;
1320 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1321 group, element_name);
1326 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1332 pe = peek_element (parser);
1333 _dbus_assert (pe != NULL);
1334 _dbus_assert (pe->type == ELEMENT_POLICY);
1336 switch (pe->d.policy.type)
1338 case POLICY_IGNORED:
1339 /* drop the rule on the floor */
1342 case POLICY_DEFAULT:
1343 if (!bus_policy_append_default_rule (parser->policy, rule))
1346 case POLICY_MANDATORY:
1347 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1351 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1353 dbus_set_error (error, DBUS_ERROR_FAILED,
1354 "<%s> rule cannot be per-user because it has bus-global semantics",
1359 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
1364 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1366 dbus_set_error (error, DBUS_ERROR_FAILED,
1367 "<%s> rule cannot be per-group because it has bus-global semantics",
1372 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
1378 bus_policy_rule_unref (rule);
1385 BUS_SET_OOM (error);
1388 bus_policy_rule_unref (rule);
1393 start_policy_child (BusConfigParser *parser,
1394 const char *element_name,
1395 const char **attribute_names,
1396 const char **attribute_values,
1399 if (strcmp (element_name, "allow") == 0)
1401 if (!append_rule_from_element (parser, element_name,
1402 attribute_names, attribute_values,
1406 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1408 BUS_SET_OOM (error);
1414 else if (strcmp (element_name, "deny") == 0)
1416 if (!append_rule_from_element (parser, element_name,
1417 attribute_names, attribute_values,
1421 if (push_element (parser, ELEMENT_DENY) == NULL)
1423 BUS_SET_OOM (error);
1431 dbus_set_error (error, DBUS_ERROR_FAILED,
1432 "Element <%s> not allowed inside <%s> in configuration file",
1433 element_name, "policy");
1439 start_selinux_child (BusConfigParser *parser,
1440 const char *element_name,
1441 const char **attribute_names,
1442 const char **attribute_values,
1445 if (strcmp (element_name, "associate") == 0)
1448 const char *context;
1450 if (!locate_attributes (parser, "associate",
1455 "context", &context,
1459 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1461 BUS_SET_OOM (error);
1465 if (own == NULL || context == NULL)
1467 dbus_set_error (error, DBUS_ERROR_FAILED,
1468 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1472 if (!bus_selinux_id_table_insert (parser->service_sid_table,
1475 BUS_SET_OOM (error);
1483 dbus_set_error (error, DBUS_ERROR_FAILED,
1484 "Element <%s> not allowed inside <%s> in configuration file",
1485 element_name, "selinux");
1491 bus_config_parser_start_element (BusConfigParser *parser,
1492 const char *element_name,
1493 const char **attribute_names,
1494 const char **attribute_values,
1499 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1501 /* printf ("START: %s\n", element_name); */
1503 t = top_element_type (parser);
1505 if (t == ELEMENT_NONE)
1507 if (strcmp (element_name, "busconfig") == 0)
1509 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1512 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1514 BUS_SET_OOM (error);
1522 dbus_set_error (error, DBUS_ERROR_FAILED,
1523 "Unknown element <%s> at root of configuration file",
1528 else if (t == ELEMENT_BUSCONFIG)
1530 return start_busconfig_child (parser, element_name,
1531 attribute_names, attribute_values,
1534 else if (t == ELEMENT_POLICY)
1536 return start_policy_child (parser, element_name,
1537 attribute_names, attribute_values,
1540 else if (t == ELEMENT_SELINUX)
1542 return start_selinux_child (parser, element_name,
1543 attribute_names, attribute_values,
1548 dbus_set_error (error, DBUS_ERROR_FAILED,
1549 "Element <%s> is not allowed in this context",
1556 set_limit (BusConfigParser *parser,
1561 dbus_bool_t must_be_positive;
1562 dbus_bool_t must_be_int;
1564 must_be_int = FALSE;
1565 must_be_positive = FALSE;
1567 if (strcmp (name, "max_incoming_bytes") == 0)
1569 must_be_positive = TRUE;
1570 parser->limits.max_incoming_bytes = value;
1572 else if (strcmp (name, "max_outgoing_bytes") == 0)
1574 must_be_positive = TRUE;
1575 parser->limits.max_outgoing_bytes = value;
1577 else if (strcmp (name, "max_message_size") == 0)
1579 must_be_positive = TRUE;
1580 parser->limits.max_message_size = value;
1582 else if (strcmp (name, "activation_timeout") == 0)
1584 must_be_positive = TRUE;
1586 parser->limits.activation_timeout = value;
1588 else if (strcmp (name, "auth_timeout") == 0)
1590 must_be_positive = TRUE;
1592 parser->limits.auth_timeout = value;
1594 else if (strcmp (name, "reply_timeout") == 0)
1596 must_be_positive = TRUE;
1598 parser->limits.reply_timeout = value;
1600 else if (strcmp (name, "max_completed_connections") == 0)
1602 must_be_positive = TRUE;
1604 parser->limits.max_completed_connections = value;
1606 else if (strcmp (name, "max_incomplete_connections") == 0)
1608 must_be_positive = TRUE;
1610 parser->limits.max_incomplete_connections = value;
1612 else if (strcmp (name, "max_connections_per_user") == 0)
1614 must_be_positive = TRUE;
1616 parser->limits.max_connections_per_user = value;
1618 else if (strcmp (name, "max_pending_activations") == 0)
1620 must_be_positive = TRUE;
1622 parser->limits.max_pending_activations = value;
1624 else if (strcmp (name, "max_services_per_connection") == 0)
1626 must_be_positive = TRUE;
1628 parser->limits.max_services_per_connection = value;
1630 else if (strcmp (name, "max_replies_per_connection") == 0)
1632 must_be_positive = TRUE;
1634 parser->limits.max_replies_per_connection = value;
1638 dbus_set_error (error, DBUS_ERROR_FAILED,
1639 "There is no limit called \"%s\"\n",
1644 if (must_be_positive && value < 0)
1646 dbus_set_error (error, DBUS_ERROR_FAILED,
1647 "<limit name=\"%s\"> must be a positive number\n",
1653 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1655 dbus_set_error (error, DBUS_ERROR_FAILED,
1656 "<limit name=\"%s\"> value is too large\n",
1665 bus_config_parser_end_element (BusConfigParser *parser,
1666 const char *element_name,
1673 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1675 /* printf ("END: %s\n", element_name); */
1677 t = top_element_type (parser);
1679 if (t == ELEMENT_NONE)
1681 /* should probably be an assertion failure but
1682 * being paranoid about XML parsers
1684 dbus_set_error (error, DBUS_ERROR_FAILED,
1685 "XML parser ended element with no element on the stack");
1689 n = element_type_to_name (t);
1690 _dbus_assert (n != NULL);
1691 if (strcmp (n, element_name) != 0)
1693 /* should probably be an assertion failure but
1694 * being paranoid about XML parsers
1696 dbus_set_error (error, DBUS_ERROR_FAILED,
1697 "XML element <%s> ended but topmost element on the stack was <%s>",
1702 e = peek_element (parser);
1703 _dbus_assert (e != NULL);
1708 _dbus_assert_not_reached ("element in stack has no type");
1711 case ELEMENT_INCLUDE:
1714 case ELEMENT_LISTEN:
1715 case ELEMENT_PIDFILE:
1717 case ELEMENT_SERVICEDIR:
1718 case ELEMENT_INCLUDEDIR:
1720 if (!e->had_content)
1722 dbus_set_error (error, DBUS_ERROR_FAILED,
1723 "XML element <%s> was expected to have content inside it",
1724 element_type_to_name (e->type));
1728 if (e->type == ELEMENT_LIMIT)
1730 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1736 case ELEMENT_BUSCONFIG:
1737 case ELEMENT_POLICY:
1741 case ELEMENT_SELINUX:
1742 case ELEMENT_ASSOCIATE:
1746 pop_element (parser);
1752 all_whitespace (const DBusString *str)
1756 _dbus_string_skip_white (str, 0, &i);
1758 return i == _dbus_string_get_length (str);
1762 make_full_path (const DBusString *basedir,
1763 const DBusString *filename,
1764 DBusString *full_path)
1766 if (_dbus_path_is_absolute (filename))
1768 return _dbus_string_copy (filename, 0, full_path, 0);
1772 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1775 if (!_dbus_concat_dir_and_file (full_path, filename))
1783 include_file (BusConfigParser *parser,
1784 const DBusString *filename,
1785 dbus_bool_t ignore_missing,
1788 /* FIXME good test case for this would load each config file in the
1789 * test suite both alone, and as an include, and check
1790 * that the result is the same
1792 BusConfigParser *included;
1793 const char *filename_str;
1794 DBusError tmp_error;
1796 dbus_error_init (&tmp_error);
1798 filename_str = _dbus_string_get_const_data (filename);
1800 /* Check to make sure this file hasn't already been included. */
1801 if (seen_include (parser, filename))
1803 dbus_set_error (error, DBUS_ERROR_FAILED,
1804 "Circular inclusion of file '%s'",
1809 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
1811 BUS_SET_OOM (error);
1815 /* Since parser is passed in as the parent, included
1816 inherits parser's limits. */
1817 included = bus_config_load (filename, FALSE, parser, &tmp_error);
1819 _dbus_list_pop_last (&parser->included_files);
1821 if (included == NULL)
1823 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1825 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1828 dbus_error_free (&tmp_error);
1833 dbus_move_error (&tmp_error, error);
1839 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1841 if (!merge_included (parser, included, error))
1843 bus_config_parser_unref (included);
1847 /* Copy included's limits back to parser. */
1848 parser->limits = included->limits;
1850 bus_config_parser_unref (included);
1856 include_dir (BusConfigParser *parser,
1857 const DBusString *dirname,
1860 DBusString filename;
1862 DBusError tmp_error;
1865 if (!_dbus_string_init (&filename))
1867 BUS_SET_OOM (error);
1873 dir = _dbus_directory_open (dirname, error);
1878 dbus_error_init (&tmp_error);
1879 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1881 DBusString full_path;
1883 if (!_dbus_string_init (&full_path))
1885 BUS_SET_OOM (error);
1889 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1891 BUS_SET_OOM (error);
1892 _dbus_string_free (&full_path);
1896 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1898 BUS_SET_OOM (error);
1899 _dbus_string_free (&full_path);
1903 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1905 if (!include_file (parser, &full_path, TRUE, error))
1907 _dbus_string_free (&full_path);
1912 _dbus_string_free (&full_path);
1915 if (dbus_error_is_set (&tmp_error))
1917 dbus_move_error (&tmp_error, error);
1924 _dbus_string_free (&filename);
1927 _dbus_directory_close (dir);
1933 bus_config_parser_content (BusConfigParser *parser,
1934 const DBusString *content,
1939 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1945 _dbus_string_get_const_data (content, &c_str);
1947 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1951 e = peek_element (parser);
1954 dbus_set_error (error, DBUS_ERROR_FAILED,
1955 "Text content outside of any XML element in configuration file");
1958 else if (e->had_content)
1960 _dbus_assert_not_reached ("Element had multiple content blocks");
1964 switch (top_element_type (parser))
1967 _dbus_assert_not_reached ("element at top of stack has no type");
1970 case ELEMENT_BUSCONFIG:
1971 case ELEMENT_POLICY:
1975 case ELEMENT_SELINUX:
1976 case ELEMENT_ASSOCIATE:
1977 if (all_whitespace (content))
1981 dbus_set_error (error, DBUS_ERROR_FAILED,
1982 "No text content expected inside XML element %s in configuration file",
1983 element_type_to_name (top_element_type (parser)));
1987 case ELEMENT_PIDFILE:
1991 e->had_content = TRUE;
1993 if (!_dbus_string_copy_data (content, &s))
1996 dbus_free (parser->pidfile);
1997 parser->pidfile = s;
2001 case ELEMENT_INCLUDE:
2003 DBusString full_path;
2005 e->had_content = TRUE;
2007 if (!_dbus_string_init (&full_path))
2010 if (!make_full_path (&parser->basedir, content, &full_path))
2012 _dbus_string_free (&full_path);
2016 if (!include_file (parser, &full_path,
2017 e->d.include.ignore_missing, error))
2019 _dbus_string_free (&full_path);
2023 _dbus_string_free (&full_path);
2027 case ELEMENT_INCLUDEDIR:
2029 DBusString full_path;
2031 e->had_content = TRUE;
2033 if (!_dbus_string_init (&full_path))
2036 if (!make_full_path (&parser->basedir, content, &full_path))
2038 _dbus_string_free (&full_path);
2042 if (!include_dir (parser, &full_path, error))
2044 _dbus_string_free (&full_path);
2048 _dbus_string_free (&full_path);
2056 e->had_content = TRUE;
2058 if (!_dbus_string_copy_data (content, &s))
2061 dbus_free (parser->user);
2070 e->had_content = TRUE;
2072 if (!_dbus_string_copy_data (content, &s))
2075 dbus_free (parser->bus_type);
2076 parser->bus_type = s;
2080 case ELEMENT_LISTEN:
2084 e->had_content = TRUE;
2086 if (!_dbus_string_copy_data (content, &s))
2089 if (!_dbus_list_append (&parser->listen_on,
2102 e->had_content = TRUE;
2104 if (!_dbus_string_copy_data (content, &s))
2107 if (!_dbus_list_append (&parser->mechanisms,
2116 case ELEMENT_SERVICEDIR:
2119 DBusString full_path;
2121 e->had_content = TRUE;
2123 if (!_dbus_string_init (&full_path))
2126 if (!make_full_path (&parser->basedir, content, &full_path))
2128 _dbus_string_free (&full_path);
2132 if (!_dbus_string_copy_data (&full_path, &s))
2134 _dbus_string_free (&full_path);
2138 if (!_dbus_list_append (&parser->service_dirs, s))
2140 _dbus_string_free (&full_path);
2145 _dbus_string_free (&full_path);
2153 e->had_content = TRUE;
2156 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2158 dbus_set_error (error, DBUS_ERROR_FAILED,
2159 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2164 e->d.limit.value = val;
2166 _dbus_verbose ("Loaded value %ld for limit %s\n",
2173 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2177 BUS_SET_OOM (error);
2182 bus_config_parser_finished (BusConfigParser *parser,
2185 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2187 if (parser->stack != NULL)
2189 dbus_set_error (error, DBUS_ERROR_FAILED,
2190 "Element <%s> was not closed in configuration file",
2191 element_type_to_name (top_element_type (parser)));
2196 if (parser->is_toplevel && parser->listen_on == NULL)
2198 dbus_set_error (error, DBUS_ERROR_FAILED,
2199 "Configuration file needs one or more <listen> elements giving addresses");
2207 bus_config_parser_get_user (BusConfigParser *parser)
2209 return parser->user;
2213 bus_config_parser_get_type (BusConfigParser *parser)
2215 return parser->bus_type;
2219 bus_config_parser_get_addresses (BusConfigParser *parser)
2221 return &parser->listen_on;
2225 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2227 return &parser->mechanisms;
2231 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2233 return &parser->service_dirs;
2237 bus_config_parser_get_fork (BusConfigParser *parser)
2239 return parser->fork;
2243 bus_config_parser_get_pidfile (BusConfigParser *parser)
2245 return parser->pidfile;
2249 bus_config_parser_steal_policy (BusConfigParser *parser)
2253 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2255 policy = parser->policy;
2257 parser->policy = NULL;
2262 /* Overwrite any limits that were set in the configuration file */
2264 bus_config_parser_get_limits (BusConfigParser *parser,
2267 *limits = parser->limits;
2271 bus_config_parser_steal_service_sid_table (BusConfigParser *parser)
2273 DBusHashTable *table;
2275 _dbus_assert (parser->service_sid_table != NULL); /* can only steal once */
2277 table = parser->service_sid_table;
2279 parser->service_sid_table = NULL;
2284 #ifdef DBUS_BUILD_TESTS
2295 do_load (const DBusString *full_path,
2297 dbus_bool_t oom_possible)
2299 BusConfigParser *parser;
2302 dbus_error_init (&error);
2304 parser = bus_config_load (full_path, TRUE, NULL, &error);
2307 _DBUS_ASSERT_ERROR_IS_SET (&error);
2310 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2312 _dbus_verbose ("Failed to load valid file due to OOM\n");
2313 dbus_error_free (&error);
2316 else if (validity == VALID)
2318 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2321 dbus_error_free (&error);
2326 dbus_error_free (&error);
2332 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2334 bus_config_parser_unref (parser);
2336 if (validity == INVALID)
2338 _dbus_warn ("Accepted invalid file\n");
2348 const DBusString *full_path;
2353 check_loader_oom_func (void *data)
2355 LoaderOomData *d = data;
2357 return do_load (d->full_path, d->validity, TRUE);
2361 process_test_valid_subdir (const DBusString *test_base_dir,
2365 DBusString test_directory;
2366 DBusString filename;
2374 if (!_dbus_string_init (&test_directory))
2375 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2377 _dbus_string_init_const (&filename, subdir);
2379 if (!_dbus_string_copy (test_base_dir, 0,
2380 &test_directory, 0))
2381 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2383 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2384 _dbus_assert_not_reached ("couldn't allocate full path");
2386 _dbus_string_free (&filename);
2387 if (!_dbus_string_init (&filename))
2388 _dbus_assert_not_reached ("didn't allocate filename string\n");
2390 dbus_error_init (&error);
2391 dir = _dbus_directory_open (&test_directory, &error);
2394 _dbus_warn ("Could not open %s: %s\n",
2395 _dbus_string_get_const_data (&test_directory),
2397 dbus_error_free (&error);
2401 if (validity == VALID)
2402 printf ("Testing valid files:\n");
2403 else if (validity == INVALID)
2404 printf ("Testing invalid files:\n");
2406 printf ("Testing unknown files:\n");
2409 while (_dbus_directory_get_next_file (dir, &filename, &error))
2411 DBusString full_path;
2414 if (!_dbus_string_init (&full_path))
2415 _dbus_assert_not_reached ("couldn't init string");
2417 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2418 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2420 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2421 _dbus_assert_not_reached ("couldn't concat file to dir");
2423 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2425 _dbus_verbose ("Skipping non-.conf file %s\n",
2426 _dbus_string_get_const_data (&filename));
2427 _dbus_string_free (&full_path);
2431 printf (" %s\n", _dbus_string_get_const_data (&filename));
2433 _dbus_verbose (" expecting %s\n",
2434 validity == VALID ? "valid" :
2435 (validity == INVALID ? "invalid" :
2436 (validity == UNKNOWN ? "unknown" : "???")));
2438 d.full_path = &full_path;
2439 d.validity = validity;
2441 /* FIXME hackaround for an expat problem, see
2442 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2443 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2445 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2446 if (!check_loader_oom_func (&d))
2447 _dbus_assert_not_reached ("test failed");
2449 _dbus_string_free (&full_path);
2452 if (dbus_error_is_set (&error))
2454 _dbus_warn ("Could not get next file in %s: %s\n",
2455 _dbus_string_get_const_data (&test_directory),
2457 dbus_error_free (&error);
2466 _dbus_directory_close (dir);
2467 _dbus_string_free (&test_directory);
2468 _dbus_string_free (&filename);
2474 bools_equal (dbus_bool_t a,
2481 strings_equal_or_both_null (const char *a,
2484 if (a == NULL || b == NULL)
2487 return !strcmp (a, b);
2491 elements_equal (const Element *a,
2494 if (a->type != b->type)
2497 if (!bools_equal (a->had_content, b->had_content))
2503 case ELEMENT_INCLUDE:
2504 if (!bools_equal (a->d.include.ignore_missing,
2505 b->d.include.ignore_missing))
2509 case ELEMENT_POLICY:
2510 if (a->d.policy.type != b->d.policy.type)
2512 if (a->d.policy.gid_or_uid != b->d.policy.gid_or_uid)
2517 if (strcmp (a->d.limit.name, b->d.limit.name))
2519 if (a->d.limit.value != b->d.limit.value)
2533 lists_of_elements_equal (DBusList *a,
2542 while (ia != NULL && ib != NULL)
2544 if (elements_equal (ia->data, ib->data))
2546 ia = _dbus_list_get_next_link (&a, ia);
2547 ib = _dbus_list_get_next_link (&b, ib);
2550 return ia == NULL && ib == NULL;
2554 lists_of_c_strings_equal (DBusList *a,
2563 while (ia != NULL && ib != NULL)
2565 if (strcmp (ia->data, ib->data))
2567 ia = _dbus_list_get_next_link (&a, ia);
2568 ib = _dbus_list_get_next_link (&b, ib);
2571 return ia == NULL && ib == NULL;
2575 limits_equal (const BusLimits *a,
2579 (a->max_incoming_bytes == b->max_incoming_bytes
2580 || a->max_outgoing_bytes == b->max_outgoing_bytes
2581 || a->max_message_size == b->max_message_size
2582 || a->activation_timeout == b->activation_timeout
2583 || a->auth_timeout == b->auth_timeout
2584 || a->max_completed_connections == b->max_completed_connections
2585 || a->max_incomplete_connections == b->max_incomplete_connections
2586 || a->max_connections_per_user == b->max_connections_per_user
2587 || a->max_pending_activations == b->max_pending_activations
2588 || a->max_services_per_connection == b->max_services_per_connection
2589 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2590 || a->max_replies_per_connection == b->max_replies_per_connection
2591 || a->reply_timeout == b->reply_timeout);
2595 config_parsers_equal (const BusConfigParser *a,
2596 const BusConfigParser *b)
2598 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2601 if (!lists_of_elements_equal (a->stack, b->stack))
2604 if (!strings_equal_or_both_null (a->user, b->user))
2607 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2610 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2613 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2616 /* FIXME: compare policy */
2618 /* FIXME: compare service selinux ID table */
2620 if (! limits_equal (&a->limits, &b->limits))
2623 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2626 if (! bools_equal (a->fork, b->fork))
2629 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2636 all_are_equiv (const DBusString *target_directory)
2638 DBusString filename;
2640 BusConfigParser *first_parser;
2641 BusConfigParser *parser;
2647 first_parser = NULL;
2651 if (!_dbus_string_init (&filename))
2652 _dbus_assert_not_reached ("didn't allocate filename string");
2654 dbus_error_init (&error);
2655 dir = _dbus_directory_open (target_directory, &error);
2658 _dbus_warn ("Could not open %s: %s\n",
2659 _dbus_string_get_const_data (target_directory),
2661 dbus_error_free (&error);
2665 printf ("Comparing equivalent files:\n");
2668 while (_dbus_directory_get_next_file (dir, &filename, &error))
2670 DBusString full_path;
2672 if (!_dbus_string_init (&full_path))
2673 _dbus_assert_not_reached ("couldn't init string");
2675 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2676 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2678 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2679 _dbus_assert_not_reached ("couldn't concat file to dir");
2681 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2683 _dbus_verbose ("Skipping non-.conf file %s\n",
2684 _dbus_string_get_const_data (&filename));
2685 _dbus_string_free (&full_path);
2689 printf (" %s\n", _dbus_string_get_const_data (&filename));
2691 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2692 _dbus_string_free (&full_path);
2696 _dbus_warn ("Could not load file %s: %s\n",
2697 _dbus_string_get_const_data (&full_path),
2699 dbus_error_free (&error);
2702 else if (first_parser == NULL)
2704 first_parser = parser;
2708 equal = config_parsers_equal (first_parser, parser);
2709 bus_config_parser_unref (parser);
2719 _dbus_string_free (&filename);
2721 bus_config_parser_unref (first_parser);
2723 _dbus_directory_close (dir);
2730 process_test_equiv_subdir (const DBusString *test_base_dir,
2733 DBusString test_directory;
2734 DBusString filename;
2743 if (!_dbus_string_init (&test_directory))
2744 _dbus_assert_not_reached ("didn't allocate test_directory");
2746 _dbus_string_init_const (&filename, subdir);
2748 if (!_dbus_string_copy (test_base_dir, 0,
2749 &test_directory, 0))
2750 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2752 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2753 _dbus_assert_not_reached ("couldn't allocate full path");
2755 _dbus_string_free (&filename);
2756 if (!_dbus_string_init (&filename))
2757 _dbus_assert_not_reached ("didn't allocate filename string");
2759 dbus_error_init (&error);
2760 dir = _dbus_directory_open (&test_directory, &error);
2763 _dbus_warn ("Could not open %s: %s\n",
2764 _dbus_string_get_const_data (&test_directory),
2766 dbus_error_free (&error);
2770 while (_dbus_directory_get_next_file (dir, &filename, &error))
2772 DBusString full_path;
2774 /* Skip CVS's magic directories! */
2775 if (_dbus_string_equal_c_str (&filename, "CVS"))
2778 if (!_dbus_string_init (&full_path))
2779 _dbus_assert_not_reached ("couldn't init string");
2781 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2782 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2784 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2785 _dbus_assert_not_reached ("couldn't concat file to dir");
2787 equal = all_are_equiv (&full_path);
2788 _dbus_string_free (&full_path);
2797 _dbus_string_free (&test_directory);
2798 _dbus_string_free (&filename);
2800 _dbus_directory_close (dir);
2807 bus_config_parser_test (const DBusString *test_data_dir)
2809 if (test_data_dir == NULL ||
2810 _dbus_string_get_length (test_data_dir) == 0)
2812 printf ("No test data\n");
2816 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
2819 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
2822 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
2828 #endif /* DBUS_BUILD_TESTS */