1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* config-parser.c XML-library-agnostic configuration file parser
4 * Copyright (C) 2003, 2004 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config-parser.h"
28 #include <dbus/dbus-list.h>
29 #include <dbus/dbus-internals.h>
55 /* we ignore policies for unknown groups/users */
69 unsigned int had_content : 1;
75 unsigned int ignore_missing : 1;
76 unsigned int selinux_root_relative : 1;
82 unsigned long gid_or_uid;
96 * Parser for bus configuration file.
98 struct BusConfigParser
100 int refcount; /**< Reference count */
102 DBusString basedir; /**< Directory we resolve paths relative to */
104 DBusList *stack; /**< stack of Element */
106 char *user; /**< user to run as */
108 char *bus_type; /**< Message bus type */
110 DBusList *listen_on; /**< List of addresses to listen to */
112 DBusList *mechanisms; /**< Auth mechanisms */
114 DBusList *service_dirs; /**< Directories to look for services in */
116 BusPolicy *policy; /**< Security policy */
118 BusLimits limits; /**< Limits */
120 char *pidfile; /**< PID file */
122 DBusList *included_files; /**< Included files stack */
124 DBusHashTable *service_sid_table; /**< Map service names to SELinux contexts */
126 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
128 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
132 element_type_to_name (ElementType type)
138 case ELEMENT_BUSCONFIG:
140 case ELEMENT_INCLUDE:
158 case ELEMENT_PIDFILE:
160 case ELEMENT_SERVICEDIR:
162 case ELEMENT_INCLUDEDIR:
166 case ELEMENT_SELINUX:
168 case ELEMENT_ASSOCIATE:
172 _dbus_assert_not_reached ("bad element type");
178 push_element (BusConfigParser *parser,
183 _dbus_assert (type != ELEMENT_NONE);
185 e = dbus_new0 (Element, 1);
189 if (!_dbus_list_append (&parser->stack, e))
201 element_free (Element *e)
203 if (e->type == ELEMENT_LIMIT)
204 dbus_free (e->d.limit.name);
210 pop_element (BusConfigParser *parser)
214 e = _dbus_list_pop_last (&parser->stack);
220 peek_element (BusConfigParser *parser)
224 e = _dbus_list_get_last (&parser->stack);
230 top_element_type (BusConfigParser *parser)
234 e = _dbus_list_get_last (&parser->stack);
243 merge_included (BusConfigParser *parser,
244 BusConfigParser *included,
248 DBusHashTable *table;
250 if (!bus_policy_merge (parser->policy,
257 table = bus_selinux_id_table_union (parser->service_sid_table,
258 included->service_sid_table);
265 _dbus_hash_table_unref (parser->service_sid_table);
266 parser->service_sid_table = table;
268 if (included->user != NULL)
270 dbus_free (parser->user);
271 parser->user = included->user;
272 included->user = NULL;
275 if (included->bus_type != NULL)
277 dbus_free (parser->bus_type);
278 parser->bus_type = included->bus_type;
279 included->bus_type = NULL;
285 if (included->pidfile != NULL)
287 dbus_free (parser->pidfile);
288 parser->pidfile = included->pidfile;
289 included->pidfile = NULL;
292 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
293 _dbus_list_append_link (&parser->listen_on, link);
295 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
296 _dbus_list_append_link (&parser->mechanisms, link);
298 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
299 _dbus_list_append_link (&parser->service_dirs, link);
305 seen_include (BusConfigParser *parser,
306 const DBusString *file)
310 iter = parser->included_files;
313 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
316 iter = _dbus_list_get_next_link (&parser->included_files, iter);
323 bus_config_parser_new (const DBusString *basedir,
324 dbus_bool_t is_toplevel,
325 const BusConfigParser *parent)
327 BusConfigParser *parser;
329 parser = dbus_new0 (BusConfigParser, 1);
333 parser->is_toplevel = !!is_toplevel;
335 if (!_dbus_string_init (&parser->basedir))
341 if (((parser->policy = bus_policy_new ()) == NULL) ||
342 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
343 ((parser->service_sid_table = bus_selinux_id_table_new ()) == NULL))
346 bus_policy_unref (parser->policy);
348 _dbus_string_free (&parser->basedir);
356 /* Initialize the parser's limits from the parent. */
357 parser->limits = parent->limits;
359 /* Use the parent's list of included_files to avoid
360 circular inclusions. */
361 parser->included_files = parent->included_files;
366 /* Make up some numbers! woot! */
367 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
368 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
369 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
371 /* Making this long means the user has to wait longer for an error
372 * message if something screws up, but making it too short means
373 * they might see a false failure.
375 parser->limits.activation_timeout = 25000; /* 25 seconds */
377 /* Making this long risks making a DOS attack easier, but too short
378 * and legitimate auth will fail. If interactive auth (ask user for
379 * password) is allowed, then potentially it has to be quite long.
381 parser->limits.auth_timeout = 30000; /* 30 seconds */
383 parser->limits.max_incomplete_connections = 32;
384 parser->limits.max_connections_per_user = 128;
386 /* Note that max_completed_connections / max_connections_per_user
387 * is the number of users that would have to work together to
388 * DOS all the other users.
390 parser->limits.max_completed_connections = 1024;
392 parser->limits.max_pending_activations = 256;
393 parser->limits.max_services_per_connection = 256;
395 parser->limits.max_match_rules_per_connection = 128;
397 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
398 parser->limits.max_replies_per_connection = 32;
401 parser->refcount = 1;
407 bus_config_parser_ref (BusConfigParser *parser)
409 _dbus_assert (parser->refcount > 0);
411 parser->refcount += 1;
417 bus_config_parser_unref (BusConfigParser *parser)
419 _dbus_assert (parser->refcount > 0);
421 parser->refcount -= 1;
423 if (parser->refcount == 0)
425 while (parser->stack != NULL)
426 pop_element (parser);
428 dbus_free (parser->user);
429 dbus_free (parser->bus_type);
430 dbus_free (parser->pidfile);
432 _dbus_list_foreach (&parser->listen_on,
433 (DBusForeachFunction) dbus_free,
436 _dbus_list_clear (&parser->listen_on);
438 _dbus_list_foreach (&parser->service_dirs,
439 (DBusForeachFunction) dbus_free,
442 _dbus_list_clear (&parser->service_dirs);
444 _dbus_list_foreach (&parser->mechanisms,
445 (DBusForeachFunction) dbus_free,
448 _dbus_list_clear (&parser->mechanisms);
450 _dbus_string_free (&parser->basedir);
453 bus_policy_unref (parser->policy);
455 if (parser->service_sid_table)
456 _dbus_hash_table_unref (parser->service_sid_table);
463 bus_config_parser_check_doctype (BusConfigParser *parser,
467 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
469 if (strcmp (doctype, "busconfig") != 0)
471 dbus_set_error (error,
473 "Configuration file has the wrong document type %s",
488 locate_attributes (BusConfigParser *parser,
489 const char *element_name,
490 const char **attribute_names,
491 const char **attribute_values,
493 const char *first_attribute_name,
494 const char **first_attribute_retloc,
502 LocateAttr attrs[MAX_ATTRS];
506 _dbus_assert (first_attribute_name != NULL);
507 _dbus_assert (first_attribute_retloc != NULL);
512 attrs[0].name = first_attribute_name;
513 attrs[0].retloc = first_attribute_retloc;
514 *first_attribute_retloc = NULL;
516 va_start (args, first_attribute_retloc);
518 name = va_arg (args, const char*);
519 retloc = va_arg (args, const char**);
523 _dbus_assert (retloc != NULL);
524 _dbus_assert (n_attrs < MAX_ATTRS);
526 attrs[n_attrs].name = name;
527 attrs[n_attrs].retloc = retloc;
531 name = va_arg (args, const char*);
532 retloc = va_arg (args, const char**);
541 while (attribute_names[i])
550 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
552 retloc = attrs[j].retloc;
556 dbus_set_error (error, DBUS_ERROR_FAILED,
557 "Attribute \"%s\" repeated twice on the same <%s> element",
558 attrs[j].name, element_name);
563 *retloc = attribute_values[i];
572 dbus_set_error (error, DBUS_ERROR_FAILED,
573 "Attribute \"%s\" is invalid on <%s> element in this context",
574 attribute_names[i], element_name);
587 check_no_attributes (BusConfigParser *parser,
588 const char *element_name,
589 const char **attribute_names,
590 const char **attribute_values,
593 if (attribute_names[0] != NULL)
595 dbus_set_error (error, DBUS_ERROR_FAILED,
596 "Attribute \"%s\" is invalid on <%s> element in this context",
597 attribute_names[0], element_name);
605 start_busconfig_child (BusConfigParser *parser,
606 const char *element_name,
607 const char **attribute_names,
608 const char **attribute_values,
611 if (strcmp (element_name, "user") == 0)
613 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
616 if (push_element (parser, ELEMENT_USER) == NULL)
624 else if (strcmp (element_name, "type") == 0)
626 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
629 if (push_element (parser, ELEMENT_TYPE) == NULL)
637 else if (strcmp (element_name, "fork") == 0)
639 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
642 if (push_element (parser, ELEMENT_FORK) == NULL)
652 else if (strcmp (element_name, "pidfile") == 0)
654 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
657 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
665 else if (strcmp (element_name, "listen") == 0)
667 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
670 if (push_element (parser, ELEMENT_LISTEN) == NULL)
678 else if (strcmp (element_name, "auth") == 0)
680 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
683 if (push_element (parser, ELEMENT_AUTH) == NULL)
691 else if (strcmp (element_name, "includedir") == 0)
693 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
696 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
704 else if (strcmp (element_name, "servicedir") == 0)
706 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
709 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
717 else if (strcmp (element_name, "include") == 0)
720 const char *ignore_missing;
721 const char *selinux_root_relative;
723 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
729 e->d.include.ignore_missing = FALSE;
730 e->d.include.selinux_root_relative = FALSE;
732 if (!locate_attributes (parser, "include",
736 "ignore_missing", &ignore_missing,
737 "selinux_root_relative", &selinux_root_relative,
741 if (ignore_missing != NULL)
743 if (strcmp (ignore_missing, "yes") == 0)
744 e->d.include.ignore_missing = TRUE;
745 else if (strcmp (ignore_missing, "no") == 0)
746 e->d.include.ignore_missing = FALSE;
749 dbus_set_error (error, DBUS_ERROR_FAILED,
750 "ignore_missing attribute must have value \"yes\" or \"no\"");
755 if (selinux_root_relative != NULL)
757 if (strcmp (selinux_root_relative, "yes") == 0)
758 e->d.include.selinux_root_relative = TRUE;
759 else if (strcmp (selinux_root_relative, "no") == 0)
760 e->d.include.selinux_root_relative = FALSE;
763 dbus_set_error (error, DBUS_ERROR_FAILED,
764 "selinux_root_relative attribute must have value"
765 " \"yes\" or \"no\"");
772 else if (strcmp (element_name, "policy") == 0)
779 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
785 e->d.policy.type = POLICY_IGNORED;
787 if (!locate_attributes (parser, "policy",
797 if (((context && user) ||
798 (context && group)) ||
800 !(context || user || group))
802 dbus_set_error (error, DBUS_ERROR_FAILED,
803 "<policy> element must have exactly one of (context|user|group) attributes");
809 if (strcmp (context, "default") == 0)
811 e->d.policy.type = POLICY_DEFAULT;
813 else if (strcmp (context, "mandatory") == 0)
815 e->d.policy.type = POLICY_MANDATORY;
819 dbus_set_error (error, DBUS_ERROR_FAILED,
820 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
825 else if (user != NULL)
828 _dbus_string_init_const (&username, user);
830 if (_dbus_get_user_id (&username,
831 &e->d.policy.gid_or_uid))
832 e->d.policy.type = POLICY_USER;
834 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
837 else if (group != NULL)
839 DBusString group_name;
840 _dbus_string_init_const (&group_name, group);
842 if (_dbus_get_group_id (&group_name,
843 &e->d.policy.gid_or_uid))
844 e->d.policy.type = POLICY_GROUP;
846 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
851 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
856 else if (strcmp (element_name, "limit") == 0)
861 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
867 if (!locate_attributes (parser, "limit",
877 dbus_set_error (error, DBUS_ERROR_FAILED,
878 "<limit> element must have a \"name\" attribute");
882 e->d.limit.name = _dbus_strdup (name);
883 if (e->d.limit.name == NULL)
891 else if (strcmp (element_name, "selinux") == 0)
893 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
896 if (push_element (parser, ELEMENT_SELINUX) == NULL)
906 dbus_set_error (error, DBUS_ERROR_FAILED,
907 "Element <%s> not allowed inside <%s> in configuration file",
908 element_name, "busconfig");
914 append_rule_from_element (BusConfigParser *parser,
915 const char *element_name,
916 const char **attribute_names,
917 const char **attribute_values,
921 const char *send_interface;
922 const char *send_member;
923 const char *send_error;
924 const char *send_destination;
925 const char *send_path;
926 const char *send_type;
927 const char *receive_interface;
928 const char *receive_member;
929 const char *receive_error;
930 const char *receive_sender;
931 const char *receive_path;
932 const char *receive_type;
933 const char *eavesdrop;
934 const char *send_requested_reply;
935 const char *receive_requested_reply;
941 if (!locate_attributes (parser, element_name,
945 "send_interface", &send_interface,
946 "send_member", &send_member,
947 "send_error", &send_error,
948 "send_destination", &send_destination,
949 "send_path", &send_path,
950 "send_type", &send_type,
951 "receive_interface", &receive_interface,
952 "receive_member", &receive_member,
953 "receive_error", &receive_error,
954 "receive_sender", &receive_sender,
955 "receive_path", &receive_path,
956 "receive_type", &receive_type,
957 "eavesdrop", &eavesdrop,
958 "send_requested_reply", &send_requested_reply,
959 "receive_requested_reply", &receive_requested_reply,
966 if (!(send_interface || send_member || send_error || send_destination ||
967 send_type || send_path ||
968 receive_interface || receive_member || receive_error || receive_sender ||
969 receive_type || receive_path || eavesdrop ||
970 send_requested_reply || receive_requested_reply ||
971 own || user || group))
973 dbus_set_error (error, DBUS_ERROR_FAILED,
974 "Element <%s> must have one or more attributes",
979 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
980 (receive_member && (receive_interface == NULL && receive_path == NULL)))
982 dbus_set_error (error, DBUS_ERROR_FAILED,
983 "On element <%s>, if you specify a member you must specify an interface or a path. Keep in mind that not all messages have an interface field.",
988 /* Allowed combinations of elements are:
990 * base, must be all send or all receive:
996 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
997 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
999 * user, group, own must occur alone
1001 * Pretty sure the below stuff is broken, FIXME think about it more.
1004 if (((send_interface && send_error) ||
1005 (send_interface && receive_interface) ||
1006 (send_interface && receive_member) ||
1007 (send_interface && receive_error) ||
1008 (send_interface && receive_sender) ||
1009 (send_interface && eavesdrop) ||
1010 (send_interface && receive_requested_reply) ||
1011 (send_interface && own) ||
1012 (send_interface && user) ||
1013 (send_interface && group)) ||
1015 ((send_member && send_error) ||
1016 (send_member && receive_interface) ||
1017 (send_member && receive_member) ||
1018 (send_member && receive_error) ||
1019 (send_member && receive_sender) ||
1020 (send_member && eavesdrop) ||
1021 (send_member && receive_requested_reply) ||
1022 (send_member && own) ||
1023 (send_member && user) ||
1024 (send_member && group)) ||
1026 ((send_error && receive_interface) ||
1027 (send_error && receive_member) ||
1028 (send_error && receive_error) ||
1029 (send_error && receive_sender) ||
1030 (send_error && eavesdrop) ||
1031 (send_error && receive_requested_reply) ||
1032 (send_error && own) ||
1033 (send_error && user) ||
1034 (send_error && group)) ||
1036 ((send_destination && receive_interface) ||
1037 (send_destination && receive_member) ||
1038 (send_destination && receive_error) ||
1039 (send_destination && receive_sender) ||
1040 (send_destination && eavesdrop) ||
1041 (send_destination && receive_requested_reply) ||
1042 (send_destination && own) ||
1043 (send_destination && user) ||
1044 (send_destination && group)) ||
1046 ((send_type && receive_interface) ||
1047 (send_type && receive_member) ||
1048 (send_type && receive_error) ||
1049 (send_type && receive_sender) ||
1050 (send_type && eavesdrop) ||
1051 (send_type && receive_requested_reply) ||
1052 (send_type && own) ||
1053 (send_type && user) ||
1054 (send_type && group)) ||
1056 ((send_path && receive_interface) ||
1057 (send_path && receive_member) ||
1058 (send_path && receive_error) ||
1059 (send_path && receive_sender) ||
1060 (send_path && eavesdrop) ||
1061 (send_path && receive_requested_reply) ||
1062 (send_path && own) ||
1063 (send_path && user) ||
1064 (send_path && group)) ||
1066 ((send_requested_reply && receive_interface) ||
1067 (send_requested_reply && receive_member) ||
1068 (send_requested_reply && receive_error) ||
1069 (send_requested_reply && receive_sender) ||
1070 (send_requested_reply && eavesdrop) ||
1071 (send_requested_reply && receive_requested_reply) ||
1072 (send_requested_reply && own) ||
1073 (send_requested_reply && user) ||
1074 (send_requested_reply && group)) ||
1076 ((receive_interface && receive_error) ||
1077 (receive_interface && own) ||
1078 (receive_interface && user) ||
1079 (receive_interface && group)) ||
1081 ((receive_member && receive_error) ||
1082 (receive_member && own) ||
1083 (receive_member && user) ||
1084 (receive_member && group)) ||
1086 ((receive_error && own) ||
1087 (receive_error && user) ||
1088 (receive_error && group)) ||
1090 ((eavesdrop && own) ||
1091 (eavesdrop && user) ||
1092 (eavesdrop && group)) ||
1094 ((receive_requested_reply && own) ||
1095 (receive_requested_reply && user) ||
1096 (receive_requested_reply && group)) ||
1103 dbus_set_error (error, DBUS_ERROR_FAILED,
1104 "Invalid combination of attributes on element <%s>",
1111 /* In BusPolicyRule, NULL represents wildcard.
1112 * In the config file, '*' represents it.
1114 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1116 if (send_interface || send_member || send_error || send_destination ||
1117 send_path || send_type || send_requested_reply)
1121 if (IS_WILDCARD (send_interface))
1122 send_interface = NULL;
1123 if (IS_WILDCARD (send_member))
1125 if (IS_WILDCARD (send_error))
1127 if (IS_WILDCARD (send_destination))
1128 send_destination = NULL;
1129 if (IS_WILDCARD (send_path))
1131 if (IS_WILDCARD (send_type))
1134 message_type = DBUS_MESSAGE_TYPE_INVALID;
1135 if (send_type != NULL)
1137 message_type = dbus_message_type_from_string (send_type);
1138 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1140 dbus_set_error (error, DBUS_ERROR_FAILED,
1141 "Bad message type \"%s\"",
1147 if (send_requested_reply &&
1148 !(strcmp (send_requested_reply, "true") == 0 ||
1149 strcmp (send_requested_reply, "false") == 0))
1151 dbus_set_error (error, DBUS_ERROR_FAILED,
1152 "Bad value \"%s\" for %s attribute, must be true or false",
1153 "send_requested_reply", send_requested_reply);
1157 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1161 if (send_requested_reply)
1162 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1164 rule->d.send.message_type = message_type;
1165 rule->d.send.path = _dbus_strdup (send_path);
1166 rule->d.send.interface = _dbus_strdup (send_interface);
1167 rule->d.send.member = _dbus_strdup (send_member);
1168 rule->d.send.error = _dbus_strdup (send_error);
1169 rule->d.send.destination = _dbus_strdup (send_destination);
1170 if (send_path && rule->d.send.path == NULL)
1172 if (send_interface && rule->d.send.interface == NULL)
1174 if (send_member && rule->d.send.member == NULL)
1176 if (send_error && rule->d.send.error == NULL)
1178 if (send_destination && rule->d.send.destination == NULL)
1181 else if (receive_interface || receive_member || receive_error || receive_sender ||
1182 receive_path || receive_type || eavesdrop || receive_requested_reply)
1186 if (IS_WILDCARD (receive_interface))
1187 receive_interface = NULL;
1188 if (IS_WILDCARD (receive_member))
1189 receive_member = NULL;
1190 if (IS_WILDCARD (receive_error))
1191 receive_error = NULL;
1192 if (IS_WILDCARD (receive_sender))
1193 receive_sender = NULL;
1194 if (IS_WILDCARD (receive_path))
1195 receive_path = NULL;
1196 if (IS_WILDCARD (receive_type))
1197 receive_type = NULL;
1199 message_type = DBUS_MESSAGE_TYPE_INVALID;
1200 if (receive_type != NULL)
1202 message_type = dbus_message_type_from_string (receive_type);
1203 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1205 dbus_set_error (error, DBUS_ERROR_FAILED,
1206 "Bad message type \"%s\"",
1214 !(strcmp (eavesdrop, "true") == 0 ||
1215 strcmp (eavesdrop, "false") == 0))
1217 dbus_set_error (error, DBUS_ERROR_FAILED,
1218 "Bad value \"%s\" for %s attribute, must be true or false",
1219 "eavesdrop", eavesdrop);
1223 if (receive_requested_reply &&
1224 !(strcmp (receive_requested_reply, "true") == 0 ||
1225 strcmp (receive_requested_reply, "false") == 0))
1227 dbus_set_error (error, DBUS_ERROR_FAILED,
1228 "Bad value \"%s\" for %s attribute, must be true or false",
1229 "receive_requested_reply", receive_requested_reply);
1233 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1238 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1240 if (receive_requested_reply)
1241 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1243 rule->d.receive.message_type = message_type;
1244 rule->d.receive.path = _dbus_strdup (receive_path);
1245 rule->d.receive.interface = _dbus_strdup (receive_interface);
1246 rule->d.receive.member = _dbus_strdup (receive_member);
1247 rule->d.receive.error = _dbus_strdup (receive_error);
1248 rule->d.receive.origin = _dbus_strdup (receive_sender);
1250 if (receive_path && rule->d.receive.path == NULL)
1252 if (receive_interface && rule->d.receive.interface == NULL)
1254 if (receive_member && rule->d.receive.member == NULL)
1256 if (receive_error && rule->d.receive.error == NULL)
1258 if (receive_sender && rule->d.receive.origin == NULL)
1263 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1267 if (IS_WILDCARD (own))
1270 rule->d.own.service_name = _dbus_strdup (own);
1271 if (own && rule->d.own.service_name == NULL)
1276 if (IS_WILDCARD (user))
1278 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1282 rule->d.user.uid = DBUS_UID_UNSET;
1286 DBusString username;
1289 _dbus_string_init_const (&username, user);
1291 if (_dbus_get_user_id (&username, &uid))
1293 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1297 rule->d.user.uid = uid;
1301 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1302 user, element_name);
1308 if (IS_WILDCARD (group))
1310 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1314 rule->d.group.gid = DBUS_GID_UNSET;
1318 DBusString groupname;
1321 _dbus_string_init_const (&groupname, group);
1323 if (_dbus_get_user_id (&groupname, &gid))
1325 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1329 rule->d.group.gid = gid;
1333 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1334 group, element_name);
1339 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1345 pe = peek_element (parser);
1346 _dbus_assert (pe != NULL);
1347 _dbus_assert (pe->type == ELEMENT_POLICY);
1349 switch (pe->d.policy.type)
1351 case POLICY_IGNORED:
1352 /* drop the rule on the floor */
1355 case POLICY_DEFAULT:
1356 if (!bus_policy_append_default_rule (parser->policy, rule))
1359 case POLICY_MANDATORY:
1360 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1364 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1366 dbus_set_error (error, DBUS_ERROR_FAILED,
1367 "<%s> rule cannot be per-user because it has bus-global semantics",
1372 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
1377 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1379 dbus_set_error (error, DBUS_ERROR_FAILED,
1380 "<%s> rule cannot be per-group because it has bus-global semantics",
1385 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
1391 bus_policy_rule_unref (rule);
1398 BUS_SET_OOM (error);
1401 bus_policy_rule_unref (rule);
1406 start_policy_child (BusConfigParser *parser,
1407 const char *element_name,
1408 const char **attribute_names,
1409 const char **attribute_values,
1412 if (strcmp (element_name, "allow") == 0)
1414 if (!append_rule_from_element (parser, element_name,
1415 attribute_names, attribute_values,
1419 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1421 BUS_SET_OOM (error);
1427 else if (strcmp (element_name, "deny") == 0)
1429 if (!append_rule_from_element (parser, element_name,
1430 attribute_names, attribute_values,
1434 if (push_element (parser, ELEMENT_DENY) == NULL)
1436 BUS_SET_OOM (error);
1444 dbus_set_error (error, DBUS_ERROR_FAILED,
1445 "Element <%s> not allowed inside <%s> in configuration file",
1446 element_name, "policy");
1452 start_selinux_child (BusConfigParser *parser,
1453 const char *element_name,
1454 const char **attribute_names,
1455 const char **attribute_values,
1458 if (strcmp (element_name, "associate") == 0)
1461 const char *context;
1463 if (!locate_attributes (parser, "associate",
1468 "context", &context,
1472 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1474 BUS_SET_OOM (error);
1478 if (own == NULL || context == NULL)
1480 dbus_set_error (error, DBUS_ERROR_FAILED,
1481 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1485 if (!bus_selinux_id_table_insert (parser->service_sid_table,
1488 BUS_SET_OOM (error);
1496 dbus_set_error (error, DBUS_ERROR_FAILED,
1497 "Element <%s> not allowed inside <%s> in configuration file",
1498 element_name, "selinux");
1504 bus_config_parser_start_element (BusConfigParser *parser,
1505 const char *element_name,
1506 const char **attribute_names,
1507 const char **attribute_values,
1512 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1514 /* printf ("START: %s\n", element_name); */
1516 t = top_element_type (parser);
1518 if (t == ELEMENT_NONE)
1520 if (strcmp (element_name, "busconfig") == 0)
1522 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1525 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1527 BUS_SET_OOM (error);
1535 dbus_set_error (error, DBUS_ERROR_FAILED,
1536 "Unknown element <%s> at root of configuration file",
1541 else if (t == ELEMENT_BUSCONFIG)
1543 return start_busconfig_child (parser, element_name,
1544 attribute_names, attribute_values,
1547 else if (t == ELEMENT_POLICY)
1549 return start_policy_child (parser, element_name,
1550 attribute_names, attribute_values,
1553 else if (t == ELEMENT_SELINUX)
1555 return start_selinux_child (parser, element_name,
1556 attribute_names, attribute_values,
1561 dbus_set_error (error, DBUS_ERROR_FAILED,
1562 "Element <%s> is not allowed in this context",
1569 set_limit (BusConfigParser *parser,
1574 dbus_bool_t must_be_positive;
1575 dbus_bool_t must_be_int;
1577 must_be_int = FALSE;
1578 must_be_positive = FALSE;
1580 if (strcmp (name, "max_incoming_bytes") == 0)
1582 must_be_positive = TRUE;
1583 parser->limits.max_incoming_bytes = value;
1585 else if (strcmp (name, "max_outgoing_bytes") == 0)
1587 must_be_positive = TRUE;
1588 parser->limits.max_outgoing_bytes = value;
1590 else if (strcmp (name, "max_message_size") == 0)
1592 must_be_positive = TRUE;
1593 parser->limits.max_message_size = value;
1595 else if (strcmp (name, "activation_timeout") == 0)
1597 must_be_positive = TRUE;
1599 parser->limits.activation_timeout = value;
1601 else if (strcmp (name, "auth_timeout") == 0)
1603 must_be_positive = TRUE;
1605 parser->limits.auth_timeout = value;
1607 else if (strcmp (name, "reply_timeout") == 0)
1609 must_be_positive = TRUE;
1611 parser->limits.reply_timeout = value;
1613 else if (strcmp (name, "max_completed_connections") == 0)
1615 must_be_positive = TRUE;
1617 parser->limits.max_completed_connections = value;
1619 else if (strcmp (name, "max_incomplete_connections") == 0)
1621 must_be_positive = TRUE;
1623 parser->limits.max_incomplete_connections = value;
1625 else if (strcmp (name, "max_connections_per_user") == 0)
1627 must_be_positive = TRUE;
1629 parser->limits.max_connections_per_user = value;
1631 else if (strcmp (name, "max_pending_activations") == 0)
1633 must_be_positive = TRUE;
1635 parser->limits.max_pending_activations = value;
1637 else if (strcmp (name, "max_services_per_connection") == 0)
1639 must_be_positive = TRUE;
1641 parser->limits.max_services_per_connection = value;
1643 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1645 must_be_positive = TRUE;
1647 parser->limits.max_match_rules_per_connection = value;
1649 else if (strcmp (name, "max_replies_per_connection") == 0)
1651 must_be_positive = TRUE;
1653 parser->limits.max_replies_per_connection = value;
1657 dbus_set_error (error, DBUS_ERROR_FAILED,
1658 "There is no limit called \"%s\"\n",
1663 if (must_be_positive && value < 0)
1665 dbus_set_error (error, DBUS_ERROR_FAILED,
1666 "<limit name=\"%s\"> must be a positive number\n",
1672 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1674 dbus_set_error (error, DBUS_ERROR_FAILED,
1675 "<limit name=\"%s\"> value is too large\n",
1684 bus_config_parser_end_element (BusConfigParser *parser,
1685 const char *element_name,
1692 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1694 /* printf ("END: %s\n", element_name); */
1696 t = top_element_type (parser);
1698 if (t == ELEMENT_NONE)
1700 /* should probably be an assertion failure but
1701 * being paranoid about XML parsers
1703 dbus_set_error (error, DBUS_ERROR_FAILED,
1704 "XML parser ended element with no element on the stack");
1708 n = element_type_to_name (t);
1709 _dbus_assert (n != NULL);
1710 if (strcmp (n, element_name) != 0)
1712 /* should probably be an assertion failure but
1713 * being paranoid about XML parsers
1715 dbus_set_error (error, DBUS_ERROR_FAILED,
1716 "XML element <%s> ended but topmost element on the stack was <%s>",
1721 e = peek_element (parser);
1722 _dbus_assert (e != NULL);
1727 _dbus_assert_not_reached ("element in stack has no type");
1730 case ELEMENT_INCLUDE:
1733 case ELEMENT_LISTEN:
1734 case ELEMENT_PIDFILE:
1736 case ELEMENT_SERVICEDIR:
1737 case ELEMENT_INCLUDEDIR:
1739 if (!e->had_content)
1741 dbus_set_error (error, DBUS_ERROR_FAILED,
1742 "XML element <%s> was expected to have content inside it",
1743 element_type_to_name (e->type));
1747 if (e->type == ELEMENT_LIMIT)
1749 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1755 case ELEMENT_BUSCONFIG:
1756 case ELEMENT_POLICY:
1760 case ELEMENT_SELINUX:
1761 case ELEMENT_ASSOCIATE:
1765 pop_element (parser);
1771 all_whitespace (const DBusString *str)
1775 _dbus_string_skip_white (str, 0, &i);
1777 return i == _dbus_string_get_length (str);
1781 make_full_path (const DBusString *basedir,
1782 const DBusString *filename,
1783 DBusString *full_path)
1785 if (_dbus_path_is_absolute (filename))
1787 return _dbus_string_copy (filename, 0, full_path, 0);
1791 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1794 if (!_dbus_concat_dir_and_file (full_path, filename))
1802 include_file (BusConfigParser *parser,
1803 const DBusString *filename,
1804 dbus_bool_t ignore_missing,
1807 /* FIXME good test case for this would load each config file in the
1808 * test suite both alone, and as an include, and check
1809 * that the result is the same
1811 BusConfigParser *included;
1812 const char *filename_str;
1813 DBusError tmp_error;
1815 dbus_error_init (&tmp_error);
1817 filename_str = _dbus_string_get_const_data (filename);
1819 /* Check to make sure this file hasn't already been included. */
1820 if (seen_include (parser, filename))
1822 dbus_set_error (error, DBUS_ERROR_FAILED,
1823 "Circular inclusion of file '%s'",
1828 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
1830 BUS_SET_OOM (error);
1834 /* Since parser is passed in as the parent, included
1835 inherits parser's limits. */
1836 included = bus_config_load (filename, FALSE, parser, &tmp_error);
1838 _dbus_list_pop_last (&parser->included_files);
1840 if (included == NULL)
1842 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1844 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1847 dbus_error_free (&tmp_error);
1852 dbus_move_error (&tmp_error, error);
1858 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1860 if (!merge_included (parser, included, error))
1862 bus_config_parser_unref (included);
1866 /* Copy included's limits back to parser. */
1867 parser->limits = included->limits;
1869 bus_config_parser_unref (included);
1875 include_dir (BusConfigParser *parser,
1876 const DBusString *dirname,
1879 DBusString filename;
1881 DBusError tmp_error;
1884 if (!_dbus_string_init (&filename))
1886 BUS_SET_OOM (error);
1892 dir = _dbus_directory_open (dirname, error);
1897 dbus_error_init (&tmp_error);
1898 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1900 DBusString full_path;
1902 if (!_dbus_string_init (&full_path))
1904 BUS_SET_OOM (error);
1908 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1910 BUS_SET_OOM (error);
1911 _dbus_string_free (&full_path);
1915 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1917 BUS_SET_OOM (error);
1918 _dbus_string_free (&full_path);
1922 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1924 if (!include_file (parser, &full_path, TRUE, error))
1926 _dbus_string_free (&full_path);
1931 _dbus_string_free (&full_path);
1934 if (dbus_error_is_set (&tmp_error))
1936 dbus_move_error (&tmp_error, error);
1943 _dbus_string_free (&filename);
1946 _dbus_directory_close (dir);
1952 bus_config_parser_content (BusConfigParser *parser,
1953 const DBusString *content,
1958 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1964 _dbus_string_get_const_data (content, &c_str);
1966 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1970 e = peek_element (parser);
1973 dbus_set_error (error, DBUS_ERROR_FAILED,
1974 "Text content outside of any XML element in configuration file");
1977 else if (e->had_content)
1979 _dbus_assert_not_reached ("Element had multiple content blocks");
1983 switch (top_element_type (parser))
1986 _dbus_assert_not_reached ("element at top of stack has no type");
1989 case ELEMENT_BUSCONFIG:
1990 case ELEMENT_POLICY:
1994 case ELEMENT_SELINUX:
1995 case ELEMENT_ASSOCIATE:
1996 if (all_whitespace (content))
2000 dbus_set_error (error, DBUS_ERROR_FAILED,
2001 "No text content expected inside XML element %s in configuration file",
2002 element_type_to_name (top_element_type (parser)));
2006 case ELEMENT_PIDFILE:
2010 e->had_content = TRUE;
2012 if (!_dbus_string_copy_data (content, &s))
2015 dbus_free (parser->pidfile);
2016 parser->pidfile = s;
2020 case ELEMENT_INCLUDE:
2022 DBusString full_path, selinux_policy_root;
2024 e->had_content = TRUE;
2026 if (!_dbus_string_init (&full_path))
2029 if (e->d.include.selinux_root_relative)
2031 if (!bus_selinux_get_policy_root ())
2033 dbus_set_error (error, DBUS_ERROR_FAILED,
2034 "Could not determine SELinux policy root for relative inclusion");
2035 _dbus_string_free (&full_path);
2038 _dbus_string_init_const (&selinux_policy_root,
2039 bus_selinux_get_policy_root ());
2040 if (!make_full_path (&selinux_policy_root, content, &full_path))
2042 _dbus_string_free (&full_path);
2046 else if (!make_full_path (&parser->basedir, content, &full_path))
2048 _dbus_string_free (&full_path);
2052 if (!include_file (parser, &full_path,
2053 e->d.include.ignore_missing, error))
2055 _dbus_string_free (&full_path);
2059 _dbus_string_free (&full_path);
2063 case ELEMENT_INCLUDEDIR:
2065 DBusString full_path;
2067 e->had_content = TRUE;
2069 if (!_dbus_string_init (&full_path))
2072 if (!make_full_path (&parser->basedir, content, &full_path))
2074 _dbus_string_free (&full_path);
2078 if (!include_dir (parser, &full_path, error))
2080 _dbus_string_free (&full_path);
2084 _dbus_string_free (&full_path);
2092 e->had_content = TRUE;
2094 if (!_dbus_string_copy_data (content, &s))
2097 dbus_free (parser->user);
2106 e->had_content = TRUE;
2108 if (!_dbus_string_copy_data (content, &s))
2111 dbus_free (parser->bus_type);
2112 parser->bus_type = s;
2116 case ELEMENT_LISTEN:
2120 e->had_content = TRUE;
2122 if (!_dbus_string_copy_data (content, &s))
2125 if (!_dbus_list_append (&parser->listen_on,
2138 e->had_content = TRUE;
2140 if (!_dbus_string_copy_data (content, &s))
2143 if (!_dbus_list_append (&parser->mechanisms,
2152 case ELEMENT_SERVICEDIR:
2155 DBusString full_path;
2157 e->had_content = TRUE;
2159 if (!_dbus_string_init (&full_path))
2162 if (!make_full_path (&parser->basedir, content, &full_path))
2164 _dbus_string_free (&full_path);
2168 if (!_dbus_string_copy_data (&full_path, &s))
2170 _dbus_string_free (&full_path);
2174 if (!_dbus_list_append (&parser->service_dirs, s))
2176 _dbus_string_free (&full_path);
2181 _dbus_string_free (&full_path);
2189 e->had_content = TRUE;
2192 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2194 dbus_set_error (error, DBUS_ERROR_FAILED,
2195 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2200 e->d.limit.value = val;
2202 _dbus_verbose ("Loaded value %ld for limit %s\n",
2209 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2213 BUS_SET_OOM (error);
2218 bus_config_parser_finished (BusConfigParser *parser,
2221 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2223 if (parser->stack != NULL)
2225 dbus_set_error (error, DBUS_ERROR_FAILED,
2226 "Element <%s> was not closed in configuration file",
2227 element_type_to_name (top_element_type (parser)));
2232 if (parser->is_toplevel && parser->listen_on == NULL)
2234 dbus_set_error (error, DBUS_ERROR_FAILED,
2235 "Configuration file needs one or more <listen> elements giving addresses");
2243 bus_config_parser_get_user (BusConfigParser *parser)
2245 return parser->user;
2249 bus_config_parser_get_type (BusConfigParser *parser)
2251 return parser->bus_type;
2255 bus_config_parser_get_addresses (BusConfigParser *parser)
2257 return &parser->listen_on;
2261 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2263 return &parser->mechanisms;
2267 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2269 return &parser->service_dirs;
2273 bus_config_parser_get_fork (BusConfigParser *parser)
2275 return parser->fork;
2279 bus_config_parser_get_pidfile (BusConfigParser *parser)
2281 return parser->pidfile;
2285 bus_config_parser_steal_policy (BusConfigParser *parser)
2289 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2291 policy = parser->policy;
2293 parser->policy = NULL;
2298 /* Overwrite any limits that were set in the configuration file */
2300 bus_config_parser_get_limits (BusConfigParser *parser,
2303 *limits = parser->limits;
2307 bus_config_parser_steal_service_sid_table (BusConfigParser *parser)
2309 DBusHashTable *table;
2311 _dbus_assert (parser->service_sid_table != NULL); /* can only steal once */
2313 table = parser->service_sid_table;
2315 parser->service_sid_table = NULL;
2320 #ifdef DBUS_BUILD_TESTS
2331 do_load (const DBusString *full_path,
2333 dbus_bool_t oom_possible)
2335 BusConfigParser *parser;
2338 dbus_error_init (&error);
2340 parser = bus_config_load (full_path, TRUE, NULL, &error);
2343 _DBUS_ASSERT_ERROR_IS_SET (&error);
2346 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2348 _dbus_verbose ("Failed to load valid file due to OOM\n");
2349 dbus_error_free (&error);
2352 else if (validity == VALID)
2354 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2357 dbus_error_free (&error);
2362 dbus_error_free (&error);
2368 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2370 bus_config_parser_unref (parser);
2372 if (validity == INVALID)
2374 _dbus_warn ("Accepted invalid file\n");
2384 const DBusString *full_path;
2389 check_loader_oom_func (void *data)
2391 LoaderOomData *d = data;
2393 return do_load (d->full_path, d->validity, TRUE);
2397 process_test_valid_subdir (const DBusString *test_base_dir,
2401 DBusString test_directory;
2402 DBusString filename;
2410 if (!_dbus_string_init (&test_directory))
2411 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2413 _dbus_string_init_const (&filename, subdir);
2415 if (!_dbus_string_copy (test_base_dir, 0,
2416 &test_directory, 0))
2417 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2419 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2420 _dbus_assert_not_reached ("couldn't allocate full path");
2422 _dbus_string_free (&filename);
2423 if (!_dbus_string_init (&filename))
2424 _dbus_assert_not_reached ("didn't allocate filename string\n");
2426 dbus_error_init (&error);
2427 dir = _dbus_directory_open (&test_directory, &error);
2430 _dbus_warn ("Could not open %s: %s\n",
2431 _dbus_string_get_const_data (&test_directory),
2433 dbus_error_free (&error);
2437 if (validity == VALID)
2438 printf ("Testing valid files:\n");
2439 else if (validity == INVALID)
2440 printf ("Testing invalid files:\n");
2442 printf ("Testing unknown files:\n");
2445 while (_dbus_directory_get_next_file (dir, &filename, &error))
2447 DBusString full_path;
2450 if (!_dbus_string_init (&full_path))
2451 _dbus_assert_not_reached ("couldn't init string");
2453 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2454 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2456 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2457 _dbus_assert_not_reached ("couldn't concat file to dir");
2459 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2461 _dbus_verbose ("Skipping non-.conf file %s\n",
2462 _dbus_string_get_const_data (&filename));
2463 _dbus_string_free (&full_path);
2467 printf (" %s\n", _dbus_string_get_const_data (&filename));
2469 _dbus_verbose (" expecting %s\n",
2470 validity == VALID ? "valid" :
2471 (validity == INVALID ? "invalid" :
2472 (validity == UNKNOWN ? "unknown" : "???")));
2474 d.full_path = &full_path;
2475 d.validity = validity;
2477 /* FIXME hackaround for an expat problem, see
2478 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2479 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2481 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2482 if (!check_loader_oom_func (&d))
2483 _dbus_assert_not_reached ("test failed");
2485 _dbus_string_free (&full_path);
2488 if (dbus_error_is_set (&error))
2490 _dbus_warn ("Could not get next file in %s: %s\n",
2491 _dbus_string_get_const_data (&test_directory),
2493 dbus_error_free (&error);
2502 _dbus_directory_close (dir);
2503 _dbus_string_free (&test_directory);
2504 _dbus_string_free (&filename);
2510 bools_equal (dbus_bool_t a,
2517 strings_equal_or_both_null (const char *a,
2520 if (a == NULL || b == NULL)
2523 return !strcmp (a, b);
2527 elements_equal (const Element *a,
2530 if (a->type != b->type)
2533 if (!bools_equal (a->had_content, b->had_content))
2539 case ELEMENT_INCLUDE:
2540 if (!bools_equal (a->d.include.ignore_missing,
2541 b->d.include.ignore_missing))
2545 case ELEMENT_POLICY:
2546 if (a->d.policy.type != b->d.policy.type)
2548 if (a->d.policy.gid_or_uid != b->d.policy.gid_or_uid)
2553 if (strcmp (a->d.limit.name, b->d.limit.name))
2555 if (a->d.limit.value != b->d.limit.value)
2569 lists_of_elements_equal (DBusList *a,
2578 while (ia != NULL && ib != NULL)
2580 if (elements_equal (ia->data, ib->data))
2582 ia = _dbus_list_get_next_link (&a, ia);
2583 ib = _dbus_list_get_next_link (&b, ib);
2586 return ia == NULL && ib == NULL;
2590 lists_of_c_strings_equal (DBusList *a,
2599 while (ia != NULL && ib != NULL)
2601 if (strcmp (ia->data, ib->data))
2603 ia = _dbus_list_get_next_link (&a, ia);
2604 ib = _dbus_list_get_next_link (&b, ib);
2607 return ia == NULL && ib == NULL;
2611 limits_equal (const BusLimits *a,
2615 (a->max_incoming_bytes == b->max_incoming_bytes
2616 || a->max_outgoing_bytes == b->max_outgoing_bytes
2617 || a->max_message_size == b->max_message_size
2618 || a->activation_timeout == b->activation_timeout
2619 || a->auth_timeout == b->auth_timeout
2620 || a->max_completed_connections == b->max_completed_connections
2621 || a->max_incomplete_connections == b->max_incomplete_connections
2622 || a->max_connections_per_user == b->max_connections_per_user
2623 || a->max_pending_activations == b->max_pending_activations
2624 || a->max_services_per_connection == b->max_services_per_connection
2625 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2626 || a->max_replies_per_connection == b->max_replies_per_connection
2627 || a->reply_timeout == b->reply_timeout);
2631 config_parsers_equal (const BusConfigParser *a,
2632 const BusConfigParser *b)
2634 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2637 if (!lists_of_elements_equal (a->stack, b->stack))
2640 if (!strings_equal_or_both_null (a->user, b->user))
2643 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2646 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2649 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2652 /* FIXME: compare policy */
2654 /* FIXME: compare service selinux ID table */
2656 if (! limits_equal (&a->limits, &b->limits))
2659 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2662 if (! bools_equal (a->fork, b->fork))
2665 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2672 all_are_equiv (const DBusString *target_directory)
2674 DBusString filename;
2676 BusConfigParser *first_parser;
2677 BusConfigParser *parser;
2683 first_parser = NULL;
2687 if (!_dbus_string_init (&filename))
2688 _dbus_assert_not_reached ("didn't allocate filename string");
2690 dbus_error_init (&error);
2691 dir = _dbus_directory_open (target_directory, &error);
2694 _dbus_warn ("Could not open %s: %s\n",
2695 _dbus_string_get_const_data (target_directory),
2697 dbus_error_free (&error);
2701 printf ("Comparing equivalent files:\n");
2704 while (_dbus_directory_get_next_file (dir, &filename, &error))
2706 DBusString full_path;
2708 if (!_dbus_string_init (&full_path))
2709 _dbus_assert_not_reached ("couldn't init string");
2711 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2712 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2714 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2715 _dbus_assert_not_reached ("couldn't concat file to dir");
2717 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2719 _dbus_verbose ("Skipping non-.conf file %s\n",
2720 _dbus_string_get_const_data (&filename));
2721 _dbus_string_free (&full_path);
2725 printf (" %s\n", _dbus_string_get_const_data (&filename));
2727 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2728 _dbus_string_free (&full_path);
2732 _dbus_warn ("Could not load file %s: %s\n",
2733 _dbus_string_get_const_data (&full_path),
2735 dbus_error_free (&error);
2738 else if (first_parser == NULL)
2740 first_parser = parser;
2744 equal = config_parsers_equal (first_parser, parser);
2745 bus_config_parser_unref (parser);
2755 _dbus_string_free (&filename);
2757 bus_config_parser_unref (first_parser);
2759 _dbus_directory_close (dir);
2766 process_test_equiv_subdir (const DBusString *test_base_dir,
2769 DBusString test_directory;
2770 DBusString filename;
2779 if (!_dbus_string_init (&test_directory))
2780 _dbus_assert_not_reached ("didn't allocate test_directory");
2782 _dbus_string_init_const (&filename, subdir);
2784 if (!_dbus_string_copy (test_base_dir, 0,
2785 &test_directory, 0))
2786 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2788 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2789 _dbus_assert_not_reached ("couldn't allocate full path");
2791 _dbus_string_free (&filename);
2792 if (!_dbus_string_init (&filename))
2793 _dbus_assert_not_reached ("didn't allocate filename string");
2795 dbus_error_init (&error);
2796 dir = _dbus_directory_open (&test_directory, &error);
2799 _dbus_warn ("Could not open %s: %s\n",
2800 _dbus_string_get_const_data (&test_directory),
2802 dbus_error_free (&error);
2806 while (_dbus_directory_get_next_file (dir, &filename, &error))
2808 DBusString full_path;
2810 /* Skip CVS's magic directories! */
2811 if (_dbus_string_equal_c_str (&filename, "CVS"))
2814 if (!_dbus_string_init (&full_path))
2815 _dbus_assert_not_reached ("couldn't init string");
2817 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2818 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2820 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2821 _dbus_assert_not_reached ("couldn't concat file to dir");
2823 equal = all_are_equiv (&full_path);
2824 _dbus_string_free (&full_path);
2833 _dbus_string_free (&test_directory);
2834 _dbus_string_free (&filename);
2836 _dbus_directory_close (dir);
2843 bus_config_parser_test (const DBusString *test_data_dir)
2845 if (test_data_dir == NULL ||
2846 _dbus_string_get_length (test_data_dir) == 0)
2848 printf ("No test data\n");
2852 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
2855 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
2858 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
2864 #endif /* DBUS_BUILD_TESTS */