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 BusPolicy *policy; /**< Security policy */
120 BusLimits limits; /**< Limits */
122 char *pidfile; /**< PID file */
124 DBusList *included_files; /**< Included files stack */
126 DBusHashTable *service_context_table; /**< Map service names to SELinux contexts */
128 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
130 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
134 element_type_to_name (ElementType type)
140 case ELEMENT_BUSCONFIG:
142 case ELEMENT_INCLUDE:
160 case ELEMENT_PIDFILE:
162 case ELEMENT_SERVICEDIR:
164 case ELEMENT_INCLUDEDIR:
168 case ELEMENT_SELINUX:
170 case ELEMENT_ASSOCIATE:
174 _dbus_assert_not_reached ("bad element type");
180 push_element (BusConfigParser *parser,
185 _dbus_assert (type != ELEMENT_NONE);
187 e = dbus_new0 (Element, 1);
191 if (!_dbus_list_append (&parser->stack, e))
203 element_free (Element *e)
205 if (e->type == ELEMENT_LIMIT)
206 dbus_free (e->d.limit.name);
212 pop_element (BusConfigParser *parser)
216 e = _dbus_list_pop_last (&parser->stack);
222 peek_element (BusConfigParser *parser)
226 e = _dbus_list_get_last (&parser->stack);
232 top_element_type (BusConfigParser *parser)
236 e = _dbus_list_get_last (&parser->stack);
245 merge_service_context_hash (DBusHashTable *dest,
250 _dbus_hash_iter_init (from, &iter);
251 while (_dbus_hash_iter_next (&iter))
253 const char *service = _dbus_hash_iter_get_string_key (&iter);
254 const char *context = _dbus_hash_iter_get_value (&iter);
258 service_copy = _dbus_strdup (service);
259 if (service_copy == NULL)
261 context_copy = _dbus_strdup (context);
262 if (context_copy == NULL)
265 if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
273 merge_included (BusConfigParser *parser,
274 BusConfigParser *included,
279 if (!bus_policy_merge (parser->policy,
286 if (!merge_service_context_hash (parser->service_context_table,
287 included->service_context_table))
293 if (included->user != NULL)
295 dbus_free (parser->user);
296 parser->user = included->user;
297 included->user = NULL;
300 if (included->bus_type != NULL)
302 dbus_free (parser->bus_type);
303 parser->bus_type = included->bus_type;
304 included->bus_type = NULL;
310 if (included->pidfile != NULL)
312 dbus_free (parser->pidfile);
313 parser->pidfile = included->pidfile;
314 included->pidfile = NULL;
317 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
318 _dbus_list_append_link (&parser->listen_on, link);
320 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
321 _dbus_list_append_link (&parser->mechanisms, link);
323 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
324 _dbus_list_append_link (&parser->service_dirs, link);
330 seen_include (BusConfigParser *parser,
331 const DBusString *file)
335 iter = parser->included_files;
338 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
341 iter = _dbus_list_get_next_link (&parser->included_files, iter);
348 bus_config_parser_new (const DBusString *basedir,
349 dbus_bool_t is_toplevel,
350 const BusConfigParser *parent)
352 BusConfigParser *parser;
354 parser = dbus_new0 (BusConfigParser, 1);
358 parser->is_toplevel = !!is_toplevel;
360 if (!_dbus_string_init (&parser->basedir))
366 if (((parser->policy = bus_policy_new ()) == NULL) ||
367 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
368 ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
370 dbus_free)) == NULL))
373 bus_policy_unref (parser->policy);
375 _dbus_string_free (&parser->basedir);
383 /* Initialize the parser's limits from the parent. */
384 parser->limits = parent->limits;
386 /* Use the parent's list of included_files to avoid
387 circular inclusions. */
388 parser->included_files = parent->included_files;
393 /* Make up some numbers! woot! */
394 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
395 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
396 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
398 /* Making this long means the user has to wait longer for an error
399 * message if something screws up, but making it too short means
400 * they might see a false failure.
402 parser->limits.activation_timeout = 25000; /* 25 seconds */
404 /* Making this long risks making a DOS attack easier, but too short
405 * and legitimate auth will fail. If interactive auth (ask user for
406 * password) is allowed, then potentially it has to be quite long.
408 parser->limits.auth_timeout = 30000; /* 30 seconds */
410 parser->limits.max_incomplete_connections = 32;
411 parser->limits.max_connections_per_user = 128;
413 /* Note that max_completed_connections / max_connections_per_user
414 * is the number of users that would have to work together to
415 * DOS all the other users.
417 parser->limits.max_completed_connections = 1024;
419 parser->limits.max_pending_activations = 256;
420 parser->limits.max_services_per_connection = 256;
422 parser->limits.max_match_rules_per_connection = 512;
424 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
425 parser->limits.max_replies_per_connection = 32;
428 parser->refcount = 1;
434 bus_config_parser_ref (BusConfigParser *parser)
436 _dbus_assert (parser->refcount > 0);
438 parser->refcount += 1;
444 bus_config_parser_unref (BusConfigParser *parser)
446 _dbus_assert (parser->refcount > 0);
448 parser->refcount -= 1;
450 if (parser->refcount == 0)
452 while (parser->stack != NULL)
453 pop_element (parser);
455 dbus_free (parser->user);
456 dbus_free (parser->bus_type);
457 dbus_free (parser->pidfile);
459 _dbus_list_foreach (&parser->listen_on,
460 (DBusForeachFunction) dbus_free,
463 _dbus_list_clear (&parser->listen_on);
465 _dbus_list_foreach (&parser->service_dirs,
466 (DBusForeachFunction) dbus_free,
469 _dbus_list_clear (&parser->service_dirs);
471 _dbus_list_foreach (&parser->mechanisms,
472 (DBusForeachFunction) dbus_free,
475 _dbus_list_clear (&parser->mechanisms);
477 _dbus_string_free (&parser->basedir);
480 bus_policy_unref (parser->policy);
482 if (parser->service_context_table)
483 _dbus_hash_table_unref (parser->service_context_table);
490 bus_config_parser_check_doctype (BusConfigParser *parser,
494 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
496 if (strcmp (doctype, "busconfig") != 0)
498 dbus_set_error (error,
500 "Configuration file has the wrong document type %s",
515 locate_attributes (BusConfigParser *parser,
516 const char *element_name,
517 const char **attribute_names,
518 const char **attribute_values,
520 const char *first_attribute_name,
521 const char **first_attribute_retloc,
529 LocateAttr attrs[MAX_ATTRS];
533 _dbus_assert (first_attribute_name != NULL);
534 _dbus_assert (first_attribute_retloc != NULL);
539 attrs[0].name = first_attribute_name;
540 attrs[0].retloc = first_attribute_retloc;
541 *first_attribute_retloc = NULL;
543 va_start (args, first_attribute_retloc);
545 name = va_arg (args, const char*);
546 retloc = va_arg (args, const char**);
550 _dbus_assert (retloc != NULL);
551 _dbus_assert (n_attrs < MAX_ATTRS);
553 attrs[n_attrs].name = name;
554 attrs[n_attrs].retloc = retloc;
558 name = va_arg (args, const char*);
559 retloc = va_arg (args, const char**);
568 while (attribute_names[i])
577 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
579 retloc = attrs[j].retloc;
583 dbus_set_error (error, DBUS_ERROR_FAILED,
584 "Attribute \"%s\" repeated twice on the same <%s> element",
585 attrs[j].name, element_name);
590 *retloc = attribute_values[i];
599 dbus_set_error (error, DBUS_ERROR_FAILED,
600 "Attribute \"%s\" is invalid on <%s> element in this context",
601 attribute_names[i], element_name);
614 check_no_attributes (BusConfigParser *parser,
615 const char *element_name,
616 const char **attribute_names,
617 const char **attribute_values,
620 if (attribute_names[0] != NULL)
622 dbus_set_error (error, DBUS_ERROR_FAILED,
623 "Attribute \"%s\" is invalid on <%s> element in this context",
624 attribute_names[0], element_name);
632 start_busconfig_child (BusConfigParser *parser,
633 const char *element_name,
634 const char **attribute_names,
635 const char **attribute_values,
638 if (strcmp (element_name, "user") == 0)
640 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
643 if (push_element (parser, ELEMENT_USER) == NULL)
651 else if (strcmp (element_name, "type") == 0)
653 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
656 if (push_element (parser, ELEMENT_TYPE) == NULL)
664 else if (strcmp (element_name, "fork") == 0)
666 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
669 if (push_element (parser, ELEMENT_FORK) == NULL)
679 else if (strcmp (element_name, "pidfile") == 0)
681 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
684 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
692 else if (strcmp (element_name, "listen") == 0)
694 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
697 if (push_element (parser, ELEMENT_LISTEN) == NULL)
705 else if (strcmp (element_name, "auth") == 0)
707 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
710 if (push_element (parser, ELEMENT_AUTH) == NULL)
718 else if (strcmp (element_name, "includedir") == 0)
720 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
723 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
731 else if (strcmp (element_name, "servicedir") == 0)
733 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
736 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
744 else if (strcmp (element_name, "include") == 0)
747 const char *if_selinux_enabled;
748 const char *ignore_missing;
749 const char *selinux_root_relative;
751 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
757 e->d.include.ignore_missing = FALSE;
758 e->d.include.if_selinux_enabled = FALSE;
759 e->d.include.selinux_root_relative = FALSE;
761 if (!locate_attributes (parser, "include",
765 "ignore_missing", &ignore_missing,
766 "if_selinux_enabled", &if_selinux_enabled,
767 "selinux_root_relative", &selinux_root_relative,
771 if (ignore_missing != NULL)
773 if (strcmp (ignore_missing, "yes") == 0)
774 e->d.include.ignore_missing = TRUE;
775 else if (strcmp (ignore_missing, "no") == 0)
776 e->d.include.ignore_missing = FALSE;
779 dbus_set_error (error, DBUS_ERROR_FAILED,
780 "ignore_missing attribute must have value \"yes\" or \"no\"");
785 if (if_selinux_enabled != NULL)
787 if (strcmp (if_selinux_enabled, "yes") == 0)
788 e->d.include.if_selinux_enabled = TRUE;
789 else if (strcmp (if_selinux_enabled, "no") == 0)
790 e->d.include.if_selinux_enabled = FALSE;
793 dbus_set_error (error, DBUS_ERROR_FAILED,
794 "if_selinux_enabled attribute must have value"
795 " \"yes\" or \"no\"");
800 if (selinux_root_relative != NULL)
802 if (strcmp (selinux_root_relative, "yes") == 0)
803 e->d.include.selinux_root_relative = TRUE;
804 else if (strcmp (selinux_root_relative, "no") == 0)
805 e->d.include.selinux_root_relative = FALSE;
808 dbus_set_error (error, DBUS_ERROR_FAILED,
809 "selinux_root_relative attribute must have value"
810 " \"yes\" or \"no\"");
817 else if (strcmp (element_name, "policy") == 0)
823 const char *at_console;
825 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
831 e->d.policy.type = POLICY_IGNORED;
833 if (!locate_attributes (parser, "policy",
840 "at_console", &at_console,
844 if (((context && user) ||
845 (context && group) ||
846 (context && at_console)) ||
848 (user && at_console)) ||
849 (group && at_console) ||
850 !(context || user || group || at_console))
852 dbus_set_error (error, DBUS_ERROR_FAILED,
853 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
859 if (strcmp (context, "default") == 0)
861 e->d.policy.type = POLICY_DEFAULT;
863 else if (strcmp (context, "mandatory") == 0)
865 e->d.policy.type = POLICY_MANDATORY;
869 dbus_set_error (error, DBUS_ERROR_FAILED,
870 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
875 else if (user != NULL)
878 _dbus_string_init_const (&username, user);
880 if (_dbus_get_user_id (&username,
881 &e->d.policy.gid_uid_or_at_console))
882 e->d.policy.type = POLICY_USER;
884 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
887 else if (group != NULL)
889 DBusString group_name;
890 _dbus_string_init_const (&group_name, group);
892 if (_dbus_get_group_id (&group_name,
893 &e->d.policy.gid_uid_or_at_console))
894 e->d.policy.type = POLICY_GROUP;
896 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
899 else if (at_console != NULL)
902 t = (strcmp (at_console, "true") == 0);
903 if (t || strcmp (at_console, "false") == 0)
905 e->d.policy.gid_uid_or_at_console = t;
906 e->d.policy.type = POLICY_CONSOLE;
910 dbus_set_error (error, DBUS_ERROR_FAILED,
911 "Unknown value \"%s\" for at_console in message bus configuration file",
919 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
924 else if (strcmp (element_name, "limit") == 0)
929 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
935 if (!locate_attributes (parser, "limit",
945 dbus_set_error (error, DBUS_ERROR_FAILED,
946 "<limit> element must have a \"name\" attribute");
950 e->d.limit.name = _dbus_strdup (name);
951 if (e->d.limit.name == NULL)
959 else if (strcmp (element_name, "selinux") == 0)
961 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
964 if (push_element (parser, ELEMENT_SELINUX) == NULL)
974 dbus_set_error (error, DBUS_ERROR_FAILED,
975 "Element <%s> not allowed inside <%s> in configuration file",
976 element_name, "busconfig");
982 append_rule_from_element (BusConfigParser *parser,
983 const char *element_name,
984 const char **attribute_names,
985 const char **attribute_values,
989 const char *send_interface;
990 const char *send_member;
991 const char *send_error;
992 const char *send_destination;
993 const char *send_path;
994 const char *send_type;
995 const char *receive_interface;
996 const char *receive_member;
997 const char *receive_error;
998 const char *receive_sender;
999 const char *receive_path;
1000 const char *receive_type;
1001 const char *eavesdrop;
1002 const char *send_requested_reply;
1003 const char *receive_requested_reply;
1008 BusPolicyRule *rule;
1010 if (!locate_attributes (parser, element_name,
1014 "send_interface", &send_interface,
1015 "send_member", &send_member,
1016 "send_error", &send_error,
1017 "send_destination", &send_destination,
1018 "send_path", &send_path,
1019 "send_type", &send_type,
1020 "receive_interface", &receive_interface,
1021 "receive_member", &receive_member,
1022 "receive_error", &receive_error,
1023 "receive_sender", &receive_sender,
1024 "receive_path", &receive_path,
1025 "receive_type", &receive_type,
1026 "eavesdrop", &eavesdrop,
1027 "send_requested_reply", &send_requested_reply,
1028 "receive_requested_reply", &receive_requested_reply,
1035 if (!(send_interface || send_member || send_error || send_destination ||
1036 send_type || send_path ||
1037 receive_interface || receive_member || receive_error || receive_sender ||
1038 receive_type || receive_path || eavesdrop ||
1039 send_requested_reply || receive_requested_reply ||
1040 own || user || group))
1042 dbus_set_error (error, DBUS_ERROR_FAILED,
1043 "Element <%s> must have one or more attributes",
1048 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1049 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1051 dbus_set_error (error, DBUS_ERROR_FAILED,
1052 "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.",
1057 /* Allowed combinations of elements are:
1059 * base, must be all send or all receive:
1062 * interface + member
1065 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1066 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1068 * user, group, own must occur alone
1070 * Pretty sure the below stuff is broken, FIXME think about it more.
1073 if (((send_interface && send_error) ||
1074 (send_interface && receive_interface) ||
1075 (send_interface && receive_member) ||
1076 (send_interface && receive_error) ||
1077 (send_interface && receive_sender) ||
1078 (send_interface && eavesdrop) ||
1079 (send_interface && receive_requested_reply) ||
1080 (send_interface && own) ||
1081 (send_interface && user) ||
1082 (send_interface && group)) ||
1084 ((send_member && send_error) ||
1085 (send_member && receive_interface) ||
1086 (send_member && receive_member) ||
1087 (send_member && receive_error) ||
1088 (send_member && receive_sender) ||
1089 (send_member && eavesdrop) ||
1090 (send_member && receive_requested_reply) ||
1091 (send_member && own) ||
1092 (send_member && user) ||
1093 (send_member && group)) ||
1095 ((send_error && receive_interface) ||
1096 (send_error && receive_member) ||
1097 (send_error && receive_error) ||
1098 (send_error && receive_sender) ||
1099 (send_error && eavesdrop) ||
1100 (send_error && receive_requested_reply) ||
1101 (send_error && own) ||
1102 (send_error && user) ||
1103 (send_error && group)) ||
1105 ((send_destination && receive_interface) ||
1106 (send_destination && receive_member) ||
1107 (send_destination && receive_error) ||
1108 (send_destination && receive_sender) ||
1109 (send_destination && eavesdrop) ||
1110 (send_destination && receive_requested_reply) ||
1111 (send_destination && own) ||
1112 (send_destination && user) ||
1113 (send_destination && group)) ||
1115 ((send_type && receive_interface) ||
1116 (send_type && receive_member) ||
1117 (send_type && receive_error) ||
1118 (send_type && receive_sender) ||
1119 (send_type && eavesdrop) ||
1120 (send_type && receive_requested_reply) ||
1121 (send_type && own) ||
1122 (send_type && user) ||
1123 (send_type && group)) ||
1125 ((send_path && receive_interface) ||
1126 (send_path && receive_member) ||
1127 (send_path && receive_error) ||
1128 (send_path && receive_sender) ||
1129 (send_path && eavesdrop) ||
1130 (send_path && receive_requested_reply) ||
1131 (send_path && own) ||
1132 (send_path && user) ||
1133 (send_path && group)) ||
1135 ((send_requested_reply && receive_interface) ||
1136 (send_requested_reply && receive_member) ||
1137 (send_requested_reply && receive_error) ||
1138 (send_requested_reply && receive_sender) ||
1139 (send_requested_reply && eavesdrop) ||
1140 (send_requested_reply && receive_requested_reply) ||
1141 (send_requested_reply && own) ||
1142 (send_requested_reply && user) ||
1143 (send_requested_reply && group)) ||
1145 ((receive_interface && receive_error) ||
1146 (receive_interface && own) ||
1147 (receive_interface && user) ||
1148 (receive_interface && group)) ||
1150 ((receive_member && receive_error) ||
1151 (receive_member && own) ||
1152 (receive_member && user) ||
1153 (receive_member && group)) ||
1155 ((receive_error && own) ||
1156 (receive_error && user) ||
1157 (receive_error && group)) ||
1159 ((eavesdrop && own) ||
1160 (eavesdrop && user) ||
1161 (eavesdrop && group)) ||
1163 ((receive_requested_reply && own) ||
1164 (receive_requested_reply && user) ||
1165 (receive_requested_reply && group)) ||
1172 dbus_set_error (error, DBUS_ERROR_FAILED,
1173 "Invalid combination of attributes on element <%s>",
1180 /* In BusPolicyRule, NULL represents wildcard.
1181 * In the config file, '*' represents it.
1183 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1185 if (send_interface || send_member || send_error || send_destination ||
1186 send_path || send_type || send_requested_reply)
1190 if (IS_WILDCARD (send_interface))
1191 send_interface = NULL;
1192 if (IS_WILDCARD (send_member))
1194 if (IS_WILDCARD (send_error))
1196 if (IS_WILDCARD (send_destination))
1197 send_destination = NULL;
1198 if (IS_WILDCARD (send_path))
1200 if (IS_WILDCARD (send_type))
1203 message_type = DBUS_MESSAGE_TYPE_INVALID;
1204 if (send_type != NULL)
1206 message_type = dbus_message_type_from_string (send_type);
1207 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1209 dbus_set_error (error, DBUS_ERROR_FAILED,
1210 "Bad message type \"%s\"",
1216 if (send_requested_reply &&
1217 !(strcmp (send_requested_reply, "true") == 0 ||
1218 strcmp (send_requested_reply, "false") == 0))
1220 dbus_set_error (error, DBUS_ERROR_FAILED,
1221 "Bad value \"%s\" for %s attribute, must be true or false",
1222 "send_requested_reply", send_requested_reply);
1226 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1230 if (send_requested_reply)
1231 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1233 rule->d.send.message_type = message_type;
1234 rule->d.send.path = _dbus_strdup (send_path);
1235 rule->d.send.interface = _dbus_strdup (send_interface);
1236 rule->d.send.member = _dbus_strdup (send_member);
1237 rule->d.send.error = _dbus_strdup (send_error);
1238 rule->d.send.destination = _dbus_strdup (send_destination);
1239 if (send_path && rule->d.send.path == NULL)
1241 if (send_interface && rule->d.send.interface == NULL)
1243 if (send_member && rule->d.send.member == NULL)
1245 if (send_error && rule->d.send.error == NULL)
1247 if (send_destination && rule->d.send.destination == NULL)
1250 else if (receive_interface || receive_member || receive_error || receive_sender ||
1251 receive_path || receive_type || eavesdrop || receive_requested_reply)
1255 if (IS_WILDCARD (receive_interface))
1256 receive_interface = NULL;
1257 if (IS_WILDCARD (receive_member))
1258 receive_member = NULL;
1259 if (IS_WILDCARD (receive_error))
1260 receive_error = NULL;
1261 if (IS_WILDCARD (receive_sender))
1262 receive_sender = NULL;
1263 if (IS_WILDCARD (receive_path))
1264 receive_path = NULL;
1265 if (IS_WILDCARD (receive_type))
1266 receive_type = NULL;
1268 message_type = DBUS_MESSAGE_TYPE_INVALID;
1269 if (receive_type != NULL)
1271 message_type = dbus_message_type_from_string (receive_type);
1272 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1274 dbus_set_error (error, DBUS_ERROR_FAILED,
1275 "Bad message type \"%s\"",
1283 !(strcmp (eavesdrop, "true") == 0 ||
1284 strcmp (eavesdrop, "false") == 0))
1286 dbus_set_error (error, DBUS_ERROR_FAILED,
1287 "Bad value \"%s\" for %s attribute, must be true or false",
1288 "eavesdrop", eavesdrop);
1292 if (receive_requested_reply &&
1293 !(strcmp (receive_requested_reply, "true") == 0 ||
1294 strcmp (receive_requested_reply, "false") == 0))
1296 dbus_set_error (error, DBUS_ERROR_FAILED,
1297 "Bad value \"%s\" for %s attribute, must be true or false",
1298 "receive_requested_reply", receive_requested_reply);
1302 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1307 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1309 if (receive_requested_reply)
1310 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1312 rule->d.receive.message_type = message_type;
1313 rule->d.receive.path = _dbus_strdup (receive_path);
1314 rule->d.receive.interface = _dbus_strdup (receive_interface);
1315 rule->d.receive.member = _dbus_strdup (receive_member);
1316 rule->d.receive.error = _dbus_strdup (receive_error);
1317 rule->d.receive.origin = _dbus_strdup (receive_sender);
1319 if (receive_path && rule->d.receive.path == NULL)
1321 if (receive_interface && rule->d.receive.interface == NULL)
1323 if (receive_member && rule->d.receive.member == NULL)
1325 if (receive_error && rule->d.receive.error == NULL)
1327 if (receive_sender && rule->d.receive.origin == NULL)
1332 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1336 if (IS_WILDCARD (own))
1339 rule->d.own.service_name = _dbus_strdup (own);
1340 if (own && rule->d.own.service_name == NULL)
1345 if (IS_WILDCARD (user))
1347 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1351 rule->d.user.uid = DBUS_UID_UNSET;
1355 DBusString username;
1358 _dbus_string_init_const (&username, user);
1360 if (_dbus_get_user_id (&username, &uid))
1362 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1366 rule->d.user.uid = uid;
1370 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1371 user, element_name);
1377 if (IS_WILDCARD (group))
1379 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1383 rule->d.group.gid = DBUS_GID_UNSET;
1387 DBusString groupname;
1390 _dbus_string_init_const (&groupname, group);
1392 if (_dbus_get_user_id (&groupname, &gid))
1394 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1398 rule->d.group.gid = gid;
1402 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1403 group, element_name);
1408 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1414 pe = peek_element (parser);
1415 _dbus_assert (pe != NULL);
1416 _dbus_assert (pe->type == ELEMENT_POLICY);
1418 switch (pe->d.policy.type)
1420 case POLICY_IGNORED:
1421 /* drop the rule on the floor */
1424 case POLICY_DEFAULT:
1425 if (!bus_policy_append_default_rule (parser->policy, rule))
1428 case POLICY_MANDATORY:
1429 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1433 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1435 dbus_set_error (error, DBUS_ERROR_FAILED,
1436 "<%s> rule cannot be per-user because it has bus-global semantics",
1441 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1446 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1448 dbus_set_error (error, DBUS_ERROR_FAILED,
1449 "<%s> rule cannot be per-group because it has bus-global semantics",
1454 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1460 case POLICY_CONSOLE:
1461 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1467 bus_policy_rule_unref (rule);
1474 BUS_SET_OOM (error);
1477 bus_policy_rule_unref (rule);
1482 start_policy_child (BusConfigParser *parser,
1483 const char *element_name,
1484 const char **attribute_names,
1485 const char **attribute_values,
1488 if (strcmp (element_name, "allow") == 0)
1490 if (!append_rule_from_element (parser, element_name,
1491 attribute_names, attribute_values,
1495 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1497 BUS_SET_OOM (error);
1503 else if (strcmp (element_name, "deny") == 0)
1505 if (!append_rule_from_element (parser, element_name,
1506 attribute_names, attribute_values,
1510 if (push_element (parser, ELEMENT_DENY) == NULL)
1512 BUS_SET_OOM (error);
1520 dbus_set_error (error, DBUS_ERROR_FAILED,
1521 "Element <%s> not allowed inside <%s> in configuration file",
1522 element_name, "policy");
1528 start_selinux_child (BusConfigParser *parser,
1529 const char *element_name,
1530 const char **attribute_names,
1531 const char **attribute_values,
1534 if (strcmp (element_name, "associate") == 0)
1537 const char *context;
1541 if (!locate_attributes (parser, "associate",
1546 "context", &context,
1550 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1552 BUS_SET_OOM (error);
1556 if (own == NULL || context == NULL)
1558 dbus_set_error (error, DBUS_ERROR_FAILED,
1559 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1563 own_copy = _dbus_strdup (own);
1564 if (own_copy == NULL)
1566 context_copy = _dbus_strdup (context);
1567 if (context_copy == NULL)
1570 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1571 own_copy, context_copy))
1573 BUS_SET_OOM (error);
1581 dbus_set_error (error, DBUS_ERROR_FAILED,
1582 "Element <%s> not allowed inside <%s> in configuration file",
1583 element_name, "selinux");
1589 bus_config_parser_start_element (BusConfigParser *parser,
1590 const char *element_name,
1591 const char **attribute_names,
1592 const char **attribute_values,
1597 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1599 /* printf ("START: %s\n", element_name); */
1601 t = top_element_type (parser);
1603 if (t == ELEMENT_NONE)
1605 if (strcmp (element_name, "busconfig") == 0)
1607 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1610 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1612 BUS_SET_OOM (error);
1620 dbus_set_error (error, DBUS_ERROR_FAILED,
1621 "Unknown element <%s> at root of configuration file",
1626 else if (t == ELEMENT_BUSCONFIG)
1628 return start_busconfig_child (parser, element_name,
1629 attribute_names, attribute_values,
1632 else if (t == ELEMENT_POLICY)
1634 return start_policy_child (parser, element_name,
1635 attribute_names, attribute_values,
1638 else if (t == ELEMENT_SELINUX)
1640 return start_selinux_child (parser, element_name,
1641 attribute_names, attribute_values,
1646 dbus_set_error (error, DBUS_ERROR_FAILED,
1647 "Element <%s> is not allowed in this context",
1654 set_limit (BusConfigParser *parser,
1659 dbus_bool_t must_be_positive;
1660 dbus_bool_t must_be_int;
1662 must_be_int = FALSE;
1663 must_be_positive = FALSE;
1665 if (strcmp (name, "max_incoming_bytes") == 0)
1667 must_be_positive = TRUE;
1668 parser->limits.max_incoming_bytes = value;
1670 else if (strcmp (name, "max_outgoing_bytes") == 0)
1672 must_be_positive = TRUE;
1673 parser->limits.max_outgoing_bytes = value;
1675 else if (strcmp (name, "max_message_size") == 0)
1677 must_be_positive = TRUE;
1678 parser->limits.max_message_size = value;
1680 else if (strcmp (name, "service_start_timeout") == 0)
1682 must_be_positive = TRUE;
1684 parser->limits.activation_timeout = value;
1686 else if (strcmp (name, "auth_timeout") == 0)
1688 must_be_positive = TRUE;
1690 parser->limits.auth_timeout = value;
1692 else if (strcmp (name, "reply_timeout") == 0)
1694 must_be_positive = TRUE;
1696 parser->limits.reply_timeout = value;
1698 else if (strcmp (name, "max_completed_connections") == 0)
1700 must_be_positive = TRUE;
1702 parser->limits.max_completed_connections = value;
1704 else if (strcmp (name, "max_incomplete_connections") == 0)
1706 must_be_positive = TRUE;
1708 parser->limits.max_incomplete_connections = value;
1710 else if (strcmp (name, "max_connections_per_user") == 0)
1712 must_be_positive = TRUE;
1714 parser->limits.max_connections_per_user = value;
1716 else if (strcmp (name, "max_pending_service_starts") == 0)
1718 must_be_positive = TRUE;
1720 parser->limits.max_pending_activations = value;
1722 else if (strcmp (name, "max_names_per_connection") == 0)
1724 must_be_positive = TRUE;
1726 parser->limits.max_services_per_connection = value;
1728 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1730 must_be_positive = TRUE;
1732 parser->limits.max_match_rules_per_connection = value;
1734 else if (strcmp (name, "max_replies_per_connection") == 0)
1736 must_be_positive = TRUE;
1738 parser->limits.max_replies_per_connection = value;
1742 dbus_set_error (error, DBUS_ERROR_FAILED,
1743 "There is no limit called \"%s\"\n",
1748 if (must_be_positive && value < 0)
1750 dbus_set_error (error, DBUS_ERROR_FAILED,
1751 "<limit name=\"%s\"> must be a positive number\n",
1757 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1759 dbus_set_error (error, DBUS_ERROR_FAILED,
1760 "<limit name=\"%s\"> value is too large\n",
1769 bus_config_parser_end_element (BusConfigParser *parser,
1770 const char *element_name,
1777 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1779 /* printf ("END: %s\n", element_name); */
1781 t = top_element_type (parser);
1783 if (t == ELEMENT_NONE)
1785 /* should probably be an assertion failure but
1786 * being paranoid about XML parsers
1788 dbus_set_error (error, DBUS_ERROR_FAILED,
1789 "XML parser ended element with no element on the stack");
1793 n = element_type_to_name (t);
1794 _dbus_assert (n != NULL);
1795 if (strcmp (n, element_name) != 0)
1797 /* should probably be an assertion failure but
1798 * being paranoid about XML parsers
1800 dbus_set_error (error, DBUS_ERROR_FAILED,
1801 "XML element <%s> ended but topmost element on the stack was <%s>",
1806 e = peek_element (parser);
1807 _dbus_assert (e != NULL);
1812 _dbus_assert_not_reached ("element in stack has no type");
1815 case ELEMENT_INCLUDE:
1818 case ELEMENT_LISTEN:
1819 case ELEMENT_PIDFILE:
1821 case ELEMENT_SERVICEDIR:
1822 case ELEMENT_INCLUDEDIR:
1824 if (!e->had_content)
1826 dbus_set_error (error, DBUS_ERROR_FAILED,
1827 "XML element <%s> was expected to have content inside it",
1828 element_type_to_name (e->type));
1832 if (e->type == ELEMENT_LIMIT)
1834 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1840 case ELEMENT_BUSCONFIG:
1841 case ELEMENT_POLICY:
1845 case ELEMENT_SELINUX:
1846 case ELEMENT_ASSOCIATE:
1850 pop_element (parser);
1856 all_whitespace (const DBusString *str)
1860 _dbus_string_skip_white (str, 0, &i);
1862 return i == _dbus_string_get_length (str);
1866 make_full_path (const DBusString *basedir,
1867 const DBusString *filename,
1868 DBusString *full_path)
1870 if (_dbus_path_is_absolute (filename))
1872 return _dbus_string_copy (filename, 0, full_path, 0);
1876 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1879 if (!_dbus_concat_dir_and_file (full_path, filename))
1887 include_file (BusConfigParser *parser,
1888 const DBusString *filename,
1889 dbus_bool_t ignore_missing,
1892 /* FIXME good test case for this would load each config file in the
1893 * test suite both alone, and as an include, and check
1894 * that the result is the same
1896 BusConfigParser *included;
1897 const char *filename_str;
1898 DBusError tmp_error;
1900 dbus_error_init (&tmp_error);
1902 filename_str = _dbus_string_get_const_data (filename);
1904 /* Check to make sure this file hasn't already been included. */
1905 if (seen_include (parser, filename))
1907 dbus_set_error (error, DBUS_ERROR_FAILED,
1908 "Circular inclusion of file '%s'",
1913 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
1915 BUS_SET_OOM (error);
1919 /* Since parser is passed in as the parent, included
1920 inherits parser's limits. */
1921 included = bus_config_load (filename, FALSE, parser, &tmp_error);
1923 _dbus_list_pop_last (&parser->included_files);
1925 if (included == NULL)
1927 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1929 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1932 dbus_error_free (&tmp_error);
1937 dbus_move_error (&tmp_error, error);
1943 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1945 if (!merge_included (parser, included, error))
1947 bus_config_parser_unref (included);
1951 /* Copy included's limits back to parser. */
1952 parser->limits = included->limits;
1954 bus_config_parser_unref (included);
1960 include_dir (BusConfigParser *parser,
1961 const DBusString *dirname,
1964 DBusString filename;
1966 DBusError tmp_error;
1969 if (!_dbus_string_init (&filename))
1971 BUS_SET_OOM (error);
1977 dir = _dbus_directory_open (dirname, error);
1982 dbus_error_init (&tmp_error);
1983 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1985 DBusString full_path;
1987 if (!_dbus_string_init (&full_path))
1989 BUS_SET_OOM (error);
1993 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1995 BUS_SET_OOM (error);
1996 _dbus_string_free (&full_path);
2000 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2002 BUS_SET_OOM (error);
2003 _dbus_string_free (&full_path);
2007 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2009 if (!include_file (parser, &full_path, TRUE, error))
2011 _dbus_string_free (&full_path);
2016 _dbus_string_free (&full_path);
2019 if (dbus_error_is_set (&tmp_error))
2021 dbus_move_error (&tmp_error, error);
2028 _dbus_string_free (&filename);
2031 _dbus_directory_close (dir);
2037 bus_config_parser_content (BusConfigParser *parser,
2038 const DBusString *content,
2043 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2049 _dbus_string_get_const_data (content, &c_str);
2051 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2055 e = peek_element (parser);
2058 dbus_set_error (error, DBUS_ERROR_FAILED,
2059 "Text content outside of any XML element in configuration file");
2062 else if (e->had_content)
2064 _dbus_assert_not_reached ("Element had multiple content blocks");
2068 switch (top_element_type (parser))
2071 _dbus_assert_not_reached ("element at top of stack has no type");
2074 case ELEMENT_BUSCONFIG:
2075 case ELEMENT_POLICY:
2079 case ELEMENT_SELINUX:
2080 case ELEMENT_ASSOCIATE:
2081 if (all_whitespace (content))
2085 dbus_set_error (error, DBUS_ERROR_FAILED,
2086 "No text content expected inside XML element %s in configuration file",
2087 element_type_to_name (top_element_type (parser)));
2091 case ELEMENT_PIDFILE:
2095 e->had_content = TRUE;
2097 if (!_dbus_string_copy_data (content, &s))
2100 dbus_free (parser->pidfile);
2101 parser->pidfile = s;
2105 case ELEMENT_INCLUDE:
2107 DBusString full_path, selinux_policy_root;
2109 e->had_content = TRUE;
2111 if (e->d.include.if_selinux_enabled
2112 && !bus_selinux_enabled ())
2115 if (!_dbus_string_init (&full_path))
2118 if (e->d.include.selinux_root_relative)
2120 if (!bus_selinux_get_policy_root ())
2122 dbus_set_error (error, DBUS_ERROR_FAILED,
2123 "Could not determine SELinux policy root for relative inclusion");
2124 _dbus_string_free (&full_path);
2127 _dbus_string_init_const (&selinux_policy_root,
2128 bus_selinux_get_policy_root ());
2129 if (!make_full_path (&selinux_policy_root, content, &full_path))
2131 _dbus_string_free (&full_path);
2135 else if (!make_full_path (&parser->basedir, content, &full_path))
2137 _dbus_string_free (&full_path);
2141 if (!include_file (parser, &full_path,
2142 e->d.include.ignore_missing, error))
2144 _dbus_string_free (&full_path);
2148 _dbus_string_free (&full_path);
2152 case ELEMENT_INCLUDEDIR:
2154 DBusString full_path;
2156 e->had_content = TRUE;
2158 if (!_dbus_string_init (&full_path))
2161 if (!make_full_path (&parser->basedir, content, &full_path))
2163 _dbus_string_free (&full_path);
2167 if (!include_dir (parser, &full_path, error))
2169 _dbus_string_free (&full_path);
2173 _dbus_string_free (&full_path);
2181 e->had_content = TRUE;
2183 if (!_dbus_string_copy_data (content, &s))
2186 dbus_free (parser->user);
2195 e->had_content = TRUE;
2197 if (!_dbus_string_copy_data (content, &s))
2200 dbus_free (parser->bus_type);
2201 parser->bus_type = s;
2205 case ELEMENT_LISTEN:
2209 e->had_content = TRUE;
2211 if (!_dbus_string_copy_data (content, &s))
2214 if (!_dbus_list_append (&parser->listen_on,
2227 e->had_content = TRUE;
2229 if (!_dbus_string_copy_data (content, &s))
2232 if (!_dbus_list_append (&parser->mechanisms,
2241 case ELEMENT_SERVICEDIR:
2244 DBusString full_path;
2246 e->had_content = TRUE;
2248 if (!_dbus_string_init (&full_path))
2251 if (!make_full_path (&parser->basedir, content, &full_path))
2253 _dbus_string_free (&full_path);
2257 if (!_dbus_string_copy_data (&full_path, &s))
2259 _dbus_string_free (&full_path);
2263 if (!_dbus_list_append (&parser->service_dirs, s))
2265 _dbus_string_free (&full_path);
2270 _dbus_string_free (&full_path);
2278 e->had_content = TRUE;
2281 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2283 dbus_set_error (error, DBUS_ERROR_FAILED,
2284 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2289 e->d.limit.value = val;
2291 _dbus_verbose ("Loaded value %ld for limit %s\n",
2298 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2302 BUS_SET_OOM (error);
2307 bus_config_parser_finished (BusConfigParser *parser,
2310 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2312 if (parser->stack != NULL)
2314 dbus_set_error (error, DBUS_ERROR_FAILED,
2315 "Element <%s> was not closed in configuration file",
2316 element_type_to_name (top_element_type (parser)));
2321 if (parser->is_toplevel && parser->listen_on == NULL)
2323 dbus_set_error (error, DBUS_ERROR_FAILED,
2324 "Configuration file needs one or more <listen> elements giving addresses");
2332 bus_config_parser_get_user (BusConfigParser *parser)
2334 return parser->user;
2338 bus_config_parser_get_type (BusConfigParser *parser)
2340 return parser->bus_type;
2344 bus_config_parser_get_addresses (BusConfigParser *parser)
2346 return &parser->listen_on;
2350 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2352 return &parser->mechanisms;
2356 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2358 return &parser->service_dirs;
2362 bus_config_parser_get_fork (BusConfigParser *parser)
2364 return parser->fork;
2368 bus_config_parser_get_pidfile (BusConfigParser *parser)
2370 return parser->pidfile;
2374 bus_config_parser_steal_policy (BusConfigParser *parser)
2378 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2380 policy = parser->policy;
2382 parser->policy = NULL;
2387 /* Overwrite any limits that were set in the configuration file */
2389 bus_config_parser_get_limits (BusConfigParser *parser,
2392 *limits = parser->limits;
2396 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2398 DBusHashTable *table;
2400 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2402 table = parser->service_context_table;
2404 parser->service_context_table = NULL;
2409 #ifdef DBUS_BUILD_TESTS
2420 do_load (const DBusString *full_path,
2422 dbus_bool_t oom_possible)
2424 BusConfigParser *parser;
2427 dbus_error_init (&error);
2429 parser = bus_config_load (full_path, TRUE, NULL, &error);
2432 _DBUS_ASSERT_ERROR_IS_SET (&error);
2435 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2437 _dbus_verbose ("Failed to load valid file due to OOM\n");
2438 dbus_error_free (&error);
2441 else if (validity == VALID)
2443 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2446 dbus_error_free (&error);
2451 dbus_error_free (&error);
2457 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2459 bus_config_parser_unref (parser);
2461 if (validity == INVALID)
2463 _dbus_warn ("Accepted invalid file\n");
2473 const DBusString *full_path;
2478 check_loader_oom_func (void *data)
2480 LoaderOomData *d = data;
2482 return do_load (d->full_path, d->validity, TRUE);
2486 process_test_valid_subdir (const DBusString *test_base_dir,
2490 DBusString test_directory;
2491 DBusString filename;
2499 if (!_dbus_string_init (&test_directory))
2500 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2502 _dbus_string_init_const (&filename, subdir);
2504 if (!_dbus_string_copy (test_base_dir, 0,
2505 &test_directory, 0))
2506 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2508 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2509 _dbus_assert_not_reached ("couldn't allocate full path");
2511 _dbus_string_free (&filename);
2512 if (!_dbus_string_init (&filename))
2513 _dbus_assert_not_reached ("didn't allocate filename string\n");
2515 dbus_error_init (&error);
2516 dir = _dbus_directory_open (&test_directory, &error);
2519 _dbus_warn ("Could not open %s: %s\n",
2520 _dbus_string_get_const_data (&test_directory),
2522 dbus_error_free (&error);
2526 if (validity == VALID)
2527 printf ("Testing valid files:\n");
2528 else if (validity == INVALID)
2529 printf ("Testing invalid files:\n");
2531 printf ("Testing unknown files:\n");
2534 while (_dbus_directory_get_next_file (dir, &filename, &error))
2536 DBusString full_path;
2539 if (!_dbus_string_init (&full_path))
2540 _dbus_assert_not_reached ("couldn't init string");
2542 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2543 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2545 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2546 _dbus_assert_not_reached ("couldn't concat file to dir");
2548 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2550 _dbus_verbose ("Skipping non-.conf file %s\n",
2551 _dbus_string_get_const_data (&filename));
2552 _dbus_string_free (&full_path);
2556 printf (" %s\n", _dbus_string_get_const_data (&filename));
2558 _dbus_verbose (" expecting %s\n",
2559 validity == VALID ? "valid" :
2560 (validity == INVALID ? "invalid" :
2561 (validity == UNKNOWN ? "unknown" : "???")));
2563 d.full_path = &full_path;
2564 d.validity = validity;
2566 /* FIXME hackaround for an expat problem, see
2567 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2568 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2570 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2571 if (!check_loader_oom_func (&d))
2572 _dbus_assert_not_reached ("test failed");
2574 _dbus_string_free (&full_path);
2577 if (dbus_error_is_set (&error))
2579 _dbus_warn ("Could not get next file in %s: %s\n",
2580 _dbus_string_get_const_data (&test_directory),
2582 dbus_error_free (&error);
2591 _dbus_directory_close (dir);
2592 _dbus_string_free (&test_directory);
2593 _dbus_string_free (&filename);
2599 bools_equal (dbus_bool_t a,
2606 strings_equal_or_both_null (const char *a,
2609 if (a == NULL || b == NULL)
2612 return !strcmp (a, b);
2616 elements_equal (const Element *a,
2619 if (a->type != b->type)
2622 if (!bools_equal (a->had_content, b->had_content))
2628 case ELEMENT_INCLUDE:
2629 if (!bools_equal (a->d.include.ignore_missing,
2630 b->d.include.ignore_missing))
2634 case ELEMENT_POLICY:
2635 if (a->d.policy.type != b->d.policy.type)
2637 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
2642 if (strcmp (a->d.limit.name, b->d.limit.name))
2644 if (a->d.limit.value != b->d.limit.value)
2658 lists_of_elements_equal (DBusList *a,
2667 while (ia != NULL && ib != NULL)
2669 if (elements_equal (ia->data, ib->data))
2671 ia = _dbus_list_get_next_link (&a, ia);
2672 ib = _dbus_list_get_next_link (&b, ib);
2675 return ia == NULL && ib == NULL;
2679 lists_of_c_strings_equal (DBusList *a,
2688 while (ia != NULL && ib != NULL)
2690 if (strcmp (ia->data, ib->data))
2692 ia = _dbus_list_get_next_link (&a, ia);
2693 ib = _dbus_list_get_next_link (&b, ib);
2696 return ia == NULL && ib == NULL;
2700 limits_equal (const BusLimits *a,
2704 (a->max_incoming_bytes == b->max_incoming_bytes
2705 || a->max_outgoing_bytes == b->max_outgoing_bytes
2706 || a->max_message_size == b->max_message_size
2707 || a->activation_timeout == b->activation_timeout
2708 || a->auth_timeout == b->auth_timeout
2709 || a->max_completed_connections == b->max_completed_connections
2710 || a->max_incomplete_connections == b->max_incomplete_connections
2711 || a->max_connections_per_user == b->max_connections_per_user
2712 || a->max_pending_activations == b->max_pending_activations
2713 || a->max_services_per_connection == b->max_services_per_connection
2714 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2715 || a->max_replies_per_connection == b->max_replies_per_connection
2716 || a->reply_timeout == b->reply_timeout);
2720 config_parsers_equal (const BusConfigParser *a,
2721 const BusConfigParser *b)
2723 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2726 if (!lists_of_elements_equal (a->stack, b->stack))
2729 if (!strings_equal_or_both_null (a->user, b->user))
2732 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2735 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2738 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2741 /* FIXME: compare policy */
2743 /* FIXME: compare service selinux ID table */
2745 if (! limits_equal (&a->limits, &b->limits))
2748 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2751 if (! bools_equal (a->fork, b->fork))
2754 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2761 all_are_equiv (const DBusString *target_directory)
2763 DBusString filename;
2765 BusConfigParser *first_parser;
2766 BusConfigParser *parser;
2772 first_parser = NULL;
2776 if (!_dbus_string_init (&filename))
2777 _dbus_assert_not_reached ("didn't allocate filename string");
2779 dbus_error_init (&error);
2780 dir = _dbus_directory_open (target_directory, &error);
2783 _dbus_warn ("Could not open %s: %s\n",
2784 _dbus_string_get_const_data (target_directory),
2786 dbus_error_free (&error);
2790 printf ("Comparing equivalent files:\n");
2793 while (_dbus_directory_get_next_file (dir, &filename, &error))
2795 DBusString full_path;
2797 if (!_dbus_string_init (&full_path))
2798 _dbus_assert_not_reached ("couldn't init string");
2800 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2801 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2803 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2804 _dbus_assert_not_reached ("couldn't concat file to dir");
2806 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2808 _dbus_verbose ("Skipping non-.conf file %s\n",
2809 _dbus_string_get_const_data (&filename));
2810 _dbus_string_free (&full_path);
2814 printf (" %s\n", _dbus_string_get_const_data (&filename));
2816 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2820 _dbus_warn ("Could not load file %s: %s\n",
2821 _dbus_string_get_const_data (&full_path),
2823 _dbus_string_free (&full_path);
2824 dbus_error_free (&error);
2827 else if (first_parser == NULL)
2829 _dbus_string_free (&full_path);
2830 first_parser = parser;
2834 _dbus_string_free (&full_path);
2835 equal = config_parsers_equal (first_parser, parser);
2836 bus_config_parser_unref (parser);
2845 _dbus_string_free (&filename);
2847 bus_config_parser_unref (first_parser);
2849 _dbus_directory_close (dir);
2856 process_test_equiv_subdir (const DBusString *test_base_dir,
2859 DBusString test_directory;
2860 DBusString filename;
2869 if (!_dbus_string_init (&test_directory))
2870 _dbus_assert_not_reached ("didn't allocate test_directory");
2872 _dbus_string_init_const (&filename, subdir);
2874 if (!_dbus_string_copy (test_base_dir, 0,
2875 &test_directory, 0))
2876 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2878 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2879 _dbus_assert_not_reached ("couldn't allocate full path");
2881 _dbus_string_free (&filename);
2882 if (!_dbus_string_init (&filename))
2883 _dbus_assert_not_reached ("didn't allocate filename string");
2885 dbus_error_init (&error);
2886 dir = _dbus_directory_open (&test_directory, &error);
2889 _dbus_warn ("Could not open %s: %s\n",
2890 _dbus_string_get_const_data (&test_directory),
2892 dbus_error_free (&error);
2896 while (_dbus_directory_get_next_file (dir, &filename, &error))
2898 DBusString full_path;
2900 /* Skip CVS's magic directories! */
2901 if (_dbus_string_equal_c_str (&filename, "CVS"))
2904 if (!_dbus_string_init (&full_path))
2905 _dbus_assert_not_reached ("couldn't init string");
2907 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2908 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2910 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2911 _dbus_assert_not_reached ("couldn't concat file to dir");
2913 equal = all_are_equiv (&full_path);
2914 _dbus_string_free (&full_path);
2923 _dbus_string_free (&test_directory);
2924 _dbus_string_free (&filename);
2926 _dbus_directory_close (dir);
2933 bus_config_parser_test (const DBusString *test_data_dir)
2935 if (test_data_dir == NULL ||
2936 _dbus_string_get_length (test_data_dir) == 0)
2938 printf ("No test data\n");
2942 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
2945 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
2948 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
2954 #endif /* DBUS_BUILD_TESTS */