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 */
70 unsigned int had_content : 1;
76 unsigned int ignore_missing : 1;
77 unsigned int if_selinux_enabled : 1;
78 unsigned int selinux_root_relative : 1;
84 unsigned long gid_uid_or_at_console;
98 * Parser for bus configuration file.
100 struct BusConfigParser
102 int refcount; /**< Reference count */
104 DBusString basedir; /**< Directory we resolve paths relative to */
106 DBusList *stack; /**< stack of Element */
108 char *user; /**< user to run as */
110 char *bus_type; /**< Message bus type */
112 DBusList *listen_on; /**< List of addresses to listen to */
114 DBusList *mechanisms; /**< Auth mechanisms */
116 DBusList *service_dirs; /**< Directories to look for services in */
118 DBusList *conf_dirs; /**< Directories to look for policy configuration in */
120 BusPolicy *policy; /**< Security policy */
122 BusLimits limits; /**< Limits */
124 char *pidfile; /**< PID file */
126 DBusList *included_files; /**< Included files stack */
128 DBusHashTable *service_context_table; /**< Map service names to SELinux contexts */
130 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
132 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
136 element_type_to_name (ElementType type)
142 case ELEMENT_BUSCONFIG:
144 case ELEMENT_INCLUDE:
162 case ELEMENT_PIDFILE:
164 case ELEMENT_SERVICEDIR:
166 case ELEMENT_INCLUDEDIR:
170 case ELEMENT_SELINUX:
172 case ELEMENT_ASSOCIATE:
176 _dbus_assert_not_reached ("bad element type");
182 push_element (BusConfigParser *parser,
187 _dbus_assert (type != ELEMENT_NONE);
189 e = dbus_new0 (Element, 1);
193 if (!_dbus_list_append (&parser->stack, e))
205 element_free (Element *e)
207 if (e->type == ELEMENT_LIMIT)
208 dbus_free (e->d.limit.name);
214 pop_element (BusConfigParser *parser)
218 e = _dbus_list_pop_last (&parser->stack);
224 peek_element (BusConfigParser *parser)
228 e = _dbus_list_get_last (&parser->stack);
234 top_element_type (BusConfigParser *parser)
238 e = _dbus_list_get_last (&parser->stack);
247 merge_service_context_hash (DBusHashTable *dest,
257 _dbus_hash_iter_init (from, &iter);
258 while (_dbus_hash_iter_next (&iter))
260 const char *service = _dbus_hash_iter_get_string_key (&iter);
261 const char *context = _dbus_hash_iter_get_value (&iter);
263 service_copy = _dbus_strdup (service);
264 if (service_copy == NULL)
266 context_copy = _dbus_strdup (context);
267 if (context_copy == NULL)
270 if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
281 dbus_free (service_copy);
284 dbus_free (context_copy);
290 merge_included (BusConfigParser *parser,
291 BusConfigParser *included,
296 if (!bus_policy_merge (parser->policy,
303 if (!merge_service_context_hash (parser->service_context_table,
304 included->service_context_table))
310 if (included->user != NULL)
312 dbus_free (parser->user);
313 parser->user = included->user;
314 included->user = NULL;
317 if (included->bus_type != NULL)
319 dbus_free (parser->bus_type);
320 parser->bus_type = included->bus_type;
321 included->bus_type = NULL;
327 if (included->pidfile != NULL)
329 dbus_free (parser->pidfile);
330 parser->pidfile = included->pidfile;
331 included->pidfile = NULL;
334 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
335 _dbus_list_append_link (&parser->listen_on, link);
337 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
338 _dbus_list_append_link (&parser->mechanisms, link);
340 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
341 _dbus_list_append_link (&parser->service_dirs, link);
343 while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
344 _dbus_list_append_link (&parser->conf_dirs, link);
350 seen_include (BusConfigParser *parser,
351 const DBusString *file)
355 iter = parser->included_files;
358 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
361 iter = _dbus_list_get_next_link (&parser->included_files, iter);
368 bus_config_parser_new (const DBusString *basedir,
369 dbus_bool_t is_toplevel,
370 const BusConfigParser *parent)
372 BusConfigParser *parser;
374 parser = dbus_new0 (BusConfigParser, 1);
378 parser->is_toplevel = !!is_toplevel;
380 if (!_dbus_string_init (&parser->basedir))
386 if (((parser->policy = bus_policy_new ()) == NULL) ||
387 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
388 ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
390 dbus_free)) == NULL))
393 bus_policy_unref (parser->policy);
395 _dbus_string_free (&parser->basedir);
403 /* Initialize the parser's limits from the parent. */
404 parser->limits = parent->limits;
406 /* Use the parent's list of included_files to avoid
407 circular inclusions. */
408 parser->included_files = parent->included_files;
413 /* Make up some numbers! woot! */
414 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
415 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
416 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
418 /* Making this long means the user has to wait longer for an error
419 * message if something screws up, but making it too short means
420 * they might see a false failure.
422 parser->limits.activation_timeout = 25000; /* 25 seconds */
424 /* Making this long risks making a DOS attack easier, but too short
425 * and legitimate auth will fail. If interactive auth (ask user for
426 * password) is allowed, then potentially it has to be quite long.
428 parser->limits.auth_timeout = 30000; /* 30 seconds */
430 parser->limits.max_incomplete_connections = 32;
431 parser->limits.max_connections_per_user = 128;
433 /* Note that max_completed_connections / max_connections_per_user
434 * is the number of users that would have to work together to
435 * DOS all the other users.
437 parser->limits.max_completed_connections = 1024;
439 parser->limits.max_pending_activations = 256;
440 parser->limits.max_services_per_connection = 256;
442 parser->limits.max_match_rules_per_connection = 512;
444 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
445 parser->limits.max_replies_per_connection = 32;
448 parser->refcount = 1;
454 bus_config_parser_ref (BusConfigParser *parser)
456 _dbus_assert (parser->refcount > 0);
458 parser->refcount += 1;
464 bus_config_parser_unref (BusConfigParser *parser)
466 _dbus_assert (parser->refcount > 0);
468 parser->refcount -= 1;
470 if (parser->refcount == 0)
472 while (parser->stack != NULL)
473 pop_element (parser);
475 dbus_free (parser->user);
476 dbus_free (parser->bus_type);
477 dbus_free (parser->pidfile);
479 _dbus_list_foreach (&parser->listen_on,
480 (DBusForeachFunction) dbus_free,
483 _dbus_list_clear (&parser->listen_on);
485 _dbus_list_foreach (&parser->service_dirs,
486 (DBusForeachFunction) dbus_free,
489 _dbus_list_clear (&parser->service_dirs);
491 _dbus_list_foreach (&parser->conf_dirs,
492 (DBusForeachFunction) dbus_free,
495 _dbus_list_clear (&parser->conf_dirs);
497 _dbus_list_foreach (&parser->mechanisms,
498 (DBusForeachFunction) dbus_free,
501 _dbus_list_clear (&parser->mechanisms);
503 _dbus_string_free (&parser->basedir);
506 bus_policy_unref (parser->policy);
508 if (parser->service_context_table)
509 _dbus_hash_table_unref (parser->service_context_table);
516 bus_config_parser_check_doctype (BusConfigParser *parser,
520 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
522 if (strcmp (doctype, "busconfig") != 0)
524 dbus_set_error (error,
526 "Configuration file has the wrong document type %s",
541 locate_attributes (BusConfigParser *parser,
542 const char *element_name,
543 const char **attribute_names,
544 const char **attribute_values,
546 const char *first_attribute_name,
547 const char **first_attribute_retloc,
555 LocateAttr attrs[MAX_ATTRS];
559 _dbus_assert (first_attribute_name != NULL);
560 _dbus_assert (first_attribute_retloc != NULL);
565 attrs[0].name = first_attribute_name;
566 attrs[0].retloc = first_attribute_retloc;
567 *first_attribute_retloc = NULL;
569 va_start (args, first_attribute_retloc);
571 name = va_arg (args, const char*);
572 retloc = va_arg (args, const char**);
576 _dbus_assert (retloc != NULL);
577 _dbus_assert (n_attrs < MAX_ATTRS);
579 attrs[n_attrs].name = name;
580 attrs[n_attrs].retloc = retloc;
584 name = va_arg (args, const char*);
585 retloc = va_arg (args, const char**);
594 while (attribute_names[i])
603 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
605 retloc = attrs[j].retloc;
609 dbus_set_error (error, DBUS_ERROR_FAILED,
610 "Attribute \"%s\" repeated twice on the same <%s> element",
611 attrs[j].name, element_name);
616 *retloc = attribute_values[i];
625 dbus_set_error (error, DBUS_ERROR_FAILED,
626 "Attribute \"%s\" is invalid on <%s> element in this context",
627 attribute_names[i], element_name);
640 check_no_attributes (BusConfigParser *parser,
641 const char *element_name,
642 const char **attribute_names,
643 const char **attribute_values,
646 if (attribute_names[0] != NULL)
648 dbus_set_error (error, DBUS_ERROR_FAILED,
649 "Attribute \"%s\" is invalid on <%s> element in this context",
650 attribute_names[0], element_name);
658 start_busconfig_child (BusConfigParser *parser,
659 const char *element_name,
660 const char **attribute_names,
661 const char **attribute_values,
664 if (strcmp (element_name, "user") == 0)
666 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
669 if (push_element (parser, ELEMENT_USER) == NULL)
677 else if (strcmp (element_name, "type") == 0)
679 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
682 if (push_element (parser, ELEMENT_TYPE) == NULL)
690 else if (strcmp (element_name, "fork") == 0)
692 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
695 if (push_element (parser, ELEMENT_FORK) == NULL)
705 else if (strcmp (element_name, "pidfile") == 0)
707 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
710 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
718 else if (strcmp (element_name, "listen") == 0)
720 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
723 if (push_element (parser, ELEMENT_LISTEN) == NULL)
731 else if (strcmp (element_name, "auth") == 0)
733 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
736 if (push_element (parser, ELEMENT_AUTH) == NULL)
744 else if (strcmp (element_name, "includedir") == 0)
746 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
749 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
757 else if (strcmp (element_name, "servicedir") == 0)
759 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
762 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
770 else if (strcmp (element_name, "include") == 0)
773 const char *if_selinux_enabled;
774 const char *ignore_missing;
775 const char *selinux_root_relative;
777 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
783 e->d.include.ignore_missing = FALSE;
784 e->d.include.if_selinux_enabled = FALSE;
785 e->d.include.selinux_root_relative = FALSE;
787 if (!locate_attributes (parser, "include",
791 "ignore_missing", &ignore_missing,
792 "if_selinux_enabled", &if_selinux_enabled,
793 "selinux_root_relative", &selinux_root_relative,
797 if (ignore_missing != NULL)
799 if (strcmp (ignore_missing, "yes") == 0)
800 e->d.include.ignore_missing = TRUE;
801 else if (strcmp (ignore_missing, "no") == 0)
802 e->d.include.ignore_missing = FALSE;
805 dbus_set_error (error, DBUS_ERROR_FAILED,
806 "ignore_missing attribute must have value \"yes\" or \"no\"");
811 if (if_selinux_enabled != NULL)
813 if (strcmp (if_selinux_enabled, "yes") == 0)
814 e->d.include.if_selinux_enabled = TRUE;
815 else if (strcmp (if_selinux_enabled, "no") == 0)
816 e->d.include.if_selinux_enabled = FALSE;
819 dbus_set_error (error, DBUS_ERROR_FAILED,
820 "if_selinux_enabled attribute must have value"
821 " \"yes\" or \"no\"");
826 if (selinux_root_relative != NULL)
828 if (strcmp (selinux_root_relative, "yes") == 0)
829 e->d.include.selinux_root_relative = TRUE;
830 else if (strcmp (selinux_root_relative, "no") == 0)
831 e->d.include.selinux_root_relative = FALSE;
834 dbus_set_error (error, DBUS_ERROR_FAILED,
835 "selinux_root_relative attribute must have value"
836 " \"yes\" or \"no\"");
843 else if (strcmp (element_name, "policy") == 0)
849 const char *at_console;
851 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
857 e->d.policy.type = POLICY_IGNORED;
859 if (!locate_attributes (parser, "policy",
866 "at_console", &at_console,
870 if (((context && user) ||
871 (context && group) ||
872 (context && at_console)) ||
874 (user && at_console)) ||
875 (group && at_console) ||
876 !(context || user || group || at_console))
878 dbus_set_error (error, DBUS_ERROR_FAILED,
879 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
885 if (strcmp (context, "default") == 0)
887 e->d.policy.type = POLICY_DEFAULT;
889 else if (strcmp (context, "mandatory") == 0)
891 e->d.policy.type = POLICY_MANDATORY;
895 dbus_set_error (error, DBUS_ERROR_FAILED,
896 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
901 else if (user != NULL)
904 _dbus_string_init_const (&username, user);
906 if (_dbus_get_user_id (&username,
907 &e->d.policy.gid_uid_or_at_console))
908 e->d.policy.type = POLICY_USER;
910 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
913 else if (group != NULL)
915 DBusString group_name;
916 _dbus_string_init_const (&group_name, group);
918 if (_dbus_get_group_id (&group_name,
919 &e->d.policy.gid_uid_or_at_console))
920 e->d.policy.type = POLICY_GROUP;
922 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
925 else if (at_console != NULL)
928 t = (strcmp (at_console, "true") == 0);
929 if (t || strcmp (at_console, "false") == 0)
931 e->d.policy.gid_uid_or_at_console = t;
932 e->d.policy.type = POLICY_CONSOLE;
936 dbus_set_error (error, DBUS_ERROR_FAILED,
937 "Unknown value \"%s\" for at_console in message bus configuration file",
945 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
950 else if (strcmp (element_name, "limit") == 0)
955 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
961 if (!locate_attributes (parser, "limit",
971 dbus_set_error (error, DBUS_ERROR_FAILED,
972 "<limit> element must have a \"name\" attribute");
976 e->d.limit.name = _dbus_strdup (name);
977 if (e->d.limit.name == NULL)
985 else if (strcmp (element_name, "selinux") == 0)
987 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
990 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1000 dbus_set_error (error, DBUS_ERROR_FAILED,
1001 "Element <%s> not allowed inside <%s> in configuration file",
1002 element_name, "busconfig");
1008 append_rule_from_element (BusConfigParser *parser,
1009 const char *element_name,
1010 const char **attribute_names,
1011 const char **attribute_values,
1015 const char *send_interface;
1016 const char *send_member;
1017 const char *send_error;
1018 const char *send_destination;
1019 const char *send_path;
1020 const char *send_type;
1021 const char *receive_interface;
1022 const char *receive_member;
1023 const char *receive_error;
1024 const char *receive_sender;
1025 const char *receive_path;
1026 const char *receive_type;
1027 const char *eavesdrop;
1028 const char *send_requested_reply;
1029 const char *receive_requested_reply;
1034 BusPolicyRule *rule;
1036 if (!locate_attributes (parser, element_name,
1040 "send_interface", &send_interface,
1041 "send_member", &send_member,
1042 "send_error", &send_error,
1043 "send_destination", &send_destination,
1044 "send_path", &send_path,
1045 "send_type", &send_type,
1046 "receive_interface", &receive_interface,
1047 "receive_member", &receive_member,
1048 "receive_error", &receive_error,
1049 "receive_sender", &receive_sender,
1050 "receive_path", &receive_path,
1051 "receive_type", &receive_type,
1052 "eavesdrop", &eavesdrop,
1053 "send_requested_reply", &send_requested_reply,
1054 "receive_requested_reply", &receive_requested_reply,
1061 if (!(send_interface || send_member || send_error || send_destination ||
1062 send_type || send_path ||
1063 receive_interface || receive_member || receive_error || receive_sender ||
1064 receive_type || receive_path || eavesdrop ||
1065 send_requested_reply || receive_requested_reply ||
1066 own || user || group))
1068 dbus_set_error (error, DBUS_ERROR_FAILED,
1069 "Element <%s> must have one or more attributes",
1074 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1075 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1077 dbus_set_error (error, DBUS_ERROR_FAILED,
1078 "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.",
1083 /* Allowed combinations of elements are:
1085 * base, must be all send or all receive:
1088 * interface + member
1091 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1092 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1094 * user, group, own must occur alone
1096 * Pretty sure the below stuff is broken, FIXME think about it more.
1099 if (((send_interface && send_error) ||
1100 (send_interface && receive_interface) ||
1101 (send_interface && receive_member) ||
1102 (send_interface && receive_error) ||
1103 (send_interface && receive_sender) ||
1104 (send_interface && eavesdrop) ||
1105 (send_interface && receive_requested_reply) ||
1106 (send_interface && own) ||
1107 (send_interface && user) ||
1108 (send_interface && group)) ||
1110 ((send_member && send_error) ||
1111 (send_member && receive_interface) ||
1112 (send_member && receive_member) ||
1113 (send_member && receive_error) ||
1114 (send_member && receive_sender) ||
1115 (send_member && eavesdrop) ||
1116 (send_member && receive_requested_reply) ||
1117 (send_member && own) ||
1118 (send_member && user) ||
1119 (send_member && group)) ||
1121 ((send_error && receive_interface) ||
1122 (send_error && receive_member) ||
1123 (send_error && receive_error) ||
1124 (send_error && receive_sender) ||
1125 (send_error && eavesdrop) ||
1126 (send_error && receive_requested_reply) ||
1127 (send_error && own) ||
1128 (send_error && user) ||
1129 (send_error && group)) ||
1131 ((send_destination && receive_interface) ||
1132 (send_destination && receive_member) ||
1133 (send_destination && receive_error) ||
1134 (send_destination && receive_sender) ||
1135 (send_destination && eavesdrop) ||
1136 (send_destination && receive_requested_reply) ||
1137 (send_destination && own) ||
1138 (send_destination && user) ||
1139 (send_destination && group)) ||
1141 ((send_type && receive_interface) ||
1142 (send_type && receive_member) ||
1143 (send_type && receive_error) ||
1144 (send_type && receive_sender) ||
1145 (send_type && eavesdrop) ||
1146 (send_type && receive_requested_reply) ||
1147 (send_type && own) ||
1148 (send_type && user) ||
1149 (send_type && group)) ||
1151 ((send_path && receive_interface) ||
1152 (send_path && receive_member) ||
1153 (send_path && receive_error) ||
1154 (send_path && receive_sender) ||
1155 (send_path && eavesdrop) ||
1156 (send_path && receive_requested_reply) ||
1157 (send_path && own) ||
1158 (send_path && user) ||
1159 (send_path && group)) ||
1161 ((send_requested_reply && receive_interface) ||
1162 (send_requested_reply && receive_member) ||
1163 (send_requested_reply && receive_error) ||
1164 (send_requested_reply && receive_sender) ||
1165 (send_requested_reply && eavesdrop) ||
1166 (send_requested_reply && receive_requested_reply) ||
1167 (send_requested_reply && own) ||
1168 (send_requested_reply && user) ||
1169 (send_requested_reply && group)) ||
1171 ((receive_interface && receive_error) ||
1172 (receive_interface && own) ||
1173 (receive_interface && user) ||
1174 (receive_interface && group)) ||
1176 ((receive_member && receive_error) ||
1177 (receive_member && own) ||
1178 (receive_member && user) ||
1179 (receive_member && group)) ||
1181 ((receive_error && own) ||
1182 (receive_error && user) ||
1183 (receive_error && group)) ||
1185 ((eavesdrop && own) ||
1186 (eavesdrop && user) ||
1187 (eavesdrop && group)) ||
1189 ((receive_requested_reply && own) ||
1190 (receive_requested_reply && user) ||
1191 (receive_requested_reply && group)) ||
1198 dbus_set_error (error, DBUS_ERROR_FAILED,
1199 "Invalid combination of attributes on element <%s>",
1206 /* In BusPolicyRule, NULL represents wildcard.
1207 * In the config file, '*' represents it.
1209 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1211 if (send_interface || send_member || send_error || send_destination ||
1212 send_path || send_type || send_requested_reply)
1216 if (IS_WILDCARD (send_interface))
1217 send_interface = NULL;
1218 if (IS_WILDCARD (send_member))
1220 if (IS_WILDCARD (send_error))
1222 if (IS_WILDCARD (send_destination))
1223 send_destination = NULL;
1224 if (IS_WILDCARD (send_path))
1226 if (IS_WILDCARD (send_type))
1229 message_type = DBUS_MESSAGE_TYPE_INVALID;
1230 if (send_type != NULL)
1232 message_type = dbus_message_type_from_string (send_type);
1233 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1235 dbus_set_error (error, DBUS_ERROR_FAILED,
1236 "Bad message type \"%s\"",
1242 if (send_requested_reply &&
1243 !(strcmp (send_requested_reply, "true") == 0 ||
1244 strcmp (send_requested_reply, "false") == 0))
1246 dbus_set_error (error, DBUS_ERROR_FAILED,
1247 "Bad value \"%s\" for %s attribute, must be true or false",
1248 "send_requested_reply", send_requested_reply);
1252 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1256 if (send_requested_reply)
1257 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1259 rule->d.send.message_type = message_type;
1260 rule->d.send.path = _dbus_strdup (send_path);
1261 rule->d.send.interface = _dbus_strdup (send_interface);
1262 rule->d.send.member = _dbus_strdup (send_member);
1263 rule->d.send.error = _dbus_strdup (send_error);
1264 rule->d.send.destination = _dbus_strdup (send_destination);
1265 if (send_path && rule->d.send.path == NULL)
1267 if (send_interface && rule->d.send.interface == NULL)
1269 if (send_member && rule->d.send.member == NULL)
1271 if (send_error && rule->d.send.error == NULL)
1273 if (send_destination && rule->d.send.destination == NULL)
1276 else if (receive_interface || receive_member || receive_error || receive_sender ||
1277 receive_path || receive_type || eavesdrop || receive_requested_reply)
1281 if (IS_WILDCARD (receive_interface))
1282 receive_interface = NULL;
1283 if (IS_WILDCARD (receive_member))
1284 receive_member = NULL;
1285 if (IS_WILDCARD (receive_error))
1286 receive_error = NULL;
1287 if (IS_WILDCARD (receive_sender))
1288 receive_sender = NULL;
1289 if (IS_WILDCARD (receive_path))
1290 receive_path = NULL;
1291 if (IS_WILDCARD (receive_type))
1292 receive_type = NULL;
1294 message_type = DBUS_MESSAGE_TYPE_INVALID;
1295 if (receive_type != NULL)
1297 message_type = dbus_message_type_from_string (receive_type);
1298 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1300 dbus_set_error (error, DBUS_ERROR_FAILED,
1301 "Bad message type \"%s\"",
1309 !(strcmp (eavesdrop, "true") == 0 ||
1310 strcmp (eavesdrop, "false") == 0))
1312 dbus_set_error (error, DBUS_ERROR_FAILED,
1313 "Bad value \"%s\" for %s attribute, must be true or false",
1314 "eavesdrop", eavesdrop);
1318 if (receive_requested_reply &&
1319 !(strcmp (receive_requested_reply, "true") == 0 ||
1320 strcmp (receive_requested_reply, "false") == 0))
1322 dbus_set_error (error, DBUS_ERROR_FAILED,
1323 "Bad value \"%s\" for %s attribute, must be true or false",
1324 "receive_requested_reply", receive_requested_reply);
1328 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1333 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1335 if (receive_requested_reply)
1336 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1338 rule->d.receive.message_type = message_type;
1339 rule->d.receive.path = _dbus_strdup (receive_path);
1340 rule->d.receive.interface = _dbus_strdup (receive_interface);
1341 rule->d.receive.member = _dbus_strdup (receive_member);
1342 rule->d.receive.error = _dbus_strdup (receive_error);
1343 rule->d.receive.origin = _dbus_strdup (receive_sender);
1345 if (receive_path && rule->d.receive.path == NULL)
1347 if (receive_interface && rule->d.receive.interface == NULL)
1349 if (receive_member && rule->d.receive.member == NULL)
1351 if (receive_error && rule->d.receive.error == NULL)
1353 if (receive_sender && rule->d.receive.origin == NULL)
1358 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1362 if (IS_WILDCARD (own))
1365 rule->d.own.service_name = _dbus_strdup (own);
1366 if (own && rule->d.own.service_name == NULL)
1371 if (IS_WILDCARD (user))
1373 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1377 rule->d.user.uid = DBUS_UID_UNSET;
1381 DBusString username;
1384 _dbus_string_init_const (&username, user);
1386 if (_dbus_get_user_id (&username, &uid))
1388 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1392 rule->d.user.uid = uid;
1396 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1397 user, element_name);
1403 if (IS_WILDCARD (group))
1405 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1409 rule->d.group.gid = DBUS_GID_UNSET;
1413 DBusString groupname;
1416 _dbus_string_init_const (&groupname, group);
1418 if (_dbus_get_user_id (&groupname, &gid))
1420 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1424 rule->d.group.gid = gid;
1428 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1429 group, element_name);
1434 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1440 pe = peek_element (parser);
1441 _dbus_assert (pe != NULL);
1442 _dbus_assert (pe->type == ELEMENT_POLICY);
1444 switch (pe->d.policy.type)
1446 case POLICY_IGNORED:
1447 /* drop the rule on the floor */
1450 case POLICY_DEFAULT:
1451 if (!bus_policy_append_default_rule (parser->policy, rule))
1454 case POLICY_MANDATORY:
1455 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1459 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1461 dbus_set_error (error, DBUS_ERROR_FAILED,
1462 "<%s> rule cannot be per-user because it has bus-global semantics",
1467 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1472 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1474 dbus_set_error (error, DBUS_ERROR_FAILED,
1475 "<%s> rule cannot be per-group because it has bus-global semantics",
1480 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1486 case POLICY_CONSOLE:
1487 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1493 bus_policy_rule_unref (rule);
1500 BUS_SET_OOM (error);
1503 bus_policy_rule_unref (rule);
1508 start_policy_child (BusConfigParser *parser,
1509 const char *element_name,
1510 const char **attribute_names,
1511 const char **attribute_values,
1514 if (strcmp (element_name, "allow") == 0)
1516 if (!append_rule_from_element (parser, element_name,
1517 attribute_names, attribute_values,
1521 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1523 BUS_SET_OOM (error);
1529 else if (strcmp (element_name, "deny") == 0)
1531 if (!append_rule_from_element (parser, element_name,
1532 attribute_names, attribute_values,
1536 if (push_element (parser, ELEMENT_DENY) == NULL)
1538 BUS_SET_OOM (error);
1546 dbus_set_error (error, DBUS_ERROR_FAILED,
1547 "Element <%s> not allowed inside <%s> in configuration file",
1548 element_name, "policy");
1554 start_selinux_child (BusConfigParser *parser,
1555 const char *element_name,
1556 const char **attribute_names,
1557 const char **attribute_values,
1564 context_copy = NULL;
1566 if (strcmp (element_name, "associate") == 0)
1569 const char *context;
1571 if (!locate_attributes (parser, "associate",
1576 "context", &context,
1580 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1582 BUS_SET_OOM (error);
1586 if (own == NULL || context == NULL)
1588 dbus_set_error (error, DBUS_ERROR_FAILED,
1589 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1593 own_copy = _dbus_strdup (own);
1594 if (own_copy == NULL)
1596 context_copy = _dbus_strdup (context);
1597 if (context_copy == NULL)
1600 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1601 own_copy, context_copy))
1608 dbus_set_error (error, DBUS_ERROR_FAILED,
1609 "Element <%s> not allowed inside <%s> in configuration file",
1610 element_name, "selinux");
1616 dbus_free (own_copy);
1619 dbus_free (context_copy);
1621 BUS_SET_OOM (error);
1626 bus_config_parser_start_element (BusConfigParser *parser,
1627 const char *element_name,
1628 const char **attribute_names,
1629 const char **attribute_values,
1634 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1636 /* printf ("START: %s\n", element_name); */
1638 t = top_element_type (parser);
1640 if (t == ELEMENT_NONE)
1642 if (strcmp (element_name, "busconfig") == 0)
1644 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1647 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1649 BUS_SET_OOM (error);
1657 dbus_set_error (error, DBUS_ERROR_FAILED,
1658 "Unknown element <%s> at root of configuration file",
1663 else if (t == ELEMENT_BUSCONFIG)
1665 return start_busconfig_child (parser, element_name,
1666 attribute_names, attribute_values,
1669 else if (t == ELEMENT_POLICY)
1671 return start_policy_child (parser, element_name,
1672 attribute_names, attribute_values,
1675 else if (t == ELEMENT_SELINUX)
1677 return start_selinux_child (parser, element_name,
1678 attribute_names, attribute_values,
1683 dbus_set_error (error, DBUS_ERROR_FAILED,
1684 "Element <%s> is not allowed in this context",
1691 set_limit (BusConfigParser *parser,
1696 dbus_bool_t must_be_positive;
1697 dbus_bool_t must_be_int;
1699 must_be_int = FALSE;
1700 must_be_positive = FALSE;
1702 if (strcmp (name, "max_incoming_bytes") == 0)
1704 must_be_positive = TRUE;
1705 parser->limits.max_incoming_bytes = value;
1707 else if (strcmp (name, "max_outgoing_bytes") == 0)
1709 must_be_positive = TRUE;
1710 parser->limits.max_outgoing_bytes = value;
1712 else if (strcmp (name, "max_message_size") == 0)
1714 must_be_positive = TRUE;
1715 parser->limits.max_message_size = value;
1717 else if (strcmp (name, "service_start_timeout") == 0)
1719 must_be_positive = TRUE;
1721 parser->limits.activation_timeout = value;
1723 else if (strcmp (name, "auth_timeout") == 0)
1725 must_be_positive = TRUE;
1727 parser->limits.auth_timeout = value;
1729 else if (strcmp (name, "reply_timeout") == 0)
1731 must_be_positive = TRUE;
1733 parser->limits.reply_timeout = value;
1735 else if (strcmp (name, "max_completed_connections") == 0)
1737 must_be_positive = TRUE;
1739 parser->limits.max_completed_connections = value;
1741 else if (strcmp (name, "max_incomplete_connections") == 0)
1743 must_be_positive = TRUE;
1745 parser->limits.max_incomplete_connections = value;
1747 else if (strcmp (name, "max_connections_per_user") == 0)
1749 must_be_positive = TRUE;
1751 parser->limits.max_connections_per_user = value;
1753 else if (strcmp (name, "max_pending_service_starts") == 0)
1755 must_be_positive = TRUE;
1757 parser->limits.max_pending_activations = value;
1759 else if (strcmp (name, "max_names_per_connection") == 0)
1761 must_be_positive = TRUE;
1763 parser->limits.max_services_per_connection = value;
1765 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1767 must_be_positive = TRUE;
1769 parser->limits.max_match_rules_per_connection = value;
1771 else if (strcmp (name, "max_replies_per_connection") == 0)
1773 must_be_positive = TRUE;
1775 parser->limits.max_replies_per_connection = value;
1779 dbus_set_error (error, DBUS_ERROR_FAILED,
1780 "There is no limit called \"%s\"\n",
1785 if (must_be_positive && value < 0)
1787 dbus_set_error (error, DBUS_ERROR_FAILED,
1788 "<limit name=\"%s\"> must be a positive number\n",
1794 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1796 dbus_set_error (error, DBUS_ERROR_FAILED,
1797 "<limit name=\"%s\"> value is too large\n",
1806 bus_config_parser_end_element (BusConfigParser *parser,
1807 const char *element_name,
1814 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1816 /* printf ("END: %s\n", element_name); */
1818 t = top_element_type (parser);
1820 if (t == ELEMENT_NONE)
1822 /* should probably be an assertion failure but
1823 * being paranoid about XML parsers
1825 dbus_set_error (error, DBUS_ERROR_FAILED,
1826 "XML parser ended element with no element on the stack");
1830 n = element_type_to_name (t);
1831 _dbus_assert (n != NULL);
1832 if (strcmp (n, element_name) != 0)
1834 /* should probably be an assertion failure but
1835 * being paranoid about XML parsers
1837 dbus_set_error (error, DBUS_ERROR_FAILED,
1838 "XML element <%s> ended but topmost element on the stack was <%s>",
1843 e = peek_element (parser);
1844 _dbus_assert (e != NULL);
1849 _dbus_assert_not_reached ("element in stack has no type");
1852 case ELEMENT_INCLUDE:
1855 case ELEMENT_LISTEN:
1856 case ELEMENT_PIDFILE:
1858 case ELEMENT_SERVICEDIR:
1859 case ELEMENT_INCLUDEDIR:
1861 if (!e->had_content)
1863 dbus_set_error (error, DBUS_ERROR_FAILED,
1864 "XML element <%s> was expected to have content inside it",
1865 element_type_to_name (e->type));
1869 if (e->type == ELEMENT_LIMIT)
1871 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1877 case ELEMENT_BUSCONFIG:
1878 case ELEMENT_POLICY:
1882 case ELEMENT_SELINUX:
1883 case ELEMENT_ASSOCIATE:
1887 pop_element (parser);
1893 all_whitespace (const DBusString *str)
1897 _dbus_string_skip_white (str, 0, &i);
1899 return i == _dbus_string_get_length (str);
1903 make_full_path (const DBusString *basedir,
1904 const DBusString *filename,
1905 DBusString *full_path)
1907 if (_dbus_path_is_absolute (filename))
1909 return _dbus_string_copy (filename, 0, full_path, 0);
1913 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1916 if (!_dbus_concat_dir_and_file (full_path, filename))
1924 include_file (BusConfigParser *parser,
1925 const DBusString *filename,
1926 dbus_bool_t ignore_missing,
1929 /* FIXME good test case for this would load each config file in the
1930 * test suite both alone, and as an include, and check
1931 * that the result is the same
1933 BusConfigParser *included;
1934 const char *filename_str;
1935 DBusError tmp_error;
1937 dbus_error_init (&tmp_error);
1939 filename_str = _dbus_string_get_const_data (filename);
1941 /* Check to make sure this file hasn't already been included. */
1942 if (seen_include (parser, filename))
1944 dbus_set_error (error, DBUS_ERROR_FAILED,
1945 "Circular inclusion of file '%s'",
1950 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
1952 BUS_SET_OOM (error);
1956 /* Since parser is passed in as the parent, included
1957 inherits parser's limits. */
1958 included = bus_config_load (filename, FALSE, parser, &tmp_error);
1960 _dbus_list_pop_last (&parser->included_files);
1962 if (included == NULL)
1964 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1966 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1969 dbus_error_free (&tmp_error);
1974 dbus_move_error (&tmp_error, error);
1980 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1982 if (!merge_included (parser, included, error))
1984 bus_config_parser_unref (included);
1988 /* Copy included's limits back to parser. */
1989 parser->limits = included->limits;
1991 bus_config_parser_unref (included);
1997 include_dir (BusConfigParser *parser,
1998 const DBusString *dirname,
2001 DBusString filename;
2003 DBusError tmp_error;
2007 if (!_dbus_string_init (&filename))
2009 BUS_SET_OOM (error);
2015 dir = _dbus_directory_open (dirname, error);
2020 dbus_error_init (&tmp_error);
2021 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2023 DBusString full_path;
2025 if (!_dbus_string_init (&full_path))
2027 BUS_SET_OOM (error);
2031 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2033 BUS_SET_OOM (error);
2034 _dbus_string_free (&full_path);
2038 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2040 BUS_SET_OOM (error);
2041 _dbus_string_free (&full_path);
2045 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2047 if (!include_file (parser, &full_path, TRUE, error))
2049 _dbus_string_free (&full_path);
2054 _dbus_string_free (&full_path);
2057 if (dbus_error_is_set (&tmp_error))
2059 dbus_move_error (&tmp_error, error);
2064 if (!_dbus_string_copy_data (dirname, &s))
2066 BUS_SET_OOM (error);
2070 if (!_dbus_list_append (&parser->conf_dirs, s))
2073 BUS_SET_OOM (error);
2080 _dbus_string_free (&filename);
2083 _dbus_directory_close (dir);
2089 bus_config_parser_content (BusConfigParser *parser,
2090 const DBusString *content,
2095 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2101 _dbus_string_get_const_data (content, &c_str);
2103 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2107 e = peek_element (parser);
2110 dbus_set_error (error, DBUS_ERROR_FAILED,
2111 "Text content outside of any XML element in configuration file");
2114 else if (e->had_content)
2116 _dbus_assert_not_reached ("Element had multiple content blocks");
2120 switch (top_element_type (parser))
2123 _dbus_assert_not_reached ("element at top of stack has no type");
2126 case ELEMENT_BUSCONFIG:
2127 case ELEMENT_POLICY:
2131 case ELEMENT_SELINUX:
2132 case ELEMENT_ASSOCIATE:
2133 if (all_whitespace (content))
2137 dbus_set_error (error, DBUS_ERROR_FAILED,
2138 "No text content expected inside XML element %s in configuration file",
2139 element_type_to_name (top_element_type (parser)));
2143 case ELEMENT_PIDFILE:
2147 e->had_content = TRUE;
2149 if (!_dbus_string_copy_data (content, &s))
2152 dbus_free (parser->pidfile);
2153 parser->pidfile = s;
2157 case ELEMENT_INCLUDE:
2159 DBusString full_path, selinux_policy_root;
2161 e->had_content = TRUE;
2163 if (e->d.include.if_selinux_enabled
2164 && !bus_selinux_enabled ())
2167 if (!_dbus_string_init (&full_path))
2170 if (e->d.include.selinux_root_relative)
2172 if (!bus_selinux_get_policy_root ())
2174 dbus_set_error (error, DBUS_ERROR_FAILED,
2175 "Could not determine SELinux policy root for relative inclusion");
2176 _dbus_string_free (&full_path);
2179 _dbus_string_init_const (&selinux_policy_root,
2180 bus_selinux_get_policy_root ());
2181 if (!make_full_path (&selinux_policy_root, content, &full_path))
2183 _dbus_string_free (&full_path);
2187 else if (!make_full_path (&parser->basedir, content, &full_path))
2189 _dbus_string_free (&full_path);
2193 if (!include_file (parser, &full_path,
2194 e->d.include.ignore_missing, error))
2196 _dbus_string_free (&full_path);
2200 _dbus_string_free (&full_path);
2204 case ELEMENT_INCLUDEDIR:
2206 DBusString full_path;
2208 e->had_content = TRUE;
2210 if (!_dbus_string_init (&full_path))
2213 if (!make_full_path (&parser->basedir, content, &full_path))
2215 _dbus_string_free (&full_path);
2219 if (!include_dir (parser, &full_path, error))
2221 _dbus_string_free (&full_path);
2225 _dbus_string_free (&full_path);
2233 e->had_content = TRUE;
2235 if (!_dbus_string_copy_data (content, &s))
2238 dbus_free (parser->user);
2247 e->had_content = TRUE;
2249 if (!_dbus_string_copy_data (content, &s))
2252 dbus_free (parser->bus_type);
2253 parser->bus_type = s;
2257 case ELEMENT_LISTEN:
2261 e->had_content = TRUE;
2263 if (!_dbus_string_copy_data (content, &s))
2266 if (!_dbus_list_append (&parser->listen_on,
2279 e->had_content = TRUE;
2281 if (!_dbus_string_copy_data (content, &s))
2284 if (!_dbus_list_append (&parser->mechanisms,
2293 case ELEMENT_SERVICEDIR:
2296 DBusString full_path;
2298 e->had_content = TRUE;
2300 if (!_dbus_string_init (&full_path))
2303 if (!make_full_path (&parser->basedir, content, &full_path))
2305 _dbus_string_free (&full_path);
2309 if (!_dbus_string_copy_data (&full_path, &s))
2311 _dbus_string_free (&full_path);
2315 if (!_dbus_list_append (&parser->service_dirs, s))
2317 _dbus_string_free (&full_path);
2322 _dbus_string_free (&full_path);
2330 e->had_content = TRUE;
2333 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2335 dbus_set_error (error, DBUS_ERROR_FAILED,
2336 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2341 e->d.limit.value = val;
2343 _dbus_verbose ("Loaded value %ld for limit %s\n",
2350 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2354 BUS_SET_OOM (error);
2359 bus_config_parser_finished (BusConfigParser *parser,
2362 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2364 if (parser->stack != NULL)
2366 dbus_set_error (error, DBUS_ERROR_FAILED,
2367 "Element <%s> was not closed in configuration file",
2368 element_type_to_name (top_element_type (parser)));
2373 if (parser->is_toplevel && parser->listen_on == NULL)
2375 dbus_set_error (error, DBUS_ERROR_FAILED,
2376 "Configuration file needs one or more <listen> elements giving addresses");
2384 bus_config_parser_get_user (BusConfigParser *parser)
2386 return parser->user;
2390 bus_config_parser_get_type (BusConfigParser *parser)
2392 return parser->bus_type;
2396 bus_config_parser_get_addresses (BusConfigParser *parser)
2398 return &parser->listen_on;
2402 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2404 return &parser->mechanisms;
2408 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2410 return &parser->service_dirs;
2414 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2416 return &parser->conf_dirs;
2420 bus_config_parser_get_fork (BusConfigParser *parser)
2422 return parser->fork;
2426 bus_config_parser_get_pidfile (BusConfigParser *parser)
2428 return parser->pidfile;
2432 bus_config_parser_steal_policy (BusConfigParser *parser)
2436 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2438 policy = parser->policy;
2440 parser->policy = NULL;
2445 /* Overwrite any limits that were set in the configuration file */
2447 bus_config_parser_get_limits (BusConfigParser *parser,
2450 *limits = parser->limits;
2454 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2456 DBusHashTable *table;
2458 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2460 table = parser->service_context_table;
2462 parser->service_context_table = NULL;
2467 #ifdef DBUS_BUILD_TESTS
2478 do_load (const DBusString *full_path,
2480 dbus_bool_t oom_possible)
2482 BusConfigParser *parser;
2485 dbus_error_init (&error);
2487 parser = bus_config_load (full_path, TRUE, NULL, &error);
2490 _DBUS_ASSERT_ERROR_IS_SET (&error);
2493 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2495 _dbus_verbose ("Failed to load valid file due to OOM\n");
2496 dbus_error_free (&error);
2499 else if (validity == VALID)
2501 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2504 dbus_error_free (&error);
2509 dbus_error_free (&error);
2515 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2517 bus_config_parser_unref (parser);
2519 if (validity == INVALID)
2521 _dbus_warn ("Accepted invalid file\n");
2531 const DBusString *full_path;
2536 check_loader_oom_func (void *data)
2538 LoaderOomData *d = data;
2540 return do_load (d->full_path, d->validity, TRUE);
2544 process_test_valid_subdir (const DBusString *test_base_dir,
2548 DBusString test_directory;
2549 DBusString filename;
2557 if (!_dbus_string_init (&test_directory))
2558 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2560 _dbus_string_init_const (&filename, subdir);
2562 if (!_dbus_string_copy (test_base_dir, 0,
2563 &test_directory, 0))
2564 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2566 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2567 _dbus_assert_not_reached ("couldn't allocate full path");
2569 _dbus_string_free (&filename);
2570 if (!_dbus_string_init (&filename))
2571 _dbus_assert_not_reached ("didn't allocate filename string\n");
2573 dbus_error_init (&error);
2574 dir = _dbus_directory_open (&test_directory, &error);
2577 _dbus_warn ("Could not open %s: %s\n",
2578 _dbus_string_get_const_data (&test_directory),
2580 dbus_error_free (&error);
2584 if (validity == VALID)
2585 printf ("Testing valid files:\n");
2586 else if (validity == INVALID)
2587 printf ("Testing invalid files:\n");
2589 printf ("Testing unknown files:\n");
2592 while (_dbus_directory_get_next_file (dir, &filename, &error))
2594 DBusString full_path;
2597 if (!_dbus_string_init (&full_path))
2598 _dbus_assert_not_reached ("couldn't init string");
2600 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2601 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2603 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2604 _dbus_assert_not_reached ("couldn't concat file to dir");
2606 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2608 _dbus_verbose ("Skipping non-.conf file %s\n",
2609 _dbus_string_get_const_data (&filename));
2610 _dbus_string_free (&full_path);
2614 printf (" %s\n", _dbus_string_get_const_data (&filename));
2616 _dbus_verbose (" expecting %s\n",
2617 validity == VALID ? "valid" :
2618 (validity == INVALID ? "invalid" :
2619 (validity == UNKNOWN ? "unknown" : "???")));
2621 d.full_path = &full_path;
2622 d.validity = validity;
2624 /* FIXME hackaround for an expat problem, see
2625 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2626 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2628 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2629 if (!check_loader_oom_func (&d))
2630 _dbus_assert_not_reached ("test failed");
2632 _dbus_string_free (&full_path);
2635 if (dbus_error_is_set (&error))
2637 _dbus_warn ("Could not get next file in %s: %s\n",
2638 _dbus_string_get_const_data (&test_directory),
2640 dbus_error_free (&error);
2649 _dbus_directory_close (dir);
2650 _dbus_string_free (&test_directory);
2651 _dbus_string_free (&filename);
2657 bools_equal (dbus_bool_t a,
2664 strings_equal_or_both_null (const char *a,
2667 if (a == NULL || b == NULL)
2670 return !strcmp (a, b);
2674 elements_equal (const Element *a,
2677 if (a->type != b->type)
2680 if (!bools_equal (a->had_content, b->had_content))
2686 case ELEMENT_INCLUDE:
2687 if (!bools_equal (a->d.include.ignore_missing,
2688 b->d.include.ignore_missing))
2692 case ELEMENT_POLICY:
2693 if (a->d.policy.type != b->d.policy.type)
2695 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
2700 if (strcmp (a->d.limit.name, b->d.limit.name))
2702 if (a->d.limit.value != b->d.limit.value)
2716 lists_of_elements_equal (DBusList *a,
2725 while (ia != NULL && ib != NULL)
2727 if (elements_equal (ia->data, ib->data))
2729 ia = _dbus_list_get_next_link (&a, ia);
2730 ib = _dbus_list_get_next_link (&b, ib);
2733 return ia == NULL && ib == NULL;
2737 lists_of_c_strings_equal (DBusList *a,
2746 while (ia != NULL && ib != NULL)
2748 if (strcmp (ia->data, ib->data))
2750 ia = _dbus_list_get_next_link (&a, ia);
2751 ib = _dbus_list_get_next_link (&b, ib);
2754 return ia == NULL && ib == NULL;
2758 limits_equal (const BusLimits *a,
2762 (a->max_incoming_bytes == b->max_incoming_bytes
2763 || a->max_outgoing_bytes == b->max_outgoing_bytes
2764 || a->max_message_size == b->max_message_size
2765 || a->activation_timeout == b->activation_timeout
2766 || a->auth_timeout == b->auth_timeout
2767 || a->max_completed_connections == b->max_completed_connections
2768 || a->max_incomplete_connections == b->max_incomplete_connections
2769 || a->max_connections_per_user == b->max_connections_per_user
2770 || a->max_pending_activations == b->max_pending_activations
2771 || a->max_services_per_connection == b->max_services_per_connection
2772 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2773 || a->max_replies_per_connection == b->max_replies_per_connection
2774 || a->reply_timeout == b->reply_timeout);
2778 config_parsers_equal (const BusConfigParser *a,
2779 const BusConfigParser *b)
2781 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2784 if (!lists_of_elements_equal (a->stack, b->stack))
2787 if (!strings_equal_or_both_null (a->user, b->user))
2790 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2793 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2796 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2799 /* FIXME: compare policy */
2801 /* FIXME: compare service selinux ID table */
2803 if (! limits_equal (&a->limits, &b->limits))
2806 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2809 if (! bools_equal (a->fork, b->fork))
2812 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2819 all_are_equiv (const DBusString *target_directory)
2821 DBusString filename;
2823 BusConfigParser *first_parser;
2824 BusConfigParser *parser;
2830 first_parser = NULL;
2834 if (!_dbus_string_init (&filename))
2835 _dbus_assert_not_reached ("didn't allocate filename string");
2837 dbus_error_init (&error);
2838 dir = _dbus_directory_open (target_directory, &error);
2841 _dbus_warn ("Could not open %s: %s\n",
2842 _dbus_string_get_const_data (target_directory),
2844 dbus_error_free (&error);
2848 printf ("Comparing equivalent files:\n");
2851 while (_dbus_directory_get_next_file (dir, &filename, &error))
2853 DBusString full_path;
2855 if (!_dbus_string_init (&full_path))
2856 _dbus_assert_not_reached ("couldn't init string");
2858 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2859 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2861 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2862 _dbus_assert_not_reached ("couldn't concat file to dir");
2864 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2866 _dbus_verbose ("Skipping non-.conf file %s\n",
2867 _dbus_string_get_const_data (&filename));
2868 _dbus_string_free (&full_path);
2872 printf (" %s\n", _dbus_string_get_const_data (&filename));
2874 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2878 _dbus_warn ("Could not load file %s: %s\n",
2879 _dbus_string_get_const_data (&full_path),
2881 _dbus_string_free (&full_path);
2882 dbus_error_free (&error);
2885 else if (first_parser == NULL)
2887 _dbus_string_free (&full_path);
2888 first_parser = parser;
2892 _dbus_string_free (&full_path);
2893 equal = config_parsers_equal (first_parser, parser);
2894 bus_config_parser_unref (parser);
2903 _dbus_string_free (&filename);
2905 bus_config_parser_unref (first_parser);
2907 _dbus_directory_close (dir);
2914 process_test_equiv_subdir (const DBusString *test_base_dir,
2917 DBusString test_directory;
2918 DBusString filename;
2927 if (!_dbus_string_init (&test_directory))
2928 _dbus_assert_not_reached ("didn't allocate test_directory");
2930 _dbus_string_init_const (&filename, subdir);
2932 if (!_dbus_string_copy (test_base_dir, 0,
2933 &test_directory, 0))
2934 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2936 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2937 _dbus_assert_not_reached ("couldn't allocate full path");
2939 _dbus_string_free (&filename);
2940 if (!_dbus_string_init (&filename))
2941 _dbus_assert_not_reached ("didn't allocate filename string");
2943 dbus_error_init (&error);
2944 dir = _dbus_directory_open (&test_directory, &error);
2947 _dbus_warn ("Could not open %s: %s\n",
2948 _dbus_string_get_const_data (&test_directory),
2950 dbus_error_free (&error);
2954 while (_dbus_directory_get_next_file (dir, &filename, &error))
2956 DBusString full_path;
2958 /* Skip CVS's magic directories! */
2959 if (_dbus_string_equal_c_str (&filename, "CVS"))
2962 if (!_dbus_string_init (&full_path))
2963 _dbus_assert_not_reached ("couldn't init string");
2965 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2966 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2968 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2969 _dbus_assert_not_reached ("couldn't concat file to dir");
2971 equal = all_are_equiv (&full_path);
2972 _dbus_string_free (&full_path);
2981 _dbus_string_free (&test_directory);
2982 _dbus_string_free (&filename);
2984 _dbus_directory_close (dir);
2991 bus_config_parser_test (const DBusString *test_data_dir)
2993 if (test_data_dir == NULL ||
2994 _dbus_string_get_length (test_data_dir) == 0)
2996 printf ("No test data\n");
3000 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3003 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3006 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3012 #endif /* DBUS_BUILD_TESTS */