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 service_dirs_find_dir (DBusList **service_dirs,
295 _dbus_assert (dir != NULL);
297 for (link = *service_dirs; link; link = link->next)
299 const char *link_dir;
301 link_dir = (const char *)link->data;
302 if (strcmp (dir, link_dir) == 0)
310 service_dirs_append_unique_or_free (DBusList **service_dirs,
313 if (!service_dirs_find_dir (service_dirs, dir))
314 return _dbus_list_append (service_dirs, dir);
321 service_dirs_append_link_unique_or_free (DBusList **service_dirs,
324 if (!service_dirs_find_dir (service_dirs, dir_link->data))
326 _dbus_list_append_link (service_dirs, dir_link);
330 dbus_free (dir_link->data);
331 _dbus_list_free_link (dir_link);
336 merge_included (BusConfigParser *parser,
337 BusConfigParser *included,
342 if (!bus_policy_merge (parser->policy,
349 if (!merge_service_context_hash (parser->service_context_table,
350 included->service_context_table))
356 if (included->user != NULL)
358 dbus_free (parser->user);
359 parser->user = included->user;
360 included->user = NULL;
363 if (included->bus_type != NULL)
365 dbus_free (parser->bus_type);
366 parser->bus_type = included->bus_type;
367 included->bus_type = NULL;
373 if (included->pidfile != NULL)
375 dbus_free (parser->pidfile);
376 parser->pidfile = included->pidfile;
377 included->pidfile = NULL;
380 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
381 _dbus_list_append_link (&parser->listen_on, link);
383 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
384 _dbus_list_append_link (&parser->mechanisms, link);
386 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
387 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
389 while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
390 _dbus_list_append_link (&parser->conf_dirs, link);
396 seen_include (BusConfigParser *parser,
397 const DBusString *file)
401 iter = parser->included_files;
404 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
407 iter = _dbus_list_get_next_link (&parser->included_files, iter);
414 bus_config_parser_new (const DBusString *basedir,
415 dbus_bool_t is_toplevel,
416 const BusConfigParser *parent)
418 BusConfigParser *parser;
420 parser = dbus_new0 (BusConfigParser, 1);
424 parser->is_toplevel = !!is_toplevel;
426 if (!_dbus_string_init (&parser->basedir))
432 if (((parser->policy = bus_policy_new ()) == NULL) ||
433 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
434 ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
436 dbus_free)) == NULL))
439 bus_policy_unref (parser->policy);
441 _dbus_string_free (&parser->basedir);
449 /* Initialize the parser's limits from the parent. */
450 parser->limits = parent->limits;
452 /* Use the parent's list of included_files to avoid
453 circular inclusions. */
454 parser->included_files = parent->included_files;
459 /* Make up some numbers! woot! */
460 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
461 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
462 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
464 /* Making this long means the user has to wait longer for an error
465 * message if something screws up, but making it too short means
466 * they might see a false failure.
468 parser->limits.activation_timeout = 25000; /* 25 seconds */
470 /* Making this long risks making a DOS attack easier, but too short
471 * and legitimate auth will fail. If interactive auth (ask user for
472 * password) is allowed, then potentially it has to be quite long.
474 parser->limits.auth_timeout = 30000; /* 30 seconds */
476 parser->limits.max_incomplete_connections = 32;
477 parser->limits.max_connections_per_user = 128;
479 /* Note that max_completed_connections / max_connections_per_user
480 * is the number of users that would have to work together to
481 * DOS all the other users.
483 parser->limits.max_completed_connections = 1024;
485 parser->limits.max_pending_activations = 256;
486 parser->limits.max_services_per_connection = 256;
488 parser->limits.max_match_rules_per_connection = 512;
490 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
491 parser->limits.max_replies_per_connection = 32;
494 parser->refcount = 1;
500 bus_config_parser_ref (BusConfigParser *parser)
502 _dbus_assert (parser->refcount > 0);
504 parser->refcount += 1;
510 bus_config_parser_unref (BusConfigParser *parser)
512 _dbus_assert (parser->refcount > 0);
514 parser->refcount -= 1;
516 if (parser->refcount == 0)
518 while (parser->stack != NULL)
519 pop_element (parser);
521 dbus_free (parser->user);
522 dbus_free (parser->bus_type);
523 dbus_free (parser->pidfile);
525 _dbus_list_foreach (&parser->listen_on,
526 (DBusForeachFunction) dbus_free,
529 _dbus_list_clear (&parser->listen_on);
531 _dbus_list_foreach (&parser->service_dirs,
532 (DBusForeachFunction) dbus_free,
535 _dbus_list_clear (&parser->service_dirs);
537 _dbus_list_foreach (&parser->conf_dirs,
538 (DBusForeachFunction) dbus_free,
541 _dbus_list_clear (&parser->conf_dirs);
543 _dbus_list_foreach (&parser->mechanisms,
544 (DBusForeachFunction) dbus_free,
547 _dbus_list_clear (&parser->mechanisms);
549 _dbus_string_free (&parser->basedir);
552 bus_policy_unref (parser->policy);
554 if (parser->service_context_table)
555 _dbus_hash_table_unref (parser->service_context_table);
562 bus_config_parser_check_doctype (BusConfigParser *parser,
566 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
568 if (strcmp (doctype, "busconfig") != 0)
570 dbus_set_error (error,
572 "Configuration file has the wrong document type %s",
587 locate_attributes (BusConfigParser *parser,
588 const char *element_name,
589 const char **attribute_names,
590 const char **attribute_values,
592 const char *first_attribute_name,
593 const char **first_attribute_retloc,
601 LocateAttr attrs[MAX_ATTRS];
605 _dbus_assert (first_attribute_name != NULL);
606 _dbus_assert (first_attribute_retloc != NULL);
611 attrs[0].name = first_attribute_name;
612 attrs[0].retloc = first_attribute_retloc;
613 *first_attribute_retloc = NULL;
615 va_start (args, first_attribute_retloc);
617 name = va_arg (args, const char*);
618 retloc = va_arg (args, const char**);
622 _dbus_assert (retloc != NULL);
623 _dbus_assert (n_attrs < MAX_ATTRS);
625 attrs[n_attrs].name = name;
626 attrs[n_attrs].retloc = retloc;
630 name = va_arg (args, const char*);
631 retloc = va_arg (args, const char**);
640 while (attribute_names[i])
649 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
651 retloc = attrs[j].retloc;
655 dbus_set_error (error, DBUS_ERROR_FAILED,
656 "Attribute \"%s\" repeated twice on the same <%s> element",
657 attrs[j].name, element_name);
662 *retloc = attribute_values[i];
671 dbus_set_error (error, DBUS_ERROR_FAILED,
672 "Attribute \"%s\" is invalid on <%s> element in this context",
673 attribute_names[i], element_name);
686 check_no_attributes (BusConfigParser *parser,
687 const char *element_name,
688 const char **attribute_names,
689 const char **attribute_values,
692 if (attribute_names[0] != NULL)
694 dbus_set_error (error, DBUS_ERROR_FAILED,
695 "Attribute \"%s\" is invalid on <%s> element in this context",
696 attribute_names[0], element_name);
704 start_busconfig_child (BusConfigParser *parser,
705 const char *element_name,
706 const char **attribute_names,
707 const char **attribute_values,
710 if (strcmp (element_name, "user") == 0)
712 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
715 if (push_element (parser, ELEMENT_USER) == NULL)
723 else if (strcmp (element_name, "type") == 0)
725 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
728 if (push_element (parser, ELEMENT_TYPE) == NULL)
736 else if (strcmp (element_name, "fork") == 0)
738 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
741 if (push_element (parser, ELEMENT_FORK) == NULL)
751 else if (strcmp (element_name, "pidfile") == 0)
753 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
756 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
764 else if (strcmp (element_name, "listen") == 0)
766 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
769 if (push_element (parser, ELEMENT_LISTEN) == NULL)
777 else if (strcmp (element_name, "auth") == 0)
779 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
782 if (push_element (parser, ELEMENT_AUTH) == NULL)
790 else if (strcmp (element_name, "includedir") == 0)
792 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
795 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
803 else if (strcmp (element_name, "servicedir") == 0)
805 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
808 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
816 else if (strcmp (element_name, "include") == 0)
819 const char *if_selinux_enabled;
820 const char *ignore_missing;
821 const char *selinux_root_relative;
823 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
829 e->d.include.ignore_missing = FALSE;
830 e->d.include.if_selinux_enabled = FALSE;
831 e->d.include.selinux_root_relative = FALSE;
833 if (!locate_attributes (parser, "include",
837 "ignore_missing", &ignore_missing,
838 "if_selinux_enabled", &if_selinux_enabled,
839 "selinux_root_relative", &selinux_root_relative,
843 if (ignore_missing != NULL)
845 if (strcmp (ignore_missing, "yes") == 0)
846 e->d.include.ignore_missing = TRUE;
847 else if (strcmp (ignore_missing, "no") == 0)
848 e->d.include.ignore_missing = FALSE;
851 dbus_set_error (error, DBUS_ERROR_FAILED,
852 "ignore_missing attribute must have value \"yes\" or \"no\"");
857 if (if_selinux_enabled != NULL)
859 if (strcmp (if_selinux_enabled, "yes") == 0)
860 e->d.include.if_selinux_enabled = TRUE;
861 else if (strcmp (if_selinux_enabled, "no") == 0)
862 e->d.include.if_selinux_enabled = FALSE;
865 dbus_set_error (error, DBUS_ERROR_FAILED,
866 "if_selinux_enabled attribute must have value"
867 " \"yes\" or \"no\"");
872 if (selinux_root_relative != NULL)
874 if (strcmp (selinux_root_relative, "yes") == 0)
875 e->d.include.selinux_root_relative = TRUE;
876 else if (strcmp (selinux_root_relative, "no") == 0)
877 e->d.include.selinux_root_relative = FALSE;
880 dbus_set_error (error, DBUS_ERROR_FAILED,
881 "selinux_root_relative attribute must have value"
882 " \"yes\" or \"no\"");
889 else if (strcmp (element_name, "policy") == 0)
895 const char *at_console;
897 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
903 e->d.policy.type = POLICY_IGNORED;
905 if (!locate_attributes (parser, "policy",
912 "at_console", &at_console,
916 if (((context && user) ||
917 (context && group) ||
918 (context && at_console)) ||
920 (user && at_console)) ||
921 (group && at_console) ||
922 !(context || user || group || at_console))
924 dbus_set_error (error, DBUS_ERROR_FAILED,
925 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
931 if (strcmp (context, "default") == 0)
933 e->d.policy.type = POLICY_DEFAULT;
935 else if (strcmp (context, "mandatory") == 0)
937 e->d.policy.type = POLICY_MANDATORY;
941 dbus_set_error (error, DBUS_ERROR_FAILED,
942 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
947 else if (user != NULL)
950 _dbus_string_init_const (&username, user);
952 if (_dbus_get_user_id (&username,
953 &e->d.policy.gid_uid_or_at_console))
954 e->d.policy.type = POLICY_USER;
956 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
959 else if (group != NULL)
961 DBusString group_name;
962 _dbus_string_init_const (&group_name, group);
964 if (_dbus_get_group_id (&group_name,
965 &e->d.policy.gid_uid_or_at_console))
966 e->d.policy.type = POLICY_GROUP;
968 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
971 else if (at_console != NULL)
974 t = (strcmp (at_console, "true") == 0);
975 if (t || strcmp (at_console, "false") == 0)
977 e->d.policy.gid_uid_or_at_console = t;
978 e->d.policy.type = POLICY_CONSOLE;
982 dbus_set_error (error, DBUS_ERROR_FAILED,
983 "Unknown value \"%s\" for at_console in message bus configuration file",
991 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
996 else if (strcmp (element_name, "limit") == 0)
1001 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1003 BUS_SET_OOM (error);
1007 if (!locate_attributes (parser, "limit",
1017 dbus_set_error (error, DBUS_ERROR_FAILED,
1018 "<limit> element must have a \"name\" attribute");
1022 e->d.limit.name = _dbus_strdup (name);
1023 if (e->d.limit.name == NULL)
1025 BUS_SET_OOM (error);
1031 else if (strcmp (element_name, "selinux") == 0)
1033 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1036 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1038 BUS_SET_OOM (error);
1046 dbus_set_error (error, DBUS_ERROR_FAILED,
1047 "Element <%s> not allowed inside <%s> in configuration file",
1048 element_name, "busconfig");
1054 append_rule_from_element (BusConfigParser *parser,
1055 const char *element_name,
1056 const char **attribute_names,
1057 const char **attribute_values,
1061 const char *send_interface;
1062 const char *send_member;
1063 const char *send_error;
1064 const char *send_destination;
1065 const char *send_path;
1066 const char *send_type;
1067 const char *receive_interface;
1068 const char *receive_member;
1069 const char *receive_error;
1070 const char *receive_sender;
1071 const char *receive_path;
1072 const char *receive_type;
1073 const char *eavesdrop;
1074 const char *send_requested_reply;
1075 const char *receive_requested_reply;
1080 BusPolicyRule *rule;
1082 if (!locate_attributes (parser, element_name,
1086 "send_interface", &send_interface,
1087 "send_member", &send_member,
1088 "send_error", &send_error,
1089 "send_destination", &send_destination,
1090 "send_path", &send_path,
1091 "send_type", &send_type,
1092 "receive_interface", &receive_interface,
1093 "receive_member", &receive_member,
1094 "receive_error", &receive_error,
1095 "receive_sender", &receive_sender,
1096 "receive_path", &receive_path,
1097 "receive_type", &receive_type,
1098 "eavesdrop", &eavesdrop,
1099 "send_requested_reply", &send_requested_reply,
1100 "receive_requested_reply", &receive_requested_reply,
1107 if (!(send_interface || send_member || send_error || send_destination ||
1108 send_type || send_path ||
1109 receive_interface || receive_member || receive_error || receive_sender ||
1110 receive_type || receive_path || eavesdrop ||
1111 send_requested_reply || receive_requested_reply ||
1112 own || user || group))
1114 dbus_set_error (error, DBUS_ERROR_FAILED,
1115 "Element <%s> must have one or more attributes",
1120 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1121 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1123 dbus_set_error (error, DBUS_ERROR_FAILED,
1124 "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.",
1129 /* Allowed combinations of elements are:
1131 * base, must be all send or all receive:
1134 * interface + member
1137 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1138 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1140 * user, group, own must occur alone
1142 * Pretty sure the below stuff is broken, FIXME think about it more.
1145 if (((send_interface && send_error) ||
1146 (send_interface && receive_interface) ||
1147 (send_interface && receive_member) ||
1148 (send_interface && receive_error) ||
1149 (send_interface && receive_sender) ||
1150 (send_interface && eavesdrop) ||
1151 (send_interface && receive_requested_reply) ||
1152 (send_interface && own) ||
1153 (send_interface && user) ||
1154 (send_interface && group)) ||
1156 ((send_member && send_error) ||
1157 (send_member && receive_interface) ||
1158 (send_member && receive_member) ||
1159 (send_member && receive_error) ||
1160 (send_member && receive_sender) ||
1161 (send_member && eavesdrop) ||
1162 (send_member && receive_requested_reply) ||
1163 (send_member && own) ||
1164 (send_member && user) ||
1165 (send_member && group)) ||
1167 ((send_error && receive_interface) ||
1168 (send_error && receive_member) ||
1169 (send_error && receive_error) ||
1170 (send_error && receive_sender) ||
1171 (send_error && eavesdrop) ||
1172 (send_error && receive_requested_reply) ||
1173 (send_error && own) ||
1174 (send_error && user) ||
1175 (send_error && group)) ||
1177 ((send_destination && receive_interface) ||
1178 (send_destination && receive_member) ||
1179 (send_destination && receive_error) ||
1180 (send_destination && receive_sender) ||
1181 (send_destination && eavesdrop) ||
1182 (send_destination && receive_requested_reply) ||
1183 (send_destination && own) ||
1184 (send_destination && user) ||
1185 (send_destination && group)) ||
1187 ((send_type && receive_interface) ||
1188 (send_type && receive_member) ||
1189 (send_type && receive_error) ||
1190 (send_type && receive_sender) ||
1191 (send_type && eavesdrop) ||
1192 (send_type && receive_requested_reply) ||
1193 (send_type && own) ||
1194 (send_type && user) ||
1195 (send_type && group)) ||
1197 ((send_path && receive_interface) ||
1198 (send_path && receive_member) ||
1199 (send_path && receive_error) ||
1200 (send_path && receive_sender) ||
1201 (send_path && eavesdrop) ||
1202 (send_path && receive_requested_reply) ||
1203 (send_path && own) ||
1204 (send_path && user) ||
1205 (send_path && group)) ||
1207 ((send_requested_reply && receive_interface) ||
1208 (send_requested_reply && receive_member) ||
1209 (send_requested_reply && receive_error) ||
1210 (send_requested_reply && receive_sender) ||
1211 (send_requested_reply && eavesdrop) ||
1212 (send_requested_reply && receive_requested_reply) ||
1213 (send_requested_reply && own) ||
1214 (send_requested_reply && user) ||
1215 (send_requested_reply && group)) ||
1217 ((receive_interface && receive_error) ||
1218 (receive_interface && own) ||
1219 (receive_interface && user) ||
1220 (receive_interface && group)) ||
1222 ((receive_member && receive_error) ||
1223 (receive_member && own) ||
1224 (receive_member && user) ||
1225 (receive_member && group)) ||
1227 ((receive_error && own) ||
1228 (receive_error && user) ||
1229 (receive_error && group)) ||
1231 ((eavesdrop && own) ||
1232 (eavesdrop && user) ||
1233 (eavesdrop && group)) ||
1235 ((receive_requested_reply && own) ||
1236 (receive_requested_reply && user) ||
1237 (receive_requested_reply && group)) ||
1244 dbus_set_error (error, DBUS_ERROR_FAILED,
1245 "Invalid combination of attributes on element <%s>",
1252 /* In BusPolicyRule, NULL represents wildcard.
1253 * In the config file, '*' represents it.
1255 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1257 if (send_interface || send_member || send_error || send_destination ||
1258 send_path || send_type || send_requested_reply)
1262 if (IS_WILDCARD (send_interface))
1263 send_interface = NULL;
1264 if (IS_WILDCARD (send_member))
1266 if (IS_WILDCARD (send_error))
1268 if (IS_WILDCARD (send_destination))
1269 send_destination = NULL;
1270 if (IS_WILDCARD (send_path))
1272 if (IS_WILDCARD (send_type))
1275 message_type = DBUS_MESSAGE_TYPE_INVALID;
1276 if (send_type != NULL)
1278 message_type = dbus_message_type_from_string (send_type);
1279 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1281 dbus_set_error (error, DBUS_ERROR_FAILED,
1282 "Bad message type \"%s\"",
1288 if (send_requested_reply &&
1289 !(strcmp (send_requested_reply, "true") == 0 ||
1290 strcmp (send_requested_reply, "false") == 0))
1292 dbus_set_error (error, DBUS_ERROR_FAILED,
1293 "Bad value \"%s\" for %s attribute, must be true or false",
1294 "send_requested_reply", send_requested_reply);
1298 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1302 if (send_requested_reply)
1303 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1305 rule->d.send.message_type = message_type;
1306 rule->d.send.path = _dbus_strdup (send_path);
1307 rule->d.send.interface = _dbus_strdup (send_interface);
1308 rule->d.send.member = _dbus_strdup (send_member);
1309 rule->d.send.error = _dbus_strdup (send_error);
1310 rule->d.send.destination = _dbus_strdup (send_destination);
1311 if (send_path && rule->d.send.path == NULL)
1313 if (send_interface && rule->d.send.interface == NULL)
1315 if (send_member && rule->d.send.member == NULL)
1317 if (send_error && rule->d.send.error == NULL)
1319 if (send_destination && rule->d.send.destination == NULL)
1322 else if (receive_interface || receive_member || receive_error || receive_sender ||
1323 receive_path || receive_type || eavesdrop || receive_requested_reply)
1327 if (IS_WILDCARD (receive_interface))
1328 receive_interface = NULL;
1329 if (IS_WILDCARD (receive_member))
1330 receive_member = NULL;
1331 if (IS_WILDCARD (receive_error))
1332 receive_error = NULL;
1333 if (IS_WILDCARD (receive_sender))
1334 receive_sender = NULL;
1335 if (IS_WILDCARD (receive_path))
1336 receive_path = NULL;
1337 if (IS_WILDCARD (receive_type))
1338 receive_type = NULL;
1340 message_type = DBUS_MESSAGE_TYPE_INVALID;
1341 if (receive_type != NULL)
1343 message_type = dbus_message_type_from_string (receive_type);
1344 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1346 dbus_set_error (error, DBUS_ERROR_FAILED,
1347 "Bad message type \"%s\"",
1355 !(strcmp (eavesdrop, "true") == 0 ||
1356 strcmp (eavesdrop, "false") == 0))
1358 dbus_set_error (error, DBUS_ERROR_FAILED,
1359 "Bad value \"%s\" for %s attribute, must be true or false",
1360 "eavesdrop", eavesdrop);
1364 if (receive_requested_reply &&
1365 !(strcmp (receive_requested_reply, "true") == 0 ||
1366 strcmp (receive_requested_reply, "false") == 0))
1368 dbus_set_error (error, DBUS_ERROR_FAILED,
1369 "Bad value \"%s\" for %s attribute, must be true or false",
1370 "receive_requested_reply", receive_requested_reply);
1374 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1379 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1381 if (receive_requested_reply)
1382 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1384 rule->d.receive.message_type = message_type;
1385 rule->d.receive.path = _dbus_strdup (receive_path);
1386 rule->d.receive.interface = _dbus_strdup (receive_interface);
1387 rule->d.receive.member = _dbus_strdup (receive_member);
1388 rule->d.receive.error = _dbus_strdup (receive_error);
1389 rule->d.receive.origin = _dbus_strdup (receive_sender);
1391 if (receive_path && rule->d.receive.path == NULL)
1393 if (receive_interface && rule->d.receive.interface == NULL)
1395 if (receive_member && rule->d.receive.member == NULL)
1397 if (receive_error && rule->d.receive.error == NULL)
1399 if (receive_sender && rule->d.receive.origin == NULL)
1404 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1408 if (IS_WILDCARD (own))
1411 rule->d.own.service_name = _dbus_strdup (own);
1412 if (own && rule->d.own.service_name == NULL)
1417 if (IS_WILDCARD (user))
1419 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1423 rule->d.user.uid = DBUS_UID_UNSET;
1427 DBusString username;
1430 _dbus_string_init_const (&username, user);
1432 if (_dbus_get_user_id (&username, &uid))
1434 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1438 rule->d.user.uid = uid;
1442 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1443 user, element_name);
1449 if (IS_WILDCARD (group))
1451 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1455 rule->d.group.gid = DBUS_GID_UNSET;
1459 DBusString groupname;
1462 _dbus_string_init_const (&groupname, group);
1464 if (_dbus_get_user_id (&groupname, &gid))
1466 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1470 rule->d.group.gid = gid;
1474 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1475 group, element_name);
1480 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1486 pe = peek_element (parser);
1487 _dbus_assert (pe != NULL);
1488 _dbus_assert (pe->type == ELEMENT_POLICY);
1490 switch (pe->d.policy.type)
1492 case POLICY_IGNORED:
1493 /* drop the rule on the floor */
1496 case POLICY_DEFAULT:
1497 if (!bus_policy_append_default_rule (parser->policy, rule))
1500 case POLICY_MANDATORY:
1501 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1505 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1507 dbus_set_error (error, DBUS_ERROR_FAILED,
1508 "<%s> rule cannot be per-user because it has bus-global semantics",
1513 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1518 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1520 dbus_set_error (error, DBUS_ERROR_FAILED,
1521 "<%s> rule cannot be per-group because it has bus-global semantics",
1526 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1532 case POLICY_CONSOLE:
1533 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1539 bus_policy_rule_unref (rule);
1546 BUS_SET_OOM (error);
1549 bus_policy_rule_unref (rule);
1554 start_policy_child (BusConfigParser *parser,
1555 const char *element_name,
1556 const char **attribute_names,
1557 const char **attribute_values,
1560 if (strcmp (element_name, "allow") == 0)
1562 if (!append_rule_from_element (parser, element_name,
1563 attribute_names, attribute_values,
1567 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1569 BUS_SET_OOM (error);
1575 else if (strcmp (element_name, "deny") == 0)
1577 if (!append_rule_from_element (parser, element_name,
1578 attribute_names, attribute_values,
1582 if (push_element (parser, ELEMENT_DENY) == NULL)
1584 BUS_SET_OOM (error);
1592 dbus_set_error (error, DBUS_ERROR_FAILED,
1593 "Element <%s> not allowed inside <%s> in configuration file",
1594 element_name, "policy");
1600 start_selinux_child (BusConfigParser *parser,
1601 const char *element_name,
1602 const char **attribute_names,
1603 const char **attribute_values,
1610 context_copy = NULL;
1612 if (strcmp (element_name, "associate") == 0)
1615 const char *context;
1617 if (!locate_attributes (parser, "associate",
1622 "context", &context,
1626 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1628 BUS_SET_OOM (error);
1632 if (own == NULL || context == NULL)
1634 dbus_set_error (error, DBUS_ERROR_FAILED,
1635 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1639 own_copy = _dbus_strdup (own);
1640 if (own_copy == NULL)
1642 context_copy = _dbus_strdup (context);
1643 if (context_copy == NULL)
1646 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1647 own_copy, context_copy))
1654 dbus_set_error (error, DBUS_ERROR_FAILED,
1655 "Element <%s> not allowed inside <%s> in configuration file",
1656 element_name, "selinux");
1662 dbus_free (own_copy);
1665 dbus_free (context_copy);
1667 BUS_SET_OOM (error);
1672 bus_config_parser_start_element (BusConfigParser *parser,
1673 const char *element_name,
1674 const char **attribute_names,
1675 const char **attribute_values,
1680 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1682 /* printf ("START: %s\n", element_name); */
1684 t = top_element_type (parser);
1686 if (t == ELEMENT_NONE)
1688 if (strcmp (element_name, "busconfig") == 0)
1690 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1693 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1695 BUS_SET_OOM (error);
1703 dbus_set_error (error, DBUS_ERROR_FAILED,
1704 "Unknown element <%s> at root of configuration file",
1709 else if (t == ELEMENT_BUSCONFIG)
1711 return start_busconfig_child (parser, element_name,
1712 attribute_names, attribute_values,
1715 else if (t == ELEMENT_POLICY)
1717 return start_policy_child (parser, element_name,
1718 attribute_names, attribute_values,
1721 else if (t == ELEMENT_SELINUX)
1723 return start_selinux_child (parser, element_name,
1724 attribute_names, attribute_values,
1729 dbus_set_error (error, DBUS_ERROR_FAILED,
1730 "Element <%s> is not allowed in this context",
1737 set_limit (BusConfigParser *parser,
1742 dbus_bool_t must_be_positive;
1743 dbus_bool_t must_be_int;
1745 must_be_int = FALSE;
1746 must_be_positive = FALSE;
1748 if (strcmp (name, "max_incoming_bytes") == 0)
1750 must_be_positive = TRUE;
1751 parser->limits.max_incoming_bytes = value;
1753 else if (strcmp (name, "max_outgoing_bytes") == 0)
1755 must_be_positive = TRUE;
1756 parser->limits.max_outgoing_bytes = value;
1758 else if (strcmp (name, "max_message_size") == 0)
1760 must_be_positive = TRUE;
1761 parser->limits.max_message_size = value;
1763 else if (strcmp (name, "service_start_timeout") == 0)
1765 must_be_positive = TRUE;
1767 parser->limits.activation_timeout = value;
1769 else if (strcmp (name, "auth_timeout") == 0)
1771 must_be_positive = TRUE;
1773 parser->limits.auth_timeout = value;
1775 else if (strcmp (name, "reply_timeout") == 0)
1777 must_be_positive = TRUE;
1779 parser->limits.reply_timeout = value;
1781 else if (strcmp (name, "max_completed_connections") == 0)
1783 must_be_positive = TRUE;
1785 parser->limits.max_completed_connections = value;
1787 else if (strcmp (name, "max_incomplete_connections") == 0)
1789 must_be_positive = TRUE;
1791 parser->limits.max_incomplete_connections = value;
1793 else if (strcmp (name, "max_connections_per_user") == 0)
1795 must_be_positive = TRUE;
1797 parser->limits.max_connections_per_user = value;
1799 else if (strcmp (name, "max_pending_service_starts") == 0)
1801 must_be_positive = TRUE;
1803 parser->limits.max_pending_activations = value;
1805 else if (strcmp (name, "max_names_per_connection") == 0)
1807 must_be_positive = TRUE;
1809 parser->limits.max_services_per_connection = value;
1811 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1813 must_be_positive = TRUE;
1815 parser->limits.max_match_rules_per_connection = value;
1817 else if (strcmp (name, "max_replies_per_connection") == 0)
1819 must_be_positive = TRUE;
1821 parser->limits.max_replies_per_connection = value;
1825 dbus_set_error (error, DBUS_ERROR_FAILED,
1826 "There is no limit called \"%s\"\n",
1831 if (must_be_positive && value < 0)
1833 dbus_set_error (error, DBUS_ERROR_FAILED,
1834 "<limit name=\"%s\"> must be a positive number\n",
1840 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1842 dbus_set_error (error, DBUS_ERROR_FAILED,
1843 "<limit name=\"%s\"> value is too large\n",
1852 bus_config_parser_end_element (BusConfigParser *parser,
1853 const char *element_name,
1860 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1862 /* printf ("END: %s\n", element_name); */
1864 t = top_element_type (parser);
1866 if (t == ELEMENT_NONE)
1868 /* should probably be an assertion failure but
1869 * being paranoid about XML parsers
1871 dbus_set_error (error, DBUS_ERROR_FAILED,
1872 "XML parser ended element with no element on the stack");
1876 n = element_type_to_name (t);
1877 _dbus_assert (n != NULL);
1878 if (strcmp (n, element_name) != 0)
1880 /* should probably be an assertion failure but
1881 * being paranoid about XML parsers
1883 dbus_set_error (error, DBUS_ERROR_FAILED,
1884 "XML element <%s> ended but topmost element on the stack was <%s>",
1889 e = peek_element (parser);
1890 _dbus_assert (e != NULL);
1895 _dbus_assert_not_reached ("element in stack has no type");
1898 case ELEMENT_INCLUDE:
1901 case ELEMENT_LISTEN:
1902 case ELEMENT_PIDFILE:
1904 case ELEMENT_SERVICEDIR:
1905 case ELEMENT_INCLUDEDIR:
1907 if (!e->had_content)
1909 dbus_set_error (error, DBUS_ERROR_FAILED,
1910 "XML element <%s> was expected to have content inside it",
1911 element_type_to_name (e->type));
1915 if (e->type == ELEMENT_LIMIT)
1917 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1923 case ELEMENT_BUSCONFIG:
1924 case ELEMENT_POLICY:
1928 case ELEMENT_SELINUX:
1929 case ELEMENT_ASSOCIATE:
1933 pop_element (parser);
1939 all_whitespace (const DBusString *str)
1943 _dbus_string_skip_white (str, 0, &i);
1945 return i == _dbus_string_get_length (str);
1949 make_full_path (const DBusString *basedir,
1950 const DBusString *filename,
1951 DBusString *full_path)
1953 if (_dbus_path_is_absolute (filename))
1955 return _dbus_string_copy (filename, 0, full_path, 0);
1959 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1962 if (!_dbus_concat_dir_and_file (full_path, filename))
1970 include_file (BusConfigParser *parser,
1971 const DBusString *filename,
1972 dbus_bool_t ignore_missing,
1975 /* FIXME good test case for this would load each config file in the
1976 * test suite both alone, and as an include, and check
1977 * that the result is the same
1979 BusConfigParser *included;
1980 const char *filename_str;
1981 DBusError tmp_error;
1983 dbus_error_init (&tmp_error);
1985 filename_str = _dbus_string_get_const_data (filename);
1987 /* Check to make sure this file hasn't already been included. */
1988 if (seen_include (parser, filename))
1990 dbus_set_error (error, DBUS_ERROR_FAILED,
1991 "Circular inclusion of file '%s'",
1996 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
1998 BUS_SET_OOM (error);
2002 /* Since parser is passed in as the parent, included
2003 inherits parser's limits. */
2004 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2006 _dbus_list_pop_last (&parser->included_files);
2008 if (included == NULL)
2010 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2012 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2015 dbus_error_free (&tmp_error);
2020 dbus_move_error (&tmp_error, error);
2026 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2028 if (!merge_included (parser, included, error))
2030 bus_config_parser_unref (included);
2034 /* Copy included's limits back to parser. */
2035 parser->limits = included->limits;
2037 bus_config_parser_unref (included);
2043 include_dir (BusConfigParser *parser,
2044 const DBusString *dirname,
2047 DBusString filename;
2049 DBusError tmp_error;
2053 if (!_dbus_string_init (&filename))
2055 BUS_SET_OOM (error);
2061 dir = _dbus_directory_open (dirname, error);
2066 dbus_error_init (&tmp_error);
2067 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2069 DBusString full_path;
2071 if (!_dbus_string_init (&full_path))
2073 BUS_SET_OOM (error);
2077 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2079 BUS_SET_OOM (error);
2080 _dbus_string_free (&full_path);
2084 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2086 BUS_SET_OOM (error);
2087 _dbus_string_free (&full_path);
2091 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2093 if (!include_file (parser, &full_path, TRUE, error))
2095 _dbus_string_free (&full_path);
2100 _dbus_string_free (&full_path);
2103 if (dbus_error_is_set (&tmp_error))
2105 dbus_move_error (&tmp_error, error);
2110 if (!_dbus_string_copy_data (dirname, &s))
2112 BUS_SET_OOM (error);
2116 if (!_dbus_list_append (&parser->conf_dirs, s))
2119 BUS_SET_OOM (error);
2126 _dbus_string_free (&filename);
2129 _dbus_directory_close (dir);
2135 bus_config_parser_content (BusConfigParser *parser,
2136 const DBusString *content,
2141 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2147 _dbus_string_get_const_data (content, &c_str);
2149 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2153 e = peek_element (parser);
2156 dbus_set_error (error, DBUS_ERROR_FAILED,
2157 "Text content outside of any XML element in configuration file");
2160 else if (e->had_content)
2162 _dbus_assert_not_reached ("Element had multiple content blocks");
2166 switch (top_element_type (parser))
2169 _dbus_assert_not_reached ("element at top of stack has no type");
2172 case ELEMENT_BUSCONFIG:
2173 case ELEMENT_POLICY:
2177 case ELEMENT_SELINUX:
2178 case ELEMENT_ASSOCIATE:
2179 if (all_whitespace (content))
2183 dbus_set_error (error, DBUS_ERROR_FAILED,
2184 "No text content expected inside XML element %s in configuration file",
2185 element_type_to_name (top_element_type (parser)));
2189 case ELEMENT_PIDFILE:
2193 e->had_content = TRUE;
2195 if (!_dbus_string_copy_data (content, &s))
2198 dbus_free (parser->pidfile);
2199 parser->pidfile = s;
2203 case ELEMENT_INCLUDE:
2205 DBusString full_path, selinux_policy_root;
2207 e->had_content = TRUE;
2209 if (e->d.include.if_selinux_enabled
2210 && !bus_selinux_enabled ())
2213 if (!_dbus_string_init (&full_path))
2216 if (e->d.include.selinux_root_relative)
2218 if (!bus_selinux_get_policy_root ())
2220 dbus_set_error (error, DBUS_ERROR_FAILED,
2221 "Could not determine SELinux policy root for relative inclusion");
2222 _dbus_string_free (&full_path);
2225 _dbus_string_init_const (&selinux_policy_root,
2226 bus_selinux_get_policy_root ());
2227 if (!make_full_path (&selinux_policy_root, content, &full_path))
2229 _dbus_string_free (&full_path);
2233 else if (!make_full_path (&parser->basedir, content, &full_path))
2235 _dbus_string_free (&full_path);
2239 if (!include_file (parser, &full_path,
2240 e->d.include.ignore_missing, error))
2242 _dbus_string_free (&full_path);
2246 _dbus_string_free (&full_path);
2250 case ELEMENT_INCLUDEDIR:
2252 DBusString full_path;
2254 e->had_content = TRUE;
2256 if (!_dbus_string_init (&full_path))
2259 if (!make_full_path (&parser->basedir, content, &full_path))
2261 _dbus_string_free (&full_path);
2265 if (!include_dir (parser, &full_path, error))
2267 _dbus_string_free (&full_path);
2271 _dbus_string_free (&full_path);
2279 e->had_content = TRUE;
2281 if (!_dbus_string_copy_data (content, &s))
2284 dbus_free (parser->user);
2293 e->had_content = TRUE;
2295 if (!_dbus_string_copy_data (content, &s))
2298 dbus_free (parser->bus_type);
2299 parser->bus_type = s;
2303 case ELEMENT_LISTEN:
2307 e->had_content = TRUE;
2309 if (!_dbus_string_copy_data (content, &s))
2312 if (!_dbus_list_append (&parser->listen_on,
2325 e->had_content = TRUE;
2327 if (!_dbus_string_copy_data (content, &s))
2330 if (!_dbus_list_append (&parser->mechanisms,
2339 case ELEMENT_SERVICEDIR:
2342 DBusString full_path;
2344 e->had_content = TRUE;
2346 if (!_dbus_string_init (&full_path))
2349 if (!make_full_path (&parser->basedir, content, &full_path))
2351 _dbus_string_free (&full_path);
2355 if (!_dbus_string_copy_data (&full_path, &s))
2357 _dbus_string_free (&full_path);
2361 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2363 _dbus_string_free (&full_path);
2368 _dbus_string_free (&full_path);
2376 e->had_content = TRUE;
2379 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2381 dbus_set_error (error, DBUS_ERROR_FAILED,
2382 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2387 e->d.limit.value = val;
2389 _dbus_verbose ("Loaded value %ld for limit %s\n",
2396 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2400 BUS_SET_OOM (error);
2405 bus_config_parser_finished (BusConfigParser *parser,
2408 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2410 if (parser->stack != NULL)
2412 dbus_set_error (error, DBUS_ERROR_FAILED,
2413 "Element <%s> was not closed in configuration file",
2414 element_type_to_name (top_element_type (parser)));
2419 if (parser->is_toplevel && parser->listen_on == NULL)
2421 dbus_set_error (error, DBUS_ERROR_FAILED,
2422 "Configuration file needs one or more <listen> elements giving addresses");
2430 bus_config_parser_get_user (BusConfigParser *parser)
2432 return parser->user;
2436 bus_config_parser_get_type (BusConfigParser *parser)
2438 return parser->bus_type;
2442 bus_config_parser_get_addresses (BusConfigParser *parser)
2444 return &parser->listen_on;
2448 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2450 return &parser->mechanisms;
2454 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2456 return &parser->service_dirs;
2460 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2462 return &parser->conf_dirs;
2466 bus_config_parser_get_fork (BusConfigParser *parser)
2468 return parser->fork;
2472 bus_config_parser_get_pidfile (BusConfigParser *parser)
2474 return parser->pidfile;
2478 bus_config_parser_steal_policy (BusConfigParser *parser)
2482 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2484 policy = parser->policy;
2486 parser->policy = NULL;
2491 /* Overwrite any limits that were set in the configuration file */
2493 bus_config_parser_get_limits (BusConfigParser *parser,
2496 *limits = parser->limits;
2500 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2502 DBusHashTable *table;
2504 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2506 table = parser->service_context_table;
2508 parser->service_context_table = NULL;
2513 #ifdef DBUS_BUILD_TESTS
2524 do_load (const DBusString *full_path,
2526 dbus_bool_t oom_possible)
2528 BusConfigParser *parser;
2531 dbus_error_init (&error);
2533 parser = bus_config_load (full_path, TRUE, NULL, &error);
2536 _DBUS_ASSERT_ERROR_IS_SET (&error);
2539 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2541 _dbus_verbose ("Failed to load valid file due to OOM\n");
2542 dbus_error_free (&error);
2545 else if (validity == VALID)
2547 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2550 dbus_error_free (&error);
2555 dbus_error_free (&error);
2561 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2563 bus_config_parser_unref (parser);
2565 if (validity == INVALID)
2567 _dbus_warn ("Accepted invalid file\n");
2577 const DBusString *full_path;
2582 check_loader_oom_func (void *data)
2584 LoaderOomData *d = data;
2586 return do_load (d->full_path, d->validity, TRUE);
2590 process_test_valid_subdir (const DBusString *test_base_dir,
2594 DBusString test_directory;
2595 DBusString filename;
2603 if (!_dbus_string_init (&test_directory))
2604 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2606 _dbus_string_init_const (&filename, subdir);
2608 if (!_dbus_string_copy (test_base_dir, 0,
2609 &test_directory, 0))
2610 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2612 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2613 _dbus_assert_not_reached ("couldn't allocate full path");
2615 _dbus_string_free (&filename);
2616 if (!_dbus_string_init (&filename))
2617 _dbus_assert_not_reached ("didn't allocate filename string\n");
2619 dbus_error_init (&error);
2620 dir = _dbus_directory_open (&test_directory, &error);
2623 _dbus_warn ("Could not open %s: %s\n",
2624 _dbus_string_get_const_data (&test_directory),
2626 dbus_error_free (&error);
2630 if (validity == VALID)
2631 printf ("Testing valid files:\n");
2632 else if (validity == INVALID)
2633 printf ("Testing invalid files:\n");
2635 printf ("Testing unknown files:\n");
2638 while (_dbus_directory_get_next_file (dir, &filename, &error))
2640 DBusString full_path;
2643 if (!_dbus_string_init (&full_path))
2644 _dbus_assert_not_reached ("couldn't init string");
2646 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2647 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2649 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2650 _dbus_assert_not_reached ("couldn't concat file to dir");
2652 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2654 _dbus_verbose ("Skipping non-.conf file %s\n",
2655 _dbus_string_get_const_data (&filename));
2656 _dbus_string_free (&full_path);
2660 printf (" %s\n", _dbus_string_get_const_data (&filename));
2662 _dbus_verbose (" expecting %s\n",
2663 validity == VALID ? "valid" :
2664 (validity == INVALID ? "invalid" :
2665 (validity == UNKNOWN ? "unknown" : "???")));
2667 d.full_path = &full_path;
2668 d.validity = validity;
2670 /* FIXME hackaround for an expat problem, see
2671 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2672 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2674 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2675 if (!check_loader_oom_func (&d))
2676 _dbus_assert_not_reached ("test failed");
2678 _dbus_string_free (&full_path);
2681 if (dbus_error_is_set (&error))
2683 _dbus_warn ("Could not get next file in %s: %s\n",
2684 _dbus_string_get_const_data (&test_directory),
2686 dbus_error_free (&error);
2695 _dbus_directory_close (dir);
2696 _dbus_string_free (&test_directory);
2697 _dbus_string_free (&filename);
2703 bools_equal (dbus_bool_t a,
2710 strings_equal_or_both_null (const char *a,
2713 if (a == NULL || b == NULL)
2716 return !strcmp (a, b);
2720 elements_equal (const Element *a,
2723 if (a->type != b->type)
2726 if (!bools_equal (a->had_content, b->had_content))
2732 case ELEMENT_INCLUDE:
2733 if (!bools_equal (a->d.include.ignore_missing,
2734 b->d.include.ignore_missing))
2738 case ELEMENT_POLICY:
2739 if (a->d.policy.type != b->d.policy.type)
2741 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
2746 if (strcmp (a->d.limit.name, b->d.limit.name))
2748 if (a->d.limit.value != b->d.limit.value)
2762 lists_of_elements_equal (DBusList *a,
2771 while (ia != NULL && ib != NULL)
2773 if (elements_equal (ia->data, ib->data))
2775 ia = _dbus_list_get_next_link (&a, ia);
2776 ib = _dbus_list_get_next_link (&b, ib);
2779 return ia == NULL && ib == NULL;
2783 lists_of_c_strings_equal (DBusList *a,
2792 while (ia != NULL && ib != NULL)
2794 if (strcmp (ia->data, ib->data))
2796 ia = _dbus_list_get_next_link (&a, ia);
2797 ib = _dbus_list_get_next_link (&b, ib);
2800 return ia == NULL && ib == NULL;
2804 limits_equal (const BusLimits *a,
2808 (a->max_incoming_bytes == b->max_incoming_bytes
2809 || a->max_outgoing_bytes == b->max_outgoing_bytes
2810 || a->max_message_size == b->max_message_size
2811 || a->activation_timeout == b->activation_timeout
2812 || a->auth_timeout == b->auth_timeout
2813 || a->max_completed_connections == b->max_completed_connections
2814 || a->max_incomplete_connections == b->max_incomplete_connections
2815 || a->max_connections_per_user == b->max_connections_per_user
2816 || a->max_pending_activations == b->max_pending_activations
2817 || a->max_services_per_connection == b->max_services_per_connection
2818 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2819 || a->max_replies_per_connection == b->max_replies_per_connection
2820 || a->reply_timeout == b->reply_timeout);
2824 config_parsers_equal (const BusConfigParser *a,
2825 const BusConfigParser *b)
2827 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2830 if (!lists_of_elements_equal (a->stack, b->stack))
2833 if (!strings_equal_or_both_null (a->user, b->user))
2836 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2839 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2842 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2845 /* FIXME: compare policy */
2847 /* FIXME: compare service selinux ID table */
2849 if (! limits_equal (&a->limits, &b->limits))
2852 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2855 if (! bools_equal (a->fork, b->fork))
2858 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2865 all_are_equiv (const DBusString *target_directory)
2867 DBusString filename;
2869 BusConfigParser *first_parser;
2870 BusConfigParser *parser;
2876 first_parser = NULL;
2880 if (!_dbus_string_init (&filename))
2881 _dbus_assert_not_reached ("didn't allocate filename string");
2883 dbus_error_init (&error);
2884 dir = _dbus_directory_open (target_directory, &error);
2887 _dbus_warn ("Could not open %s: %s\n",
2888 _dbus_string_get_const_data (target_directory),
2890 dbus_error_free (&error);
2894 printf ("Comparing equivalent files:\n");
2897 while (_dbus_directory_get_next_file (dir, &filename, &error))
2899 DBusString full_path;
2901 if (!_dbus_string_init (&full_path))
2902 _dbus_assert_not_reached ("couldn't init string");
2904 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2905 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2907 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2908 _dbus_assert_not_reached ("couldn't concat file to dir");
2910 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2912 _dbus_verbose ("Skipping non-.conf file %s\n",
2913 _dbus_string_get_const_data (&filename));
2914 _dbus_string_free (&full_path);
2918 printf (" %s\n", _dbus_string_get_const_data (&filename));
2920 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2924 _dbus_warn ("Could not load file %s: %s\n",
2925 _dbus_string_get_const_data (&full_path),
2927 _dbus_string_free (&full_path);
2928 dbus_error_free (&error);
2931 else if (first_parser == NULL)
2933 _dbus_string_free (&full_path);
2934 first_parser = parser;
2938 _dbus_string_free (&full_path);
2939 equal = config_parsers_equal (first_parser, parser);
2940 bus_config_parser_unref (parser);
2949 _dbus_string_free (&filename);
2951 bus_config_parser_unref (first_parser);
2953 _dbus_directory_close (dir);
2960 process_test_equiv_subdir (const DBusString *test_base_dir,
2963 DBusString test_directory;
2964 DBusString filename;
2973 if (!_dbus_string_init (&test_directory))
2974 _dbus_assert_not_reached ("didn't allocate test_directory");
2976 _dbus_string_init_const (&filename, subdir);
2978 if (!_dbus_string_copy (test_base_dir, 0,
2979 &test_directory, 0))
2980 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2982 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2983 _dbus_assert_not_reached ("couldn't allocate full path");
2985 _dbus_string_free (&filename);
2986 if (!_dbus_string_init (&filename))
2987 _dbus_assert_not_reached ("didn't allocate filename string");
2989 dbus_error_init (&error);
2990 dir = _dbus_directory_open (&test_directory, &error);
2993 _dbus_warn ("Could not open %s: %s\n",
2994 _dbus_string_get_const_data (&test_directory),
2996 dbus_error_free (&error);
3000 while (_dbus_directory_get_next_file (dir, &filename, &error))
3002 DBusString full_path;
3004 /* Skip CVS's magic directories! */
3005 if (_dbus_string_equal_c_str (&filename, "CVS"))
3008 if (!_dbus_string_init (&full_path))
3009 _dbus_assert_not_reached ("couldn't init string");
3011 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3012 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3014 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3015 _dbus_assert_not_reached ("couldn't concat file to dir");
3017 equal = all_are_equiv (&full_path);
3018 _dbus_string_free (&full_path);
3027 _dbus_string_free (&test_directory);
3028 _dbus_string_free (&filename);
3030 _dbus_directory_close (dir);
3037 bus_config_parser_test (const DBusString *test_data_dir)
3039 if (test_data_dir == NULL ||
3040 _dbus_string_get_length (test_data_dir) == 0)
3042 printf ("No test data\n");
3046 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3049 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3052 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3058 #endif /* DBUS_BUILD_TESTS */