1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* config-parser.c XML-library-agnostic configuration file parser
4 * Copyright (C) 2003 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"
27 #include <dbus/dbus-list.h>
28 #include <dbus/dbus-internals.h>
52 /* we ignore policies for unknown groups/users */
66 unsigned int had_content : 1;
72 unsigned int ignore_missing : 1;
78 unsigned long gid_or_uid;
92 * Parser for bus configuration file.
94 struct BusConfigParser
96 int refcount; /**< Reference count */
98 DBusString basedir; /**< Directory we resolve paths relative to */
100 DBusList *stack; /**< stack of Element */
102 char *user; /**< user to run as */
104 char *bus_type; /**< Message bus type */
106 DBusList *listen_on; /**< List of addresses to listen to */
108 DBusList *mechanisms; /**< Auth mechanisms */
110 DBusList *service_dirs; /**< Directories to look for services in */
112 BusPolicy *policy; /**< Security policy */
114 BusLimits limits; /**< Limits */
116 char *pidfile; /**< PID file */
118 DBusList *included_files; /**< Included files stack */
120 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
122 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
126 element_type_to_name (ElementType type)
132 case ELEMENT_BUSCONFIG:
134 case ELEMENT_INCLUDE:
152 case ELEMENT_PIDFILE:
154 case ELEMENT_SERVICEDIR:
156 case ELEMENT_INCLUDEDIR:
162 _dbus_assert_not_reached ("bad element type");
168 push_element (BusConfigParser *parser,
173 _dbus_assert (type != ELEMENT_NONE);
175 e = dbus_new0 (Element, 1);
179 if (!_dbus_list_append (&parser->stack, e))
191 element_free (Element *e)
193 if (e->type == ELEMENT_LIMIT)
194 dbus_free (e->d.limit.name);
200 pop_element (BusConfigParser *parser)
204 e = _dbus_list_pop_last (&parser->stack);
210 peek_element (BusConfigParser *parser)
214 e = _dbus_list_get_last (&parser->stack);
220 top_element_type (BusConfigParser *parser)
224 e = _dbus_list_get_last (&parser->stack);
233 merge_included (BusConfigParser *parser,
234 BusConfigParser *included,
239 if (!bus_policy_merge (parser->policy,
246 if (included->user != NULL)
248 dbus_free (parser->user);
249 parser->user = included->user;
250 included->user = NULL;
253 if (included->bus_type != NULL)
255 dbus_free (parser->bus_type);
256 parser->bus_type = included->bus_type;
257 included->bus_type = NULL;
263 if (included->pidfile != NULL)
265 dbus_free (parser->pidfile);
266 parser->pidfile = included->pidfile;
267 included->pidfile = NULL;
270 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
271 _dbus_list_append_link (&parser->listen_on, link);
273 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
274 _dbus_list_append_link (&parser->mechanisms, link);
276 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
277 _dbus_list_append_link (&parser->service_dirs, link);
283 seen_include (BusConfigParser *parser,
284 const DBusString *file)
288 iter = parser->included_files;
291 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
294 iter = _dbus_list_get_next_link (&parser->included_files, iter);
301 bus_config_parser_new (const DBusString *basedir,
302 dbus_bool_t is_toplevel,
303 const BusConfigParser *parent)
305 BusConfigParser *parser;
307 parser = dbus_new0 (BusConfigParser, 1);
311 parser->is_toplevel = !!is_toplevel;
313 if (!_dbus_string_init (&parser->basedir))
319 if (((parser->policy = bus_policy_new ()) == NULL) ||
320 !_dbus_string_copy (basedir, 0, &parser->basedir, 0))
323 bus_policy_unref (parser->policy);
325 _dbus_string_free (&parser->basedir);
332 /* Initialize the parser's limits from the parent. */
333 parser->limits = parent->limits;
335 /* Use the parent's list of included_files to avoid
336 circular inclusions. */
337 parser->included_files = parent->included_files;
342 /* Make up some numbers! woot! */
343 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
344 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
345 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
347 /* Making this long means the user has to wait longer for an error
348 * message if something screws up, but making it too short means
349 * they might see a false failure.
351 parser->limits.activation_timeout = 25000; /* 25 seconds */
353 /* Making this long risks making a DOS attack easier, but too short
354 * and legitimate auth will fail. If interactive auth (ask user for
355 * password) is allowed, then potentially it has to be quite long.
357 parser->limits.auth_timeout = 30000; /* 30 seconds */
359 parser->limits.max_incomplete_connections = 32;
360 parser->limits.max_connections_per_user = 128;
362 /* Note that max_completed_connections / max_connections_per_user
363 * is the number of users that would have to work together to
364 * DOS all the other users.
366 parser->limits.max_completed_connections = 1024;
368 parser->limits.max_pending_activations = 256;
369 parser->limits.max_services_per_connection = 256;
371 parser->limits.max_match_rules_per_connection = 128;
373 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
374 parser->limits.max_replies_per_connection = 32;
377 parser->refcount = 1;
383 bus_config_parser_ref (BusConfigParser *parser)
385 _dbus_assert (parser->refcount > 0);
387 parser->refcount += 1;
393 bus_config_parser_unref (BusConfigParser *parser)
395 _dbus_assert (parser->refcount > 0);
397 parser->refcount -= 1;
399 if (parser->refcount == 0)
401 while (parser->stack != NULL)
402 pop_element (parser);
404 dbus_free (parser->user);
405 dbus_free (parser->bus_type);
406 dbus_free (parser->pidfile);
408 _dbus_list_foreach (&parser->listen_on,
409 (DBusForeachFunction) dbus_free,
412 _dbus_list_clear (&parser->listen_on);
414 _dbus_list_foreach (&parser->service_dirs,
415 (DBusForeachFunction) dbus_free,
418 _dbus_list_clear (&parser->service_dirs);
420 _dbus_list_foreach (&parser->mechanisms,
421 (DBusForeachFunction) dbus_free,
424 _dbus_list_clear (&parser->mechanisms);
426 _dbus_string_free (&parser->basedir);
429 bus_policy_unref (parser->policy);
436 bus_config_parser_check_doctype (BusConfigParser *parser,
440 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
442 if (strcmp (doctype, "busconfig") != 0)
444 dbus_set_error (error,
446 "Configuration file has the wrong document type %s",
461 locate_attributes (BusConfigParser *parser,
462 const char *element_name,
463 const char **attribute_names,
464 const char **attribute_values,
466 const char *first_attribute_name,
467 const char **first_attribute_retloc,
475 LocateAttr attrs[MAX_ATTRS];
479 _dbus_assert (first_attribute_name != NULL);
480 _dbus_assert (first_attribute_retloc != NULL);
485 attrs[0].name = first_attribute_name;
486 attrs[0].retloc = first_attribute_retloc;
487 *first_attribute_retloc = NULL;
489 va_start (args, first_attribute_retloc);
491 name = va_arg (args, const char*);
492 retloc = va_arg (args, const char**);
496 _dbus_assert (retloc != NULL);
497 _dbus_assert (n_attrs < MAX_ATTRS);
499 attrs[n_attrs].name = name;
500 attrs[n_attrs].retloc = retloc;
504 name = va_arg (args, const char*);
505 retloc = va_arg (args, const char**);
514 while (attribute_names[i])
523 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
525 retloc = attrs[j].retloc;
529 dbus_set_error (error, DBUS_ERROR_FAILED,
530 "Attribute \"%s\" repeated twice on the same <%s> element",
531 attrs[j].name, element_name);
536 *retloc = attribute_values[i];
545 dbus_set_error (error, DBUS_ERROR_FAILED,
546 "Attribute \"%s\" is invalid on <%s> element in this context",
547 attribute_names[i], element_name);
560 check_no_attributes (BusConfigParser *parser,
561 const char *element_name,
562 const char **attribute_names,
563 const char **attribute_values,
566 if (attribute_names[0] != NULL)
568 dbus_set_error (error, DBUS_ERROR_FAILED,
569 "Attribute \"%s\" is invalid on <%s> element in this context",
570 attribute_names[0], element_name);
578 start_busconfig_child (BusConfigParser *parser,
579 const char *element_name,
580 const char **attribute_names,
581 const char **attribute_values,
584 if (strcmp (element_name, "user") == 0)
586 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
589 if (push_element (parser, ELEMENT_USER) == NULL)
597 else if (strcmp (element_name, "type") == 0)
599 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
602 if (push_element (parser, ELEMENT_TYPE) == NULL)
610 else if (strcmp (element_name, "fork") == 0)
612 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
615 if (push_element (parser, ELEMENT_FORK) == NULL)
625 else if (strcmp (element_name, "pidfile") == 0)
627 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
630 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
638 else if (strcmp (element_name, "listen") == 0)
640 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
643 if (push_element (parser, ELEMENT_LISTEN) == NULL)
651 else if (strcmp (element_name, "auth") == 0)
653 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
656 if (push_element (parser, ELEMENT_AUTH) == NULL)
664 else if (strcmp (element_name, "includedir") == 0)
666 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
669 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
677 else if (strcmp (element_name, "servicedir") == 0)
679 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
682 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
690 else if (strcmp (element_name, "include") == 0)
693 const char *ignore_missing;
695 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
701 e->d.include.ignore_missing = FALSE;
703 if (!locate_attributes (parser, "include",
707 "ignore_missing", &ignore_missing,
711 if (ignore_missing != NULL)
713 if (strcmp (ignore_missing, "yes") == 0)
714 e->d.include.ignore_missing = TRUE;
715 else if (strcmp (ignore_missing, "no") == 0)
716 e->d.include.ignore_missing = FALSE;
719 dbus_set_error (error, DBUS_ERROR_FAILED,
720 "ignore_missing attribute must have value \"yes\" or \"no\"");
727 else if (strcmp (element_name, "policy") == 0)
734 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
740 e->d.policy.type = POLICY_IGNORED;
742 if (!locate_attributes (parser, "policy",
752 if (((context && user) ||
753 (context && group)) ||
755 !(context || user || group))
757 dbus_set_error (error, DBUS_ERROR_FAILED,
758 "<policy> element must have exactly one of (context|user|group) attributes");
764 if (strcmp (context, "default") == 0)
766 e->d.policy.type = POLICY_DEFAULT;
768 else if (strcmp (context, "mandatory") == 0)
770 e->d.policy.type = POLICY_MANDATORY;
774 dbus_set_error (error, DBUS_ERROR_FAILED,
775 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
780 else if (user != NULL)
783 _dbus_string_init_const (&username, user);
785 if (_dbus_get_user_id (&username,
786 &e->d.policy.gid_or_uid))
787 e->d.policy.type = POLICY_USER;
789 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
792 else if (group != NULL)
794 DBusString group_name;
795 _dbus_string_init_const (&group_name, group);
797 if (_dbus_get_group_id (&group_name,
798 &e->d.policy.gid_or_uid))
799 e->d.policy.type = POLICY_GROUP;
801 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
806 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
811 else if (strcmp (element_name, "limit") == 0)
816 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
822 if (!locate_attributes (parser, "limit",
832 dbus_set_error (error, DBUS_ERROR_FAILED,
833 "<limit> element must have a \"name\" attribute");
837 e->d.limit.name = _dbus_strdup (name);
838 if (e->d.limit.name == NULL)
848 dbus_set_error (error, DBUS_ERROR_FAILED,
849 "Element <%s> not allowed inside <%s> in configuration file",
850 element_name, "busconfig");
856 append_rule_from_element (BusConfigParser *parser,
857 const char *element_name,
858 const char **attribute_names,
859 const char **attribute_values,
863 const char *send_interface;
864 const char *send_member;
865 const char *send_error;
866 const char *send_destination;
867 const char *send_path;
868 const char *send_type;
869 const char *receive_interface;
870 const char *receive_member;
871 const char *receive_error;
872 const char *receive_sender;
873 const char *receive_path;
874 const char *receive_type;
875 const char *eavesdrop;
876 const char *requested_reply;
882 if (!locate_attributes (parser, element_name,
886 "send_interface", &send_interface,
887 "send_member", &send_member,
888 "send_error", &send_error,
889 "send_destination", &send_destination,
890 "send_path", &send_path,
891 "send_type", &send_type,
892 "receive_interface", &receive_interface,
893 "receive_member", &receive_member,
894 "receive_error", &receive_error,
895 "receive_sender", &receive_sender,
896 "receive_path", &receive_path,
897 "receive_type", &receive_type,
898 "eavesdrop", &eavesdrop,
899 "requested_reply", &requested_reply,
906 if (!(send_interface || send_member || send_error || send_destination ||
907 send_type || send_path ||
908 receive_interface || receive_member || receive_error || receive_sender ||
909 receive_type || receive_path || eavesdrop || requested_reply ||
910 own || user || group))
912 dbus_set_error (error, DBUS_ERROR_FAILED,
913 "Element <%s> must have one or more attributes",
918 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
919 (receive_member && (receive_interface == NULL && receive_path == NULL)))
921 dbus_set_error (error, DBUS_ERROR_FAILED,
922 "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.",
927 /* Allowed combinations of elements are:
929 * base, must be all send or all receive:
935 * base send_ can combine with send_destination, send_path, send_type
936 * base receive_ with receive_sender, receive_path, receive_type, eavesdrop, requested_reply
938 * user, group, own must occur alone
940 * Pretty sure the below stuff is broken, FIXME think about it more.
943 if (((send_interface && send_error) ||
944 (send_interface && receive_interface) ||
945 (send_interface && receive_member) ||
946 (send_interface && receive_error) ||
947 (send_interface && receive_sender) ||
948 (send_interface && eavesdrop) ||
949 (send_interface && requested_reply) ||
950 (send_interface && own) ||
951 (send_interface && user) ||
952 (send_interface && group)) ||
954 ((send_member && send_error) ||
955 (send_member && receive_interface) ||
956 (send_member && receive_member) ||
957 (send_member && receive_error) ||
958 (send_member && receive_sender) ||
959 (send_member && eavesdrop) ||
960 (send_member && requested_reply) ||
961 (send_member && own) ||
962 (send_member && user) ||
963 (send_member && group)) ||
965 ((send_error && receive_interface) ||
966 (send_error && receive_member) ||
967 (send_error && receive_error) ||
968 (send_error && receive_sender) ||
969 (send_error && eavesdrop) ||
970 (send_error && requested_reply) ||
971 (send_error && own) ||
972 (send_error && user) ||
973 (send_error && group)) ||
975 ((send_destination && receive_interface) ||
976 (send_destination && receive_member) ||
977 (send_destination && receive_error) ||
978 (send_destination && receive_sender) ||
979 (send_destination && eavesdrop) ||
980 (send_destination && requested_reply) ||
981 (send_destination && own) ||
982 (send_destination && user) ||
983 (send_destination && group)) ||
985 ((send_type && receive_interface) ||
986 (send_type && receive_member) ||
987 (send_type && receive_error) ||
988 (send_type && receive_sender) ||
989 (send_type && eavesdrop) ||
990 (send_type && requested_reply) ||
991 (send_type && own) ||
992 (send_type && user) ||
993 (send_type && group)) ||
995 ((send_path && receive_interface) ||
996 (send_path && receive_member) ||
997 (send_path && receive_error) ||
998 (send_path && receive_sender) ||
999 (send_path && eavesdrop) ||
1000 (send_path && requested_reply) ||
1001 (send_path && own) ||
1002 (send_path && user) ||
1003 (send_path && group)) ||
1005 ((receive_interface && receive_error) ||
1006 (receive_interface && own) ||
1007 (receive_interface && user) ||
1008 (receive_interface && group)) ||
1010 ((receive_member && receive_error) ||
1011 (receive_member && own) ||
1012 (receive_member && user) ||
1013 (receive_member && group)) ||
1015 ((receive_error && own) ||
1016 (receive_error && user) ||
1017 (receive_error && group)) ||
1019 ((eavesdrop && own) ||
1020 (eavesdrop && user) ||
1021 (eavesdrop && group)) ||
1023 ((requested_reply && own) ||
1024 (requested_reply && user) ||
1025 (requested_reply && group)) ||
1032 dbus_set_error (error, DBUS_ERROR_FAILED,
1033 "Invalid combination of attributes on element <%s>",
1040 /* In BusPolicyRule, NULL represents wildcard.
1041 * In the config file, '*' represents it.
1043 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1045 if (send_interface || send_member || send_error || send_destination ||
1046 send_path || send_type)
1050 if (IS_WILDCARD (send_interface))
1051 send_interface = NULL;
1052 if (IS_WILDCARD (send_member))
1054 if (IS_WILDCARD (send_error))
1056 if (IS_WILDCARD (send_destination))
1057 send_destination = NULL;
1058 if (IS_WILDCARD (send_path))
1060 if (IS_WILDCARD (send_type))
1063 message_type = DBUS_MESSAGE_TYPE_INVALID;
1064 if (send_type != NULL)
1066 message_type = dbus_message_type_from_string (send_type);
1067 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1069 dbus_set_error (error, DBUS_ERROR_FAILED,
1070 "Bad message type \"%s\"",
1076 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1080 rule->d.send.message_type = message_type;
1081 rule->d.send.path = _dbus_strdup (send_path);
1082 rule->d.send.interface = _dbus_strdup (send_interface);
1083 rule->d.send.member = _dbus_strdup (send_member);
1084 rule->d.send.error = _dbus_strdup (send_error);
1085 rule->d.send.destination = _dbus_strdup (send_destination);
1086 if (send_path && rule->d.send.path == NULL)
1088 if (send_interface && rule->d.send.interface == NULL)
1090 if (send_member && rule->d.send.member == NULL)
1092 if (send_error && rule->d.send.error == NULL)
1094 if (send_destination && rule->d.send.destination == NULL)
1097 else if (receive_interface || receive_member || receive_error || receive_sender ||
1098 receive_path || receive_type || eavesdrop || requested_reply)
1102 if (IS_WILDCARD (receive_interface))
1103 receive_interface = NULL;
1104 if (IS_WILDCARD (receive_member))
1105 receive_member = NULL;
1106 if (IS_WILDCARD (receive_error))
1107 receive_error = NULL;
1108 if (IS_WILDCARD (receive_sender))
1109 receive_sender = NULL;
1110 if (IS_WILDCARD (receive_path))
1111 receive_path = NULL;
1112 if (IS_WILDCARD (receive_type))
1113 receive_type = NULL;
1115 message_type = DBUS_MESSAGE_TYPE_INVALID;
1116 if (receive_type != NULL)
1118 message_type = dbus_message_type_from_string (receive_type);
1119 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1121 dbus_set_error (error, DBUS_ERROR_FAILED,
1122 "Bad message type \"%s\"",
1130 !(strcmp (eavesdrop, "true") == 0 ||
1131 strcmp (eavesdrop, "false") == 0))
1133 dbus_set_error (error, DBUS_ERROR_FAILED,
1134 "Bad value \"%s\" for %s attribute, must be true or false",
1135 "eavesdrop", eavesdrop);
1139 if (requested_reply &&
1140 !(strcmp (requested_reply, "true") == 0 ||
1141 strcmp (requested_reply, "false") == 0))
1143 dbus_set_error (error, DBUS_ERROR_FAILED,
1144 "Bad value \"%s\" for %s attribute, must be true or false",
1145 "requested_reply", requested_reply);
1149 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1154 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1156 if (requested_reply)
1157 rule->d.receive.requested_reply = (strcmp (requested_reply, "true") == 0);
1159 rule->d.receive.message_type = message_type;
1160 rule->d.receive.path = _dbus_strdup (receive_path);
1161 rule->d.receive.interface = _dbus_strdup (receive_interface);
1162 rule->d.receive.member = _dbus_strdup (receive_member);
1163 rule->d.receive.error = _dbus_strdup (receive_error);
1164 rule->d.receive.origin = _dbus_strdup (receive_sender);
1166 if (receive_path && rule->d.receive.path == NULL)
1168 if (receive_interface && rule->d.receive.interface == NULL)
1170 if (receive_member && rule->d.receive.member == NULL)
1172 if (receive_error && rule->d.receive.error == NULL)
1174 if (receive_sender && rule->d.receive.origin == NULL)
1179 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1183 if (IS_WILDCARD (own))
1186 rule->d.own.service_name = _dbus_strdup (own);
1187 if (own && rule->d.own.service_name == NULL)
1192 if (IS_WILDCARD (user))
1194 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1198 rule->d.user.uid = DBUS_UID_UNSET;
1202 DBusString username;
1205 _dbus_string_init_const (&username, user);
1207 if (_dbus_get_user_id (&username, &uid))
1209 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1213 rule->d.user.uid = uid;
1217 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1218 user, element_name);
1224 if (IS_WILDCARD (group))
1226 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1230 rule->d.group.gid = DBUS_GID_UNSET;
1234 DBusString groupname;
1237 _dbus_string_init_const (&groupname, group);
1239 if (_dbus_get_user_id (&groupname, &gid))
1241 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1245 rule->d.group.gid = gid;
1249 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1250 group, element_name);
1255 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1261 pe = peek_element (parser);
1262 _dbus_assert (pe != NULL);
1263 _dbus_assert (pe->type == ELEMENT_POLICY);
1265 switch (pe->d.policy.type)
1267 case POLICY_IGNORED:
1268 /* drop the rule on the floor */
1271 case POLICY_DEFAULT:
1272 if (!bus_policy_append_default_rule (parser->policy, rule))
1275 case POLICY_MANDATORY:
1276 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1280 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1282 dbus_set_error (error, DBUS_ERROR_FAILED,
1283 "<%s> rule cannot be per-user because it has bus-global semantics",
1288 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
1293 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1295 dbus_set_error (error, DBUS_ERROR_FAILED,
1296 "<%s> rule cannot be per-group because it has bus-global semantics",
1301 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
1307 bus_policy_rule_unref (rule);
1314 BUS_SET_OOM (error);
1317 bus_policy_rule_unref (rule);
1322 start_policy_child (BusConfigParser *parser,
1323 const char *element_name,
1324 const char **attribute_names,
1325 const char **attribute_values,
1328 if (strcmp (element_name, "allow") == 0)
1330 if (!append_rule_from_element (parser, element_name,
1331 attribute_names, attribute_values,
1335 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1337 BUS_SET_OOM (error);
1343 else if (strcmp (element_name, "deny") == 0)
1345 if (!append_rule_from_element (parser, element_name,
1346 attribute_names, attribute_values,
1350 if (push_element (parser, ELEMENT_DENY) == NULL)
1352 BUS_SET_OOM (error);
1360 dbus_set_error (error, DBUS_ERROR_FAILED,
1361 "Element <%s> not allowed inside <%s> in configuration file",
1362 element_name, "policy");
1368 bus_config_parser_start_element (BusConfigParser *parser,
1369 const char *element_name,
1370 const char **attribute_names,
1371 const char **attribute_values,
1376 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1378 /* printf ("START: %s\n", element_name); */
1380 t = top_element_type (parser);
1382 if (t == ELEMENT_NONE)
1384 if (strcmp (element_name, "busconfig") == 0)
1386 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1389 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1391 BUS_SET_OOM (error);
1399 dbus_set_error (error, DBUS_ERROR_FAILED,
1400 "Unknown element <%s> at root of configuration file",
1405 else if (t == ELEMENT_BUSCONFIG)
1407 return start_busconfig_child (parser, element_name,
1408 attribute_names, attribute_values,
1411 else if (t == ELEMENT_POLICY)
1413 return start_policy_child (parser, element_name,
1414 attribute_names, attribute_values,
1419 dbus_set_error (error, DBUS_ERROR_FAILED,
1420 "Element <%s> is not allowed in this context",
1427 set_limit (BusConfigParser *parser,
1432 dbus_bool_t must_be_positive;
1433 dbus_bool_t must_be_int;
1435 must_be_int = FALSE;
1436 must_be_positive = FALSE;
1438 if (strcmp (name, "max_incoming_bytes") == 0)
1440 must_be_positive = TRUE;
1441 parser->limits.max_incoming_bytes = value;
1443 else if (strcmp (name, "max_outgoing_bytes") == 0)
1445 must_be_positive = TRUE;
1446 parser->limits.max_outgoing_bytes = value;
1448 else if (strcmp (name, "max_message_size") == 0)
1450 must_be_positive = TRUE;
1451 parser->limits.max_message_size = value;
1453 else if (strcmp (name, "activation_timeout") == 0)
1455 must_be_positive = TRUE;
1457 parser->limits.activation_timeout = value;
1459 else if (strcmp (name, "auth_timeout") == 0)
1461 must_be_positive = TRUE;
1463 parser->limits.auth_timeout = value;
1465 else if (strcmp (name, "reply_timeout") == 0)
1467 must_be_positive = TRUE;
1469 parser->limits.reply_timeout = value;
1471 else if (strcmp (name, "max_completed_connections") == 0)
1473 must_be_positive = TRUE;
1475 parser->limits.max_completed_connections = value;
1477 else if (strcmp (name, "max_incomplete_connections") == 0)
1479 must_be_positive = TRUE;
1481 parser->limits.max_incomplete_connections = value;
1483 else if (strcmp (name, "max_connections_per_user") == 0)
1485 must_be_positive = TRUE;
1487 parser->limits.max_connections_per_user = value;
1489 else if (strcmp (name, "max_pending_activations") == 0)
1491 must_be_positive = TRUE;
1493 parser->limits.max_pending_activations = value;
1495 else if (strcmp (name, "max_services_per_connection") == 0)
1497 must_be_positive = TRUE;
1499 parser->limits.max_services_per_connection = value;
1501 else if (strcmp (name, "max_replies_per_connection") == 0)
1503 must_be_positive = TRUE;
1505 parser->limits.max_replies_per_connection = value;
1509 dbus_set_error (error, DBUS_ERROR_FAILED,
1510 "There is no limit called \"%s\"\n",
1515 if (must_be_positive && value < 0)
1517 dbus_set_error (error, DBUS_ERROR_FAILED,
1518 "<limit name=\"%s\"> must be a positive number\n",
1524 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1526 dbus_set_error (error, DBUS_ERROR_FAILED,
1527 "<limit name=\"%s\"> value is too large\n",
1536 bus_config_parser_end_element (BusConfigParser *parser,
1537 const char *element_name,
1544 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1546 /* printf ("END: %s\n", element_name); */
1548 t = top_element_type (parser);
1550 if (t == ELEMENT_NONE)
1552 /* should probably be an assertion failure but
1553 * being paranoid about XML parsers
1555 dbus_set_error (error, DBUS_ERROR_FAILED,
1556 "XML parser ended element with no element on the stack");
1560 n = element_type_to_name (t);
1561 _dbus_assert (n != NULL);
1562 if (strcmp (n, element_name) != 0)
1564 /* should probably be an assertion failure but
1565 * being paranoid about XML parsers
1567 dbus_set_error (error, DBUS_ERROR_FAILED,
1568 "XML element <%s> ended but topmost element on the stack was <%s>",
1573 e = peek_element (parser);
1574 _dbus_assert (e != NULL);
1579 _dbus_assert_not_reached ("element in stack has no type");
1582 case ELEMENT_INCLUDE:
1585 case ELEMENT_LISTEN:
1586 case ELEMENT_PIDFILE:
1588 case ELEMENT_SERVICEDIR:
1589 case ELEMENT_INCLUDEDIR:
1591 if (!e->had_content)
1593 dbus_set_error (error, DBUS_ERROR_FAILED,
1594 "XML element <%s> was expected to have content inside it",
1595 element_type_to_name (e->type));
1599 if (e->type == ELEMENT_LIMIT)
1601 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1607 case ELEMENT_BUSCONFIG:
1608 case ELEMENT_POLICY:
1615 pop_element (parser);
1621 all_whitespace (const DBusString *str)
1625 _dbus_string_skip_white (str, 0, &i);
1627 return i == _dbus_string_get_length (str);
1631 make_full_path (const DBusString *basedir,
1632 const DBusString *filename,
1633 DBusString *full_path)
1635 if (_dbus_path_is_absolute (filename))
1637 return _dbus_string_copy (filename, 0, full_path, 0);
1641 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1644 if (!_dbus_concat_dir_and_file (full_path, filename))
1652 include_file (BusConfigParser *parser,
1653 const DBusString *filename,
1654 dbus_bool_t ignore_missing,
1657 /* FIXME good test case for this would load each config file in the
1658 * test suite both alone, and as an include, and check
1659 * that the result is the same
1661 BusConfigParser *included;
1662 const char *filename_str;
1663 DBusError tmp_error;
1665 dbus_error_init (&tmp_error);
1667 filename_str = _dbus_string_get_const_data (filename);
1669 /* Check to make sure this file hasn't already been included. */
1670 if (seen_include (parser, filename))
1672 dbus_set_error (error, DBUS_ERROR_FAILED,
1673 "Circular inclusion of file '%s'",
1678 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
1680 BUS_SET_OOM (error);
1684 /* Since parser is passed in as the parent, included
1685 inherits parser's limits. */
1686 included = bus_config_load (filename, FALSE, parser, &tmp_error);
1688 _dbus_list_pop_last (&parser->included_files);
1690 if (included == NULL)
1692 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1694 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1697 dbus_error_free (&tmp_error);
1702 dbus_move_error (&tmp_error, error);
1708 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1710 if (!merge_included (parser, included, error))
1712 bus_config_parser_unref (included);
1716 /* Copy included's limits back to parser. */
1717 parser->limits = included->limits;
1719 bus_config_parser_unref (included);
1725 include_dir (BusConfigParser *parser,
1726 const DBusString *dirname,
1729 DBusString filename;
1731 DBusError tmp_error;
1734 if (!_dbus_string_init (&filename))
1736 BUS_SET_OOM (error);
1742 dir = _dbus_directory_open (dirname, error);
1747 dbus_error_init (&tmp_error);
1748 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1750 DBusString full_path;
1752 if (!_dbus_string_init (&full_path))
1754 BUS_SET_OOM (error);
1758 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1760 BUS_SET_OOM (error);
1761 _dbus_string_free (&full_path);
1765 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1767 BUS_SET_OOM (error);
1768 _dbus_string_free (&full_path);
1772 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1774 if (!include_file (parser, &full_path, TRUE, error))
1776 _dbus_string_free (&full_path);
1781 _dbus_string_free (&full_path);
1784 if (dbus_error_is_set (&tmp_error))
1786 dbus_move_error (&tmp_error, error);
1793 _dbus_string_free (&filename);
1796 _dbus_directory_close (dir);
1802 bus_config_parser_content (BusConfigParser *parser,
1803 const DBusString *content,
1808 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1814 _dbus_string_get_const_data (content, &c_str);
1816 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1820 e = peek_element (parser);
1823 dbus_set_error (error, DBUS_ERROR_FAILED,
1824 "Text content outside of any XML element in configuration file");
1827 else if (e->had_content)
1829 _dbus_assert_not_reached ("Element had multiple content blocks");
1833 switch (top_element_type (parser))
1836 _dbus_assert_not_reached ("element at top of stack has no type");
1839 case ELEMENT_BUSCONFIG:
1840 case ELEMENT_POLICY:
1844 if (all_whitespace (content))
1848 dbus_set_error (error, DBUS_ERROR_FAILED,
1849 "No text content expected inside XML element %s in configuration file",
1850 element_type_to_name (top_element_type (parser)));
1854 case ELEMENT_PIDFILE:
1858 e->had_content = TRUE;
1860 if (!_dbus_string_copy_data (content, &s))
1863 dbus_free (parser->pidfile);
1864 parser->pidfile = s;
1868 case ELEMENT_INCLUDE:
1870 DBusString full_path;
1872 e->had_content = TRUE;
1874 if (!_dbus_string_init (&full_path))
1877 if (!make_full_path (&parser->basedir, content, &full_path))
1879 _dbus_string_free (&full_path);
1883 if (!include_file (parser, &full_path,
1884 e->d.include.ignore_missing, error))
1886 _dbus_string_free (&full_path);
1890 _dbus_string_free (&full_path);
1894 case ELEMENT_INCLUDEDIR:
1896 DBusString full_path;
1898 e->had_content = TRUE;
1900 if (!_dbus_string_init (&full_path))
1903 if (!make_full_path (&parser->basedir, content, &full_path))
1905 _dbus_string_free (&full_path);
1909 if (!include_dir (parser, &full_path, error))
1911 _dbus_string_free (&full_path);
1915 _dbus_string_free (&full_path);
1923 e->had_content = TRUE;
1925 if (!_dbus_string_copy_data (content, &s))
1928 dbus_free (parser->user);
1937 e->had_content = TRUE;
1939 if (!_dbus_string_copy_data (content, &s))
1942 dbus_free (parser->bus_type);
1943 parser->bus_type = s;
1947 case ELEMENT_LISTEN:
1951 e->had_content = TRUE;
1953 if (!_dbus_string_copy_data (content, &s))
1956 if (!_dbus_list_append (&parser->listen_on,
1969 e->had_content = TRUE;
1971 if (!_dbus_string_copy_data (content, &s))
1974 if (!_dbus_list_append (&parser->mechanisms,
1983 case ELEMENT_SERVICEDIR:
1986 DBusString full_path;
1988 e->had_content = TRUE;
1990 if (!_dbus_string_init (&full_path))
1993 if (!make_full_path (&parser->basedir, content, &full_path))
1995 _dbus_string_free (&full_path);
1999 if (!_dbus_string_copy_data (&full_path, &s))
2001 _dbus_string_free (&full_path);
2005 if (!_dbus_list_append (&parser->service_dirs, s))
2007 _dbus_string_free (&full_path);
2012 _dbus_string_free (&full_path);
2020 e->had_content = TRUE;
2023 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2025 dbus_set_error (error, DBUS_ERROR_FAILED,
2026 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2031 e->d.limit.value = val;
2033 _dbus_verbose ("Loaded value %ld for limit %s\n",
2040 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2044 BUS_SET_OOM (error);
2049 bus_config_parser_finished (BusConfigParser *parser,
2052 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2054 if (parser->stack != NULL)
2056 dbus_set_error (error, DBUS_ERROR_FAILED,
2057 "Element <%s> was not closed in configuration file",
2058 element_type_to_name (top_element_type (parser)));
2063 if (parser->is_toplevel && parser->listen_on == NULL)
2065 dbus_set_error (error, DBUS_ERROR_FAILED,
2066 "Configuration file needs one or more <listen> elements giving addresses");
2074 bus_config_parser_get_user (BusConfigParser *parser)
2076 return parser->user;
2080 bus_config_parser_get_type (BusConfigParser *parser)
2082 return parser->bus_type;
2086 bus_config_parser_get_addresses (BusConfigParser *parser)
2088 return &parser->listen_on;
2092 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2094 return &parser->mechanisms;
2098 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2100 return &parser->service_dirs;
2104 bus_config_parser_get_fork (BusConfigParser *parser)
2106 return parser->fork;
2110 bus_config_parser_get_pidfile (BusConfigParser *parser)
2112 return parser->pidfile;
2116 bus_config_parser_steal_policy (BusConfigParser *parser)
2120 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2122 policy = parser->policy;
2124 parser->policy = NULL;
2129 /* Overwrite any limits that were set in the configuration file */
2131 bus_config_parser_get_limits (BusConfigParser *parser,
2134 *limits = parser->limits;
2137 #ifdef DBUS_BUILD_TESTS
2148 do_load (const DBusString *full_path,
2150 dbus_bool_t oom_possible)
2152 BusConfigParser *parser;
2155 dbus_error_init (&error);
2157 parser = bus_config_load (full_path, TRUE, NULL, &error);
2160 _DBUS_ASSERT_ERROR_IS_SET (&error);
2163 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2165 _dbus_verbose ("Failed to load valid file due to OOM\n");
2166 dbus_error_free (&error);
2169 else if (validity == VALID)
2171 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2174 dbus_error_free (&error);
2179 dbus_error_free (&error);
2185 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2187 bus_config_parser_unref (parser);
2189 if (validity == INVALID)
2191 _dbus_warn ("Accepted invalid file\n");
2201 const DBusString *full_path;
2206 check_loader_oom_func (void *data)
2208 LoaderOomData *d = data;
2210 return do_load (d->full_path, d->validity, TRUE);
2214 process_test_valid_subdir (const DBusString *test_base_dir,
2218 DBusString test_directory;
2219 DBusString filename;
2227 if (!_dbus_string_init (&test_directory))
2228 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2230 _dbus_string_init_const (&filename, subdir);
2232 if (!_dbus_string_copy (test_base_dir, 0,
2233 &test_directory, 0))
2234 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2236 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2237 _dbus_assert_not_reached ("couldn't allocate full path");
2239 _dbus_string_free (&filename);
2240 if (!_dbus_string_init (&filename))
2241 _dbus_assert_not_reached ("didn't allocate filename string\n");
2243 dbus_error_init (&error);
2244 dir = _dbus_directory_open (&test_directory, &error);
2247 _dbus_warn ("Could not open %s: %s\n",
2248 _dbus_string_get_const_data (&test_directory),
2250 dbus_error_free (&error);
2254 if (validity == VALID)
2255 printf ("Testing valid files:\n");
2256 else if (validity == INVALID)
2257 printf ("Testing invalid files:\n");
2259 printf ("Testing unknown files:\n");
2262 while (_dbus_directory_get_next_file (dir, &filename, &error))
2264 DBusString full_path;
2267 if (!_dbus_string_init (&full_path))
2268 _dbus_assert_not_reached ("couldn't init string");
2270 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2271 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2273 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2274 _dbus_assert_not_reached ("couldn't concat file to dir");
2276 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2278 _dbus_verbose ("Skipping non-.conf file %s\n",
2279 _dbus_string_get_const_data (&filename));
2280 _dbus_string_free (&full_path);
2284 printf (" %s\n", _dbus_string_get_const_data (&filename));
2286 _dbus_verbose (" expecting %s\n",
2287 validity == VALID ? "valid" :
2288 (validity == INVALID ? "invalid" :
2289 (validity == UNKNOWN ? "unknown" : "???")));
2291 d.full_path = &full_path;
2292 d.validity = validity;
2293 if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d))
2294 _dbus_assert_not_reached ("test failed");
2296 _dbus_string_free (&full_path);
2299 if (dbus_error_is_set (&error))
2301 _dbus_warn ("Could not get next file in %s: %s\n",
2302 _dbus_string_get_const_data (&test_directory),
2304 dbus_error_free (&error);
2313 _dbus_directory_close (dir);
2314 _dbus_string_free (&test_directory);
2315 _dbus_string_free (&filename);
2321 bools_equal (dbus_bool_t a,
2328 strings_equal_or_both_null (const char *a,
2331 if (a == NULL || b == NULL)
2334 return !strcmp (a, b);
2338 elements_equal (const Element *a,
2341 if (a->type != b->type)
2344 if (!bools_equal (a->had_content, b->had_content))
2350 case ELEMENT_INCLUDE:
2351 if (!bools_equal (a->d.include.ignore_missing,
2352 b->d.include.ignore_missing))
2356 case ELEMENT_POLICY:
2357 if (a->d.policy.type != b->d.policy.type)
2359 if (a->d.policy.gid_or_uid != b->d.policy.gid_or_uid)
2364 if (strcmp (a->d.limit.name, b->d.limit.name))
2366 if (a->d.limit.value != b->d.limit.value)
2380 lists_of_elements_equal (DBusList *a,
2389 while (ia != NULL && ib != NULL)
2391 if (elements_equal (ia->data, ib->data))
2393 ia = _dbus_list_get_next_link (&a, ia);
2394 ib = _dbus_list_get_next_link (&b, ib);
2397 return ia == NULL && ib == NULL;
2401 lists_of_c_strings_equal (DBusList *a,
2410 while (ia != NULL && ib != NULL)
2412 if (strcmp (ia->data, ib->data))
2414 ia = _dbus_list_get_next_link (&a, ia);
2415 ib = _dbus_list_get_next_link (&b, ib);
2418 return ia == NULL && ib == NULL;
2422 limits_equal (const BusLimits *a,
2426 (a->max_incoming_bytes == b->max_incoming_bytes
2427 || a->max_outgoing_bytes == b->max_outgoing_bytes
2428 || a->max_message_size == b->max_message_size
2429 || a->activation_timeout == b->activation_timeout
2430 || a->auth_timeout == b->auth_timeout
2431 || a->max_completed_connections == b->max_completed_connections
2432 || a->max_incomplete_connections == b->max_incomplete_connections
2433 || a->max_connections_per_user == b->max_connections_per_user
2434 || a->max_pending_activations == b->max_pending_activations
2435 || a->max_services_per_connection == b->max_services_per_connection
2436 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2437 || a->max_replies_per_connection == b->max_replies_per_connection
2438 || a->reply_timeout == b->reply_timeout);
2442 config_parsers_equal (const BusConfigParser *a,
2443 const BusConfigParser *b)
2445 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2448 if (!lists_of_elements_equal (a->stack, b->stack))
2451 if (!strings_equal_or_both_null (a->user, b->user))
2454 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2457 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2460 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2463 /* FIXME: compare policy */
2465 if (! limits_equal (&a->limits, &b->limits))
2468 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2471 if (! bools_equal (a->fork, b->fork))
2474 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2481 all_are_equiv (const DBusString *target_directory)
2483 DBusString filename;
2485 BusConfigParser *first_parser;
2486 BusConfigParser *parser;
2492 first_parser = NULL;
2496 if (!_dbus_string_init (&filename))
2497 _dbus_assert_not_reached ("didn't allocate filename string");
2499 dbus_error_init (&error);
2500 dir = _dbus_directory_open (target_directory, &error);
2503 _dbus_warn ("Could not open %s: %s\n",
2504 _dbus_string_get_const_data (target_directory),
2506 dbus_error_free (&error);
2510 printf ("Comparing equivalent files:\n");
2513 while (_dbus_directory_get_next_file (dir, &filename, &error))
2515 DBusString full_path;
2517 if (!_dbus_string_init (&full_path))
2518 _dbus_assert_not_reached ("couldn't init string");
2520 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2521 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2523 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2524 _dbus_assert_not_reached ("couldn't concat file to dir");
2526 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2528 _dbus_verbose ("Skipping non-.conf file %s\n",
2529 _dbus_string_get_const_data (&filename));
2530 _dbus_string_free (&full_path);
2534 printf (" %s\n", _dbus_string_get_const_data (&filename));
2536 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2537 _dbus_string_free (&full_path);
2541 _dbus_warn ("Could not load file %s: %s\n",
2542 _dbus_string_get_const_data (&full_path),
2544 dbus_error_free (&error);
2547 else if (first_parser == NULL)
2549 first_parser = parser;
2553 equal = config_parsers_equal (first_parser, parser);
2554 bus_config_parser_unref (parser);
2564 _dbus_string_free (&filename);
2566 bus_config_parser_unref (first_parser);
2568 _dbus_directory_close (dir);
2575 process_test_equiv_subdir (const DBusString *test_base_dir,
2578 DBusString test_directory;
2579 DBusString filename;
2588 if (!_dbus_string_init (&test_directory))
2589 _dbus_assert_not_reached ("didn't allocate test_directory");
2591 _dbus_string_init_const (&filename, subdir);
2593 if (!_dbus_string_copy (test_base_dir, 0,
2594 &test_directory, 0))
2595 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2597 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2598 _dbus_assert_not_reached ("couldn't allocate full path");
2600 _dbus_string_free (&filename);
2601 if (!_dbus_string_init (&filename))
2602 _dbus_assert_not_reached ("didn't allocate filename string");
2604 dbus_error_init (&error);
2605 dir = _dbus_directory_open (&test_directory, &error);
2608 _dbus_warn ("Could not open %s: %s\n",
2609 _dbus_string_get_const_data (&test_directory),
2611 dbus_error_free (&error);
2615 while (_dbus_directory_get_next_file (dir, &filename, &error))
2617 DBusString full_path;
2619 /* Skip CVS's magic directories! */
2620 if (_dbus_string_equal_c_str (&filename, "CVS"))
2623 if (!_dbus_string_init (&full_path))
2624 _dbus_assert_not_reached ("couldn't init string");
2626 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2627 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2629 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2630 _dbus_assert_not_reached ("couldn't concat file to dir");
2632 equal = all_are_equiv (&full_path);
2633 _dbus_string_free (&full_path);
2642 _dbus_string_free (&test_directory);
2643 _dbus_string_free (&filename);
2645 _dbus_directory_close (dir);
2652 bus_config_parser_test (const DBusString *test_data_dir)
2654 if (test_data_dir == NULL ||
2655 _dbus_string_get_length (test_data_dir) == 0)
2657 printf ("No test data\n");
2661 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
2664 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
2667 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
2673 #endif /* DBUS_BUILD_TESTS */