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>
51 ELEMENT_STANDARD_SESSION_SERVICEDIRS
56 /* we ignore policies for unknown groups/users */
71 unsigned int had_content : 1;
77 unsigned int ignore_missing : 1;
78 unsigned int if_selinux_enabled : 1;
79 unsigned int selinux_root_relative : 1;
85 unsigned long gid_uid_or_at_console;
99 * Parser for bus configuration file.
101 struct BusConfigParser
103 int refcount; /**< Reference count */
105 DBusString basedir; /**< Directory we resolve paths relative to */
107 DBusList *stack; /**< stack of Element */
109 char *user; /**< user to run as */
111 char *bus_type; /**< Message bus type */
113 DBusList *listen_on; /**< List of addresses to listen to */
115 DBusList *mechanisms; /**< Auth mechanisms */
117 DBusList *service_dirs; /**< Directories to look for services in */
119 DBusList *conf_dirs; /**< Directories to look for policy configuration in */
121 BusPolicy *policy; /**< Security policy */
123 BusLimits limits; /**< Limits */
125 char *pidfile; /**< PID file */
127 DBusList *included_files; /**< Included files stack */
129 DBusHashTable *service_context_table; /**< Map service names to SELinux contexts */
131 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
133 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
137 element_type_to_name (ElementType type)
143 case ELEMENT_BUSCONFIG:
145 case ELEMENT_INCLUDE:
163 case ELEMENT_PIDFILE:
165 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
166 return "standard_session_servicedirs";
167 case ELEMENT_SERVICEDIR:
169 case ELEMENT_INCLUDEDIR:
173 case ELEMENT_SELINUX:
175 case ELEMENT_ASSOCIATE:
179 _dbus_assert_not_reached ("bad element type");
185 push_element (BusConfigParser *parser,
190 _dbus_assert (type != ELEMENT_NONE);
192 e = dbus_new0 (Element, 1);
196 if (!_dbus_list_append (&parser->stack, e))
208 element_free (Element *e)
210 if (e->type == ELEMENT_LIMIT)
211 dbus_free (e->d.limit.name);
217 pop_element (BusConfigParser *parser)
221 e = _dbus_list_pop_last (&parser->stack);
227 peek_element (BusConfigParser *parser)
231 e = _dbus_list_get_last (&parser->stack);
237 top_element_type (BusConfigParser *parser)
241 e = _dbus_list_get_last (&parser->stack);
250 merge_service_context_hash (DBusHashTable *dest,
260 _dbus_hash_iter_init (from, &iter);
261 while (_dbus_hash_iter_next (&iter))
263 const char *service = _dbus_hash_iter_get_string_key (&iter);
264 const char *context = _dbus_hash_iter_get_value (&iter);
266 service_copy = _dbus_strdup (service);
267 if (service_copy == NULL)
269 context_copy = _dbus_strdup (context);
270 if (context_copy == NULL)
273 if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
284 dbus_free (service_copy);
287 dbus_free (context_copy);
293 service_dirs_find_dir (DBusList **service_dirs,
298 _dbus_assert (dir != NULL);
300 for (link = *service_dirs; link; link = _dbus_list_get_next_link(service_dirs, link))
302 const char *link_dir;
304 link_dir = (const char *)link->data;
305 if (strcmp (dir, link_dir) == 0)
313 service_dirs_append_unique_or_free (DBusList **service_dirs,
316 if (!service_dirs_find_dir (service_dirs, dir))
317 return _dbus_list_append (service_dirs, dir);
324 service_dirs_append_link_unique_or_free (DBusList **service_dirs,
327 if (!service_dirs_find_dir (service_dirs, dir_link->data))
329 _dbus_list_append_link (service_dirs, dir_link);
333 dbus_free (dir_link->data);
334 _dbus_list_free_link (dir_link);
339 merge_included (BusConfigParser *parser,
340 BusConfigParser *included,
345 if (!bus_policy_merge (parser->policy,
352 if (!merge_service_context_hash (parser->service_context_table,
353 included->service_context_table))
359 if (included->user != NULL)
361 dbus_free (parser->user);
362 parser->user = included->user;
363 included->user = NULL;
366 if (included->bus_type != NULL)
368 dbus_free (parser->bus_type);
369 parser->bus_type = included->bus_type;
370 included->bus_type = NULL;
376 if (included->pidfile != NULL)
378 dbus_free (parser->pidfile);
379 parser->pidfile = included->pidfile;
380 included->pidfile = NULL;
383 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
384 _dbus_list_append_link (&parser->listen_on, link);
386 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
387 _dbus_list_append_link (&parser->mechanisms, link);
389 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
390 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
392 while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
393 _dbus_list_append_link (&parser->conf_dirs, link);
399 seen_include (BusConfigParser *parser,
400 const DBusString *file)
404 iter = parser->included_files;
407 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
410 iter = _dbus_list_get_next_link (&parser->included_files, iter);
417 bus_config_parser_new (const DBusString *basedir,
418 dbus_bool_t is_toplevel,
419 const BusConfigParser *parent)
421 BusConfigParser *parser;
423 parser = dbus_new0 (BusConfigParser, 1);
427 parser->is_toplevel = !!is_toplevel;
429 if (!_dbus_string_init (&parser->basedir))
435 if (((parser->policy = bus_policy_new ()) == NULL) ||
436 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
437 ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
439 dbus_free)) == NULL))
442 bus_policy_unref (parser->policy);
444 _dbus_string_free (&parser->basedir);
452 /* Initialize the parser's limits from the parent. */
453 parser->limits = parent->limits;
455 /* Use the parent's list of included_files to avoid
456 circular inclusions. */
457 parser->included_files = parent->included_files;
462 /* Make up some numbers! woot! */
463 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
464 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
465 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
467 /* Making this long means the user has to wait longer for an error
468 * message if something screws up, but making it too short means
469 * they might see a false failure.
471 parser->limits.activation_timeout = 25000; /* 25 seconds */
473 /* Making this long risks making a DOS attack easier, but too short
474 * and legitimate auth will fail. If interactive auth (ask user for
475 * password) is allowed, then potentially it has to be quite long.
477 parser->limits.auth_timeout = 30000; /* 30 seconds */
479 parser->limits.max_incomplete_connections = 32;
480 parser->limits.max_connections_per_user = 128;
482 /* Note that max_completed_connections / max_connections_per_user
483 * is the number of users that would have to work together to
484 * DOS all the other users.
486 parser->limits.max_completed_connections = 1024;
488 parser->limits.max_pending_activations = 256;
489 parser->limits.max_services_per_connection = 256;
491 parser->limits.max_match_rules_per_connection = 512;
493 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
494 parser->limits.max_replies_per_connection = 32;
497 parser->refcount = 1;
503 bus_config_parser_ref (BusConfigParser *parser)
505 _dbus_assert (parser->refcount > 0);
507 parser->refcount += 1;
513 bus_config_parser_unref (BusConfigParser *parser)
515 _dbus_assert (parser->refcount > 0);
517 parser->refcount -= 1;
519 if (parser->refcount == 0)
521 while (parser->stack != NULL)
522 pop_element (parser);
524 dbus_free (parser->user);
525 dbus_free (parser->bus_type);
526 dbus_free (parser->pidfile);
528 _dbus_list_foreach (&parser->listen_on,
529 (DBusForeachFunction) dbus_free,
532 _dbus_list_clear (&parser->listen_on);
534 _dbus_list_foreach (&parser->service_dirs,
535 (DBusForeachFunction) dbus_free,
538 _dbus_list_clear (&parser->service_dirs);
540 _dbus_list_foreach (&parser->conf_dirs,
541 (DBusForeachFunction) dbus_free,
544 _dbus_list_clear (&parser->conf_dirs);
546 _dbus_list_foreach (&parser->mechanisms,
547 (DBusForeachFunction) dbus_free,
550 _dbus_list_clear (&parser->mechanisms);
552 _dbus_string_free (&parser->basedir);
555 bus_policy_unref (parser->policy);
557 if (parser->service_context_table)
558 _dbus_hash_table_unref (parser->service_context_table);
565 bus_config_parser_check_doctype (BusConfigParser *parser,
569 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
571 if (strcmp (doctype, "busconfig") != 0)
573 dbus_set_error (error,
575 "Configuration file has the wrong document type %s",
590 locate_attributes (BusConfigParser *parser,
591 const char *element_name,
592 const char **attribute_names,
593 const char **attribute_values,
595 const char *first_attribute_name,
596 const char **first_attribute_retloc,
604 LocateAttr attrs[MAX_ATTRS];
608 _dbus_assert (first_attribute_name != NULL);
609 _dbus_assert (first_attribute_retloc != NULL);
614 attrs[0].name = first_attribute_name;
615 attrs[0].retloc = first_attribute_retloc;
616 *first_attribute_retloc = NULL;
618 va_start (args, first_attribute_retloc);
620 name = va_arg (args, const char*);
621 retloc = va_arg (args, const char**);
625 _dbus_assert (retloc != NULL);
626 _dbus_assert (n_attrs < MAX_ATTRS);
628 attrs[n_attrs].name = name;
629 attrs[n_attrs].retloc = retloc;
633 name = va_arg (args, const char*);
634 retloc = va_arg (args, const char**);
643 while (attribute_names[i])
652 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
654 retloc = attrs[j].retloc;
658 dbus_set_error (error, DBUS_ERROR_FAILED,
659 "Attribute \"%s\" repeated twice on the same <%s> element",
660 attrs[j].name, element_name);
665 *retloc = attribute_values[i];
674 dbus_set_error (error, DBUS_ERROR_FAILED,
675 "Attribute \"%s\" is invalid on <%s> element in this context",
676 attribute_names[i], element_name);
689 check_no_attributes (BusConfigParser *parser,
690 const char *element_name,
691 const char **attribute_names,
692 const char **attribute_values,
695 if (attribute_names[0] != NULL)
697 dbus_set_error (error, DBUS_ERROR_FAILED,
698 "Attribute \"%s\" is invalid on <%s> element in this context",
699 attribute_names[0], element_name);
707 start_busconfig_child (BusConfigParser *parser,
708 const char *element_name,
709 const char **attribute_names,
710 const char **attribute_values,
713 if (strcmp (element_name, "user") == 0)
715 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
718 if (push_element (parser, ELEMENT_USER) == NULL)
726 else if (strcmp (element_name, "type") == 0)
728 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
731 if (push_element (parser, ELEMENT_TYPE) == NULL)
739 else if (strcmp (element_name, "fork") == 0)
741 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
744 if (push_element (parser, ELEMENT_FORK) == NULL)
754 else if (strcmp (element_name, "pidfile") == 0)
756 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
759 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
767 else if (strcmp (element_name, "listen") == 0)
769 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
772 if (push_element (parser, ELEMENT_LISTEN) == NULL)
780 else if (strcmp (element_name, "auth") == 0)
782 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
785 if (push_element (parser, ELEMENT_AUTH) == NULL)
793 else if (strcmp (element_name, "includedir") == 0)
795 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
798 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
806 else if (strcmp (element_name, "standard_session_servicedirs") == 0)
812 if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
815 if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
821 if (!_dbus_get_standard_session_servicedirs (&dirs))
827 while ((link = _dbus_list_pop_first_link (&dirs)))
828 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
832 else if (strcmp (element_name, "servicedir") == 0)
834 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
837 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
845 else if (strcmp (element_name, "include") == 0)
848 const char *if_selinux_enabled;
849 const char *ignore_missing;
850 const char *selinux_root_relative;
852 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
858 e->d.include.ignore_missing = FALSE;
859 e->d.include.if_selinux_enabled = FALSE;
860 e->d.include.selinux_root_relative = FALSE;
862 if (!locate_attributes (parser, "include",
866 "ignore_missing", &ignore_missing,
867 "if_selinux_enabled", &if_selinux_enabled,
868 "selinux_root_relative", &selinux_root_relative,
872 if (ignore_missing != NULL)
874 if (strcmp (ignore_missing, "yes") == 0)
875 e->d.include.ignore_missing = TRUE;
876 else if (strcmp (ignore_missing, "no") == 0)
877 e->d.include.ignore_missing = FALSE;
880 dbus_set_error (error, DBUS_ERROR_FAILED,
881 "ignore_missing attribute must have value \"yes\" or \"no\"");
886 if (if_selinux_enabled != NULL)
888 if (strcmp (if_selinux_enabled, "yes") == 0)
889 e->d.include.if_selinux_enabled = TRUE;
890 else if (strcmp (if_selinux_enabled, "no") == 0)
891 e->d.include.if_selinux_enabled = FALSE;
894 dbus_set_error (error, DBUS_ERROR_FAILED,
895 "if_selinux_enabled attribute must have value"
896 " \"yes\" or \"no\"");
901 if (selinux_root_relative != NULL)
903 if (strcmp (selinux_root_relative, "yes") == 0)
904 e->d.include.selinux_root_relative = TRUE;
905 else if (strcmp (selinux_root_relative, "no") == 0)
906 e->d.include.selinux_root_relative = FALSE;
909 dbus_set_error (error, DBUS_ERROR_FAILED,
910 "selinux_root_relative attribute must have value"
911 " \"yes\" or \"no\"");
918 else if (strcmp (element_name, "policy") == 0)
924 const char *at_console;
926 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
932 e->d.policy.type = POLICY_IGNORED;
934 if (!locate_attributes (parser, "policy",
941 "at_console", &at_console,
945 if (((context && user) ||
946 (context && group) ||
947 (context && at_console)) ||
949 (user && at_console)) ||
950 (group && at_console) ||
951 !(context || user || group || at_console))
953 dbus_set_error (error, DBUS_ERROR_FAILED,
954 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
960 if (strcmp (context, "default") == 0)
962 e->d.policy.type = POLICY_DEFAULT;
964 else if (strcmp (context, "mandatory") == 0)
966 e->d.policy.type = POLICY_MANDATORY;
970 dbus_set_error (error, DBUS_ERROR_FAILED,
971 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
976 else if (user != NULL)
979 _dbus_string_init_const (&username, user);
981 if (_dbus_get_user_id (&username,
982 &e->d.policy.gid_uid_or_at_console))
983 e->d.policy.type = POLICY_USER;
985 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
988 else if (group != NULL)
990 DBusString group_name;
991 _dbus_string_init_const (&group_name, group);
993 if (_dbus_get_group_id (&group_name,
994 &e->d.policy.gid_uid_or_at_console))
995 e->d.policy.type = POLICY_GROUP;
997 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
1000 else if (at_console != NULL)
1003 t = (strcmp (at_console, "true") == 0);
1004 if (t || strcmp (at_console, "false") == 0)
1006 e->d.policy.gid_uid_or_at_console = t;
1007 e->d.policy.type = POLICY_CONSOLE;
1011 dbus_set_error (error, DBUS_ERROR_FAILED,
1012 "Unknown value \"%s\" for at_console in message bus configuration file",
1020 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
1025 else if (strcmp (element_name, "limit") == 0)
1030 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1032 BUS_SET_OOM (error);
1036 if (!locate_attributes (parser, "limit",
1046 dbus_set_error (error, DBUS_ERROR_FAILED,
1047 "<limit> element must have a \"name\" attribute");
1051 e->d.limit.name = _dbus_strdup (name);
1052 if (e->d.limit.name == NULL)
1054 BUS_SET_OOM (error);
1060 else if (strcmp (element_name, "selinux") == 0)
1062 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1065 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1067 BUS_SET_OOM (error);
1075 dbus_set_error (error, DBUS_ERROR_FAILED,
1076 "Element <%s> not allowed inside <%s> in configuration file",
1077 element_name, "busconfig");
1083 append_rule_from_element (BusConfigParser *parser,
1084 const char *element_name,
1085 const char **attribute_names,
1086 const char **attribute_values,
1090 const char *send_interface;
1091 const char *send_member;
1092 const char *send_error;
1093 const char *send_destination;
1094 const char *send_path;
1095 const char *send_type;
1096 const char *receive_interface;
1097 const char *receive_member;
1098 const char *receive_error;
1099 const char *receive_sender;
1100 const char *receive_path;
1101 const char *receive_type;
1102 const char *eavesdrop;
1103 const char *send_requested_reply;
1104 const char *receive_requested_reply;
1109 BusPolicyRule *rule;
1111 if (!locate_attributes (parser, element_name,
1115 "send_interface", &send_interface,
1116 "send_member", &send_member,
1117 "send_error", &send_error,
1118 "send_destination", &send_destination,
1119 "send_path", &send_path,
1120 "send_type", &send_type,
1121 "receive_interface", &receive_interface,
1122 "receive_member", &receive_member,
1123 "receive_error", &receive_error,
1124 "receive_sender", &receive_sender,
1125 "receive_path", &receive_path,
1126 "receive_type", &receive_type,
1127 "eavesdrop", &eavesdrop,
1128 "send_requested_reply", &send_requested_reply,
1129 "receive_requested_reply", &receive_requested_reply,
1136 if (!(send_interface || send_member || send_error || send_destination ||
1137 send_type || send_path ||
1138 receive_interface || receive_member || receive_error || receive_sender ||
1139 receive_type || receive_path || eavesdrop ||
1140 send_requested_reply || receive_requested_reply ||
1141 own || user || group))
1143 dbus_set_error (error, DBUS_ERROR_FAILED,
1144 "Element <%s> must have one or more attributes",
1149 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1150 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1152 dbus_set_error (error, DBUS_ERROR_FAILED,
1153 "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.",
1158 /* Allowed combinations of elements are:
1160 * base, must be all send or all receive:
1163 * interface + member
1166 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1167 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1169 * user, group, own must occur alone
1171 * Pretty sure the below stuff is broken, FIXME think about it more.
1174 if (((send_interface && send_error) ||
1175 (send_interface && receive_interface) ||
1176 (send_interface && receive_member) ||
1177 (send_interface && receive_error) ||
1178 (send_interface && receive_sender) ||
1179 (send_interface && eavesdrop) ||
1180 (send_interface && receive_requested_reply) ||
1181 (send_interface && own) ||
1182 (send_interface && user) ||
1183 (send_interface && group)) ||
1185 ((send_member && send_error) ||
1186 (send_member && receive_interface) ||
1187 (send_member && receive_member) ||
1188 (send_member && receive_error) ||
1189 (send_member && receive_sender) ||
1190 (send_member && eavesdrop) ||
1191 (send_member && receive_requested_reply) ||
1192 (send_member && own) ||
1193 (send_member && user) ||
1194 (send_member && group)) ||
1196 ((send_error && receive_interface) ||
1197 (send_error && receive_member) ||
1198 (send_error && receive_error) ||
1199 (send_error && receive_sender) ||
1200 (send_error && eavesdrop) ||
1201 (send_error && receive_requested_reply) ||
1202 (send_error && own) ||
1203 (send_error && user) ||
1204 (send_error && group)) ||
1206 ((send_destination && receive_interface) ||
1207 (send_destination && receive_member) ||
1208 (send_destination && receive_error) ||
1209 (send_destination && receive_sender) ||
1210 (send_destination && eavesdrop) ||
1211 (send_destination && receive_requested_reply) ||
1212 (send_destination && own) ||
1213 (send_destination && user) ||
1214 (send_destination && group)) ||
1216 ((send_type && receive_interface) ||
1217 (send_type && receive_member) ||
1218 (send_type && receive_error) ||
1219 (send_type && receive_sender) ||
1220 (send_type && eavesdrop) ||
1221 (send_type && receive_requested_reply) ||
1222 (send_type && own) ||
1223 (send_type && user) ||
1224 (send_type && group)) ||
1226 ((send_path && receive_interface) ||
1227 (send_path && receive_member) ||
1228 (send_path && receive_error) ||
1229 (send_path && receive_sender) ||
1230 (send_path && eavesdrop) ||
1231 (send_path && receive_requested_reply) ||
1232 (send_path && own) ||
1233 (send_path && user) ||
1234 (send_path && group)) ||
1236 ((send_requested_reply && receive_interface) ||
1237 (send_requested_reply && receive_member) ||
1238 (send_requested_reply && receive_error) ||
1239 (send_requested_reply && receive_sender) ||
1240 (send_requested_reply && eavesdrop) ||
1241 (send_requested_reply && receive_requested_reply) ||
1242 (send_requested_reply && own) ||
1243 (send_requested_reply && user) ||
1244 (send_requested_reply && group)) ||
1246 ((receive_interface && receive_error) ||
1247 (receive_interface && own) ||
1248 (receive_interface && user) ||
1249 (receive_interface && group)) ||
1251 ((receive_member && receive_error) ||
1252 (receive_member && own) ||
1253 (receive_member && user) ||
1254 (receive_member && group)) ||
1256 ((receive_error && own) ||
1257 (receive_error && user) ||
1258 (receive_error && group)) ||
1260 ((eavesdrop && own) ||
1261 (eavesdrop && user) ||
1262 (eavesdrop && group)) ||
1264 ((receive_requested_reply && own) ||
1265 (receive_requested_reply && user) ||
1266 (receive_requested_reply && group)) ||
1273 dbus_set_error (error, DBUS_ERROR_FAILED,
1274 "Invalid combination of attributes on element <%s>",
1281 /* In BusPolicyRule, NULL represents wildcard.
1282 * In the config file, '*' represents it.
1284 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1286 if (send_interface || send_member || send_error || send_destination ||
1287 send_path || send_type || send_requested_reply)
1291 if (IS_WILDCARD (send_interface))
1292 send_interface = NULL;
1293 if (IS_WILDCARD (send_member))
1295 if (IS_WILDCARD (send_error))
1297 if (IS_WILDCARD (send_destination))
1298 send_destination = NULL;
1299 if (IS_WILDCARD (send_path))
1301 if (IS_WILDCARD (send_type))
1304 message_type = DBUS_MESSAGE_TYPE_INVALID;
1305 if (send_type != NULL)
1307 message_type = dbus_message_type_from_string (send_type);
1308 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1310 dbus_set_error (error, DBUS_ERROR_FAILED,
1311 "Bad message type \"%s\"",
1317 if (send_requested_reply &&
1318 !(strcmp (send_requested_reply, "true") == 0 ||
1319 strcmp (send_requested_reply, "false") == 0))
1321 dbus_set_error (error, DBUS_ERROR_FAILED,
1322 "Bad value \"%s\" for %s attribute, must be true or false",
1323 "send_requested_reply", send_requested_reply);
1327 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1331 if (send_requested_reply)
1332 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1334 rule->d.send.message_type = message_type;
1335 rule->d.send.path = _dbus_strdup (send_path);
1336 rule->d.send.interface = _dbus_strdup (send_interface);
1337 rule->d.send.member = _dbus_strdup (send_member);
1338 rule->d.send.error = _dbus_strdup (send_error);
1339 rule->d.send.destination = _dbus_strdup (send_destination);
1340 if (send_path && rule->d.send.path == NULL)
1342 if (send_interface && rule->d.send.interface == NULL)
1344 if (send_member && rule->d.send.member == NULL)
1346 if (send_error && rule->d.send.error == NULL)
1348 if (send_destination && rule->d.send.destination == NULL)
1351 else if (receive_interface || receive_member || receive_error || receive_sender ||
1352 receive_path || receive_type || eavesdrop || receive_requested_reply)
1356 if (IS_WILDCARD (receive_interface))
1357 receive_interface = NULL;
1358 if (IS_WILDCARD (receive_member))
1359 receive_member = NULL;
1360 if (IS_WILDCARD (receive_error))
1361 receive_error = NULL;
1362 if (IS_WILDCARD (receive_sender))
1363 receive_sender = NULL;
1364 if (IS_WILDCARD (receive_path))
1365 receive_path = NULL;
1366 if (IS_WILDCARD (receive_type))
1367 receive_type = NULL;
1369 message_type = DBUS_MESSAGE_TYPE_INVALID;
1370 if (receive_type != NULL)
1372 message_type = dbus_message_type_from_string (receive_type);
1373 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1375 dbus_set_error (error, DBUS_ERROR_FAILED,
1376 "Bad message type \"%s\"",
1384 !(strcmp (eavesdrop, "true") == 0 ||
1385 strcmp (eavesdrop, "false") == 0))
1387 dbus_set_error (error, DBUS_ERROR_FAILED,
1388 "Bad value \"%s\" for %s attribute, must be true or false",
1389 "eavesdrop", eavesdrop);
1393 if (receive_requested_reply &&
1394 !(strcmp (receive_requested_reply, "true") == 0 ||
1395 strcmp (receive_requested_reply, "false") == 0))
1397 dbus_set_error (error, DBUS_ERROR_FAILED,
1398 "Bad value \"%s\" for %s attribute, must be true or false",
1399 "receive_requested_reply", receive_requested_reply);
1403 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1408 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1410 if (receive_requested_reply)
1411 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1413 rule->d.receive.message_type = message_type;
1414 rule->d.receive.path = _dbus_strdup (receive_path);
1415 rule->d.receive.interface = _dbus_strdup (receive_interface);
1416 rule->d.receive.member = _dbus_strdup (receive_member);
1417 rule->d.receive.error = _dbus_strdup (receive_error);
1418 rule->d.receive.origin = _dbus_strdup (receive_sender);
1420 if (receive_path && rule->d.receive.path == NULL)
1422 if (receive_interface && rule->d.receive.interface == NULL)
1424 if (receive_member && rule->d.receive.member == NULL)
1426 if (receive_error && rule->d.receive.error == NULL)
1428 if (receive_sender && rule->d.receive.origin == NULL)
1433 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1437 if (IS_WILDCARD (own))
1440 rule->d.own.service_name = _dbus_strdup (own);
1441 if (own && rule->d.own.service_name == NULL)
1446 if (IS_WILDCARD (user))
1448 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1452 rule->d.user.uid = DBUS_UID_UNSET;
1456 DBusString username;
1459 _dbus_string_init_const (&username, user);
1461 if (_dbus_get_user_id (&username, &uid))
1463 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1467 rule->d.user.uid = uid;
1471 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1472 user, element_name);
1478 if (IS_WILDCARD (group))
1480 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1484 rule->d.group.gid = DBUS_GID_UNSET;
1488 DBusString groupname;
1491 _dbus_string_init_const (&groupname, group);
1493 if (_dbus_get_user_id (&groupname, &gid))
1495 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1499 rule->d.group.gid = gid;
1503 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1504 group, element_name);
1509 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1515 pe = peek_element (parser);
1516 _dbus_assert (pe != NULL);
1517 _dbus_assert (pe->type == ELEMENT_POLICY);
1519 switch (pe->d.policy.type)
1521 case POLICY_IGNORED:
1522 /* drop the rule on the floor */
1525 case POLICY_DEFAULT:
1526 if (!bus_policy_append_default_rule (parser->policy, rule))
1529 case POLICY_MANDATORY:
1530 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1534 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1536 dbus_set_error (error, DBUS_ERROR_FAILED,
1537 "<%s> rule cannot be per-user because it has bus-global semantics",
1542 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1547 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1549 dbus_set_error (error, DBUS_ERROR_FAILED,
1550 "<%s> rule cannot be per-group because it has bus-global semantics",
1555 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1561 case POLICY_CONSOLE:
1562 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1568 bus_policy_rule_unref (rule);
1575 BUS_SET_OOM (error);
1578 bus_policy_rule_unref (rule);
1583 start_policy_child (BusConfigParser *parser,
1584 const char *element_name,
1585 const char **attribute_names,
1586 const char **attribute_values,
1589 if (strcmp (element_name, "allow") == 0)
1591 if (!append_rule_from_element (parser, element_name,
1592 attribute_names, attribute_values,
1596 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1598 BUS_SET_OOM (error);
1604 else if (strcmp (element_name, "deny") == 0)
1606 if (!append_rule_from_element (parser, element_name,
1607 attribute_names, attribute_values,
1611 if (push_element (parser, ELEMENT_DENY) == NULL)
1613 BUS_SET_OOM (error);
1621 dbus_set_error (error, DBUS_ERROR_FAILED,
1622 "Element <%s> not allowed inside <%s> in configuration file",
1623 element_name, "policy");
1629 start_selinux_child (BusConfigParser *parser,
1630 const char *element_name,
1631 const char **attribute_names,
1632 const char **attribute_values,
1639 context_copy = NULL;
1641 if (strcmp (element_name, "associate") == 0)
1644 const char *context;
1646 if (!locate_attributes (parser, "associate",
1651 "context", &context,
1655 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1657 BUS_SET_OOM (error);
1661 if (own == NULL || context == NULL)
1663 dbus_set_error (error, DBUS_ERROR_FAILED,
1664 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1668 own_copy = _dbus_strdup (own);
1669 if (own_copy == NULL)
1671 context_copy = _dbus_strdup (context);
1672 if (context_copy == NULL)
1675 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1676 own_copy, context_copy))
1683 dbus_set_error (error, DBUS_ERROR_FAILED,
1684 "Element <%s> not allowed inside <%s> in configuration file",
1685 element_name, "selinux");
1691 dbus_free (own_copy);
1694 dbus_free (context_copy);
1696 BUS_SET_OOM (error);
1701 bus_config_parser_start_element (BusConfigParser *parser,
1702 const char *element_name,
1703 const char **attribute_names,
1704 const char **attribute_values,
1709 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1711 /* printf ("START: %s\n", element_name); */
1713 t = top_element_type (parser);
1715 if (t == ELEMENT_NONE)
1717 if (strcmp (element_name, "busconfig") == 0)
1719 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1722 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1724 BUS_SET_OOM (error);
1732 dbus_set_error (error, DBUS_ERROR_FAILED,
1733 "Unknown element <%s> at root of configuration file",
1738 else if (t == ELEMENT_BUSCONFIG)
1740 return start_busconfig_child (parser, element_name,
1741 attribute_names, attribute_values,
1744 else if (t == ELEMENT_POLICY)
1746 return start_policy_child (parser, element_name,
1747 attribute_names, attribute_values,
1750 else if (t == ELEMENT_SELINUX)
1752 return start_selinux_child (parser, element_name,
1753 attribute_names, attribute_values,
1758 dbus_set_error (error, DBUS_ERROR_FAILED,
1759 "Element <%s> is not allowed in this context",
1766 set_limit (BusConfigParser *parser,
1771 dbus_bool_t must_be_positive;
1772 dbus_bool_t must_be_int;
1774 must_be_int = FALSE;
1775 must_be_positive = FALSE;
1777 if (strcmp (name, "max_incoming_bytes") == 0)
1779 must_be_positive = TRUE;
1780 parser->limits.max_incoming_bytes = value;
1782 else if (strcmp (name, "max_outgoing_bytes") == 0)
1784 must_be_positive = TRUE;
1785 parser->limits.max_outgoing_bytes = value;
1787 else if (strcmp (name, "max_message_size") == 0)
1789 must_be_positive = TRUE;
1790 parser->limits.max_message_size = value;
1792 else if (strcmp (name, "service_start_timeout") == 0)
1794 must_be_positive = TRUE;
1796 parser->limits.activation_timeout = value;
1798 else if (strcmp (name, "auth_timeout") == 0)
1800 must_be_positive = TRUE;
1802 parser->limits.auth_timeout = value;
1804 else if (strcmp (name, "reply_timeout") == 0)
1806 must_be_positive = TRUE;
1808 parser->limits.reply_timeout = value;
1810 else if (strcmp (name, "max_completed_connections") == 0)
1812 must_be_positive = TRUE;
1814 parser->limits.max_completed_connections = value;
1816 else if (strcmp (name, "max_incomplete_connections") == 0)
1818 must_be_positive = TRUE;
1820 parser->limits.max_incomplete_connections = value;
1822 else if (strcmp (name, "max_connections_per_user") == 0)
1824 must_be_positive = TRUE;
1826 parser->limits.max_connections_per_user = value;
1828 else if (strcmp (name, "max_pending_service_starts") == 0)
1830 must_be_positive = TRUE;
1832 parser->limits.max_pending_activations = value;
1834 else if (strcmp (name, "max_names_per_connection") == 0)
1836 must_be_positive = TRUE;
1838 parser->limits.max_services_per_connection = value;
1840 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1842 must_be_positive = TRUE;
1844 parser->limits.max_match_rules_per_connection = value;
1846 else if (strcmp (name, "max_replies_per_connection") == 0)
1848 must_be_positive = TRUE;
1850 parser->limits.max_replies_per_connection = value;
1854 dbus_set_error (error, DBUS_ERROR_FAILED,
1855 "There is no limit called \"%s\"\n",
1860 if (must_be_positive && value < 0)
1862 dbus_set_error (error, DBUS_ERROR_FAILED,
1863 "<limit name=\"%s\"> must be a positive number\n",
1869 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1871 dbus_set_error (error, DBUS_ERROR_FAILED,
1872 "<limit name=\"%s\"> value is too large\n",
1881 bus_config_parser_end_element (BusConfigParser *parser,
1882 const char *element_name,
1889 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1891 /* printf ("END: %s\n", element_name); */
1893 t = top_element_type (parser);
1895 if (t == ELEMENT_NONE)
1897 /* should probably be an assertion failure but
1898 * being paranoid about XML parsers
1900 dbus_set_error (error, DBUS_ERROR_FAILED,
1901 "XML parser ended element with no element on the stack");
1905 n = element_type_to_name (t);
1906 _dbus_assert (n != NULL);
1907 if (strcmp (n, element_name) != 0)
1909 /* should probably be an assertion failure but
1910 * being paranoid about XML parsers
1912 dbus_set_error (error, DBUS_ERROR_FAILED,
1913 "XML element <%s> ended but topmost element on the stack was <%s>",
1918 e = peek_element (parser);
1919 _dbus_assert (e != NULL);
1924 _dbus_assert_not_reached ("element in stack has no type");
1927 case ELEMENT_INCLUDE:
1930 case ELEMENT_LISTEN:
1931 case ELEMENT_PIDFILE:
1933 case ELEMENT_SERVICEDIR:
1934 case ELEMENT_INCLUDEDIR:
1936 if (!e->had_content)
1938 dbus_set_error (error, DBUS_ERROR_FAILED,
1939 "XML element <%s> was expected to have content inside it",
1940 element_type_to_name (e->type));
1944 if (e->type == ELEMENT_LIMIT)
1946 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1952 case ELEMENT_BUSCONFIG:
1953 case ELEMENT_POLICY:
1957 case ELEMENT_SELINUX:
1958 case ELEMENT_ASSOCIATE:
1959 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
1963 pop_element (parser);
1969 all_whitespace (const DBusString *str)
1973 _dbus_string_skip_white (str, 0, &i);
1975 return i == _dbus_string_get_length (str);
1979 make_full_path (const DBusString *basedir,
1980 const DBusString *filename,
1981 DBusString *full_path)
1983 if (_dbus_path_is_absolute (filename))
1985 return _dbus_string_copy (filename, 0, full_path, 0);
1989 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1992 if (!_dbus_concat_dir_and_file (full_path, filename))
2000 include_file (BusConfigParser *parser,
2001 const DBusString *filename,
2002 dbus_bool_t ignore_missing,
2005 /* FIXME good test case for this would load each config file in the
2006 * test suite both alone, and as an include, and check
2007 * that the result is the same
2009 BusConfigParser *included;
2010 const char *filename_str;
2011 DBusError tmp_error;
2013 dbus_error_init (&tmp_error);
2015 filename_str = _dbus_string_get_const_data (filename);
2017 /* Check to make sure this file hasn't already been included. */
2018 if (seen_include (parser, filename))
2020 dbus_set_error (error, DBUS_ERROR_FAILED,
2021 "Circular inclusion of file '%s'",
2026 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2028 BUS_SET_OOM (error);
2032 /* Since parser is passed in as the parent, included
2033 inherits parser's limits. */
2034 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2036 _dbus_list_pop_last (&parser->included_files);
2038 if (included == NULL)
2040 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2042 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2045 dbus_error_free (&tmp_error);
2050 dbus_move_error (&tmp_error, error);
2056 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2058 if (!merge_included (parser, included, error))
2060 bus_config_parser_unref (included);
2064 /* Copy included's limits back to parser. */
2065 parser->limits = included->limits;
2067 bus_config_parser_unref (included);
2073 include_dir (BusConfigParser *parser,
2074 const DBusString *dirname,
2077 DBusString filename;
2079 DBusError tmp_error;
2083 if (!_dbus_string_init (&filename))
2085 BUS_SET_OOM (error);
2091 dir = _dbus_directory_open (dirname, error);
2096 dbus_error_init (&tmp_error);
2097 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2099 DBusString full_path;
2101 if (!_dbus_string_init (&full_path))
2103 BUS_SET_OOM (error);
2107 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2109 BUS_SET_OOM (error);
2110 _dbus_string_free (&full_path);
2114 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2116 BUS_SET_OOM (error);
2117 _dbus_string_free (&full_path);
2121 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2123 if (!include_file (parser, &full_path, TRUE, error))
2125 _dbus_string_free (&full_path);
2130 _dbus_string_free (&full_path);
2133 if (dbus_error_is_set (&tmp_error))
2135 dbus_move_error (&tmp_error, error);
2140 if (!_dbus_string_copy_data (dirname, &s))
2142 BUS_SET_OOM (error);
2146 if (!_dbus_list_append (&parser->conf_dirs, s))
2149 BUS_SET_OOM (error);
2156 _dbus_string_free (&filename);
2159 _dbus_directory_close (dir);
2165 bus_config_parser_content (BusConfigParser *parser,
2166 const DBusString *content,
2171 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2177 _dbus_string_get_const_data (content, &c_str);
2179 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2183 e = peek_element (parser);
2186 dbus_set_error (error, DBUS_ERROR_FAILED,
2187 "Text content outside of any XML element in configuration file");
2190 else if (e->had_content)
2192 _dbus_assert_not_reached ("Element had multiple content blocks");
2196 switch (top_element_type (parser))
2199 _dbus_assert_not_reached ("element at top of stack has no type");
2202 case ELEMENT_BUSCONFIG:
2203 case ELEMENT_POLICY:
2207 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2208 case ELEMENT_SELINUX:
2209 case ELEMENT_ASSOCIATE:
2210 if (all_whitespace (content))
2214 dbus_set_error (error, DBUS_ERROR_FAILED,
2215 "No text content expected inside XML element %s in configuration file",
2216 element_type_to_name (top_element_type (parser)));
2220 case ELEMENT_PIDFILE:
2224 e->had_content = TRUE;
2226 if (!_dbus_string_copy_data (content, &s))
2229 dbus_free (parser->pidfile);
2230 parser->pidfile = s;
2234 case ELEMENT_INCLUDE:
2236 DBusString full_path, selinux_policy_root;
2238 e->had_content = TRUE;
2240 if (e->d.include.if_selinux_enabled
2241 && !bus_selinux_enabled ())
2244 if (!_dbus_string_init (&full_path))
2247 if (e->d.include.selinux_root_relative)
2249 if (!bus_selinux_get_policy_root ())
2251 dbus_set_error (error, DBUS_ERROR_FAILED,
2252 "Could not determine SELinux policy root for relative inclusion");
2253 _dbus_string_free (&full_path);
2256 _dbus_string_init_const (&selinux_policy_root,
2257 bus_selinux_get_policy_root ());
2258 if (!make_full_path (&selinux_policy_root, content, &full_path))
2260 _dbus_string_free (&full_path);
2264 else if (!make_full_path (&parser->basedir, content, &full_path))
2266 _dbus_string_free (&full_path);
2270 if (!include_file (parser, &full_path,
2271 e->d.include.ignore_missing, error))
2273 _dbus_string_free (&full_path);
2277 _dbus_string_free (&full_path);
2281 case ELEMENT_INCLUDEDIR:
2283 DBusString full_path;
2285 e->had_content = TRUE;
2287 if (!_dbus_string_init (&full_path))
2290 if (!make_full_path (&parser->basedir, content, &full_path))
2292 _dbus_string_free (&full_path);
2296 if (!include_dir (parser, &full_path, error))
2298 _dbus_string_free (&full_path);
2302 _dbus_string_free (&full_path);
2310 e->had_content = TRUE;
2312 if (!_dbus_string_copy_data (content, &s))
2315 dbus_free (parser->user);
2324 e->had_content = TRUE;
2326 if (!_dbus_string_copy_data (content, &s))
2329 dbus_free (parser->bus_type);
2330 parser->bus_type = s;
2334 case ELEMENT_LISTEN:
2338 e->had_content = TRUE;
2340 if (!_dbus_string_copy_data (content, &s))
2343 if (!_dbus_list_append (&parser->listen_on,
2356 e->had_content = TRUE;
2358 if (!_dbus_string_copy_data (content, &s))
2361 if (!_dbus_list_append (&parser->mechanisms,
2370 case ELEMENT_SERVICEDIR:
2373 DBusString full_path;
2375 e->had_content = TRUE;
2377 if (!_dbus_string_init (&full_path))
2380 if (!make_full_path (&parser->basedir, content, &full_path))
2382 _dbus_string_free (&full_path);
2386 if (!_dbus_string_copy_data (&full_path, &s))
2388 _dbus_string_free (&full_path);
2392 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2394 _dbus_string_free (&full_path);
2399 _dbus_string_free (&full_path);
2407 e->had_content = TRUE;
2410 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2412 dbus_set_error (error, DBUS_ERROR_FAILED,
2413 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2418 e->d.limit.value = val;
2420 _dbus_verbose ("Loaded value %ld for limit %s\n",
2427 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2431 BUS_SET_OOM (error);
2436 bus_config_parser_finished (BusConfigParser *parser,
2439 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2441 if (parser->stack != NULL)
2443 dbus_set_error (error, DBUS_ERROR_FAILED,
2444 "Element <%s> was not closed in configuration file",
2445 element_type_to_name (top_element_type (parser)));
2450 if (parser->is_toplevel && parser->listen_on == NULL)
2452 dbus_set_error (error, DBUS_ERROR_FAILED,
2453 "Configuration file needs one or more <listen> elements giving addresses");
2461 bus_config_parser_get_user (BusConfigParser *parser)
2463 return parser->user;
2467 bus_config_parser_get_type (BusConfigParser *parser)
2469 return parser->bus_type;
2473 bus_config_parser_get_addresses (BusConfigParser *parser)
2475 return &parser->listen_on;
2479 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2481 return &parser->mechanisms;
2485 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2487 return &parser->service_dirs;
2491 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2493 return &parser->conf_dirs;
2497 bus_config_parser_get_fork (BusConfigParser *parser)
2499 return parser->fork;
2503 bus_config_parser_get_pidfile (BusConfigParser *parser)
2505 return parser->pidfile;
2509 bus_config_parser_steal_policy (BusConfigParser *parser)
2513 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2515 policy = parser->policy;
2517 parser->policy = NULL;
2522 /* Overwrite any limits that were set in the configuration file */
2524 bus_config_parser_get_limits (BusConfigParser *parser,
2527 *limits = parser->limits;
2531 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2533 DBusHashTable *table;
2535 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2537 table = parser->service_context_table;
2539 parser->service_context_table = NULL;
2544 #ifdef DBUS_BUILD_TESTS
2555 do_load (const DBusString *full_path,
2557 dbus_bool_t oom_possible)
2559 BusConfigParser *parser;
2562 dbus_error_init (&error);
2564 parser = bus_config_load (full_path, TRUE, NULL, &error);
2567 _DBUS_ASSERT_ERROR_IS_SET (&error);
2570 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2572 _dbus_verbose ("Failed to load valid file due to OOM\n");
2573 dbus_error_free (&error);
2576 else if (validity == VALID)
2578 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2581 dbus_error_free (&error);
2586 dbus_error_free (&error);
2592 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2594 bus_config_parser_unref (parser);
2596 if (validity == INVALID)
2598 _dbus_warn ("Accepted invalid file\n");
2608 const DBusString *full_path;
2613 check_loader_oom_func (void *data)
2615 LoaderOomData *d = data;
2617 return do_load (d->full_path, d->validity, TRUE);
2621 process_test_valid_subdir (const DBusString *test_base_dir,
2625 DBusString test_directory;
2626 DBusString filename;
2634 if (!_dbus_string_init (&test_directory))
2635 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2637 _dbus_string_init_const (&filename, subdir);
2639 if (!_dbus_string_copy (test_base_dir, 0,
2640 &test_directory, 0))
2641 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2643 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2644 _dbus_assert_not_reached ("couldn't allocate full path");
2646 _dbus_string_free (&filename);
2647 if (!_dbus_string_init (&filename))
2648 _dbus_assert_not_reached ("didn't allocate filename string\n");
2650 dbus_error_init (&error);
2651 dir = _dbus_directory_open (&test_directory, &error);
2654 _dbus_warn ("Could not open %s: %s\n",
2655 _dbus_string_get_const_data (&test_directory),
2657 dbus_error_free (&error);
2661 if (validity == VALID)
2662 printf ("Testing valid files:\n");
2663 else if (validity == INVALID)
2664 printf ("Testing invalid files:\n");
2666 printf ("Testing unknown files:\n");
2669 while (_dbus_directory_get_next_file (dir, &filename, &error))
2671 DBusString full_path;
2674 if (!_dbus_string_init (&full_path))
2675 _dbus_assert_not_reached ("couldn't init string");
2677 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2678 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2680 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2681 _dbus_assert_not_reached ("couldn't concat file to dir");
2683 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2685 _dbus_verbose ("Skipping non-.conf file %s\n",
2686 _dbus_string_get_const_data (&filename));
2687 _dbus_string_free (&full_path);
2691 printf (" %s\n", _dbus_string_get_const_data (&filename));
2693 _dbus_verbose (" expecting %s\n",
2694 validity == VALID ? "valid" :
2695 (validity == INVALID ? "invalid" :
2696 (validity == UNKNOWN ? "unknown" : "???")));
2698 d.full_path = &full_path;
2699 d.validity = validity;
2701 /* FIXME hackaround for an expat problem, see
2702 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2703 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2705 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2706 if (!check_loader_oom_func (&d))
2707 _dbus_assert_not_reached ("test failed");
2709 _dbus_string_free (&full_path);
2712 if (dbus_error_is_set (&error))
2714 _dbus_warn ("Could not get next file in %s: %s\n",
2715 _dbus_string_get_const_data (&test_directory),
2717 dbus_error_free (&error);
2726 _dbus_directory_close (dir);
2727 _dbus_string_free (&test_directory);
2728 _dbus_string_free (&filename);
2734 bools_equal (dbus_bool_t a,
2741 strings_equal_or_both_null (const char *a,
2744 if (a == NULL || b == NULL)
2747 return !strcmp (a, b);
2751 elements_equal (const Element *a,
2754 if (a->type != b->type)
2757 if (!bools_equal (a->had_content, b->had_content))
2763 case ELEMENT_INCLUDE:
2764 if (!bools_equal (a->d.include.ignore_missing,
2765 b->d.include.ignore_missing))
2769 case ELEMENT_POLICY:
2770 if (a->d.policy.type != b->d.policy.type)
2772 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
2777 if (strcmp (a->d.limit.name, b->d.limit.name))
2779 if (a->d.limit.value != b->d.limit.value)
2793 lists_of_elements_equal (DBusList *a,
2802 while (ia != NULL && ib != NULL)
2804 if (elements_equal (ia->data, ib->data))
2806 ia = _dbus_list_get_next_link (&a, ia);
2807 ib = _dbus_list_get_next_link (&b, ib);
2810 return ia == NULL && ib == NULL;
2814 lists_of_c_strings_equal (DBusList *a,
2823 while (ia != NULL && ib != NULL)
2825 if (strcmp (ia->data, ib->data))
2827 ia = _dbus_list_get_next_link (&a, ia);
2828 ib = _dbus_list_get_next_link (&b, ib);
2831 return ia == NULL && ib == NULL;
2835 limits_equal (const BusLimits *a,
2839 (a->max_incoming_bytes == b->max_incoming_bytes
2840 || a->max_outgoing_bytes == b->max_outgoing_bytes
2841 || a->max_message_size == b->max_message_size
2842 || a->activation_timeout == b->activation_timeout
2843 || a->auth_timeout == b->auth_timeout
2844 || a->max_completed_connections == b->max_completed_connections
2845 || a->max_incomplete_connections == b->max_incomplete_connections
2846 || a->max_connections_per_user == b->max_connections_per_user
2847 || a->max_pending_activations == b->max_pending_activations
2848 || a->max_services_per_connection == b->max_services_per_connection
2849 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2850 || a->max_replies_per_connection == b->max_replies_per_connection
2851 || a->reply_timeout == b->reply_timeout);
2855 config_parsers_equal (const BusConfigParser *a,
2856 const BusConfigParser *b)
2858 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2861 if (!lists_of_elements_equal (a->stack, b->stack))
2864 if (!strings_equal_or_both_null (a->user, b->user))
2867 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2870 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2873 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2876 /* FIXME: compare policy */
2878 /* FIXME: compare service selinux ID table */
2880 if (! limits_equal (&a->limits, &b->limits))
2883 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2886 if (! bools_equal (a->fork, b->fork))
2889 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2896 all_are_equiv (const DBusString *target_directory)
2898 DBusString filename;
2900 BusConfigParser *first_parser;
2901 BusConfigParser *parser;
2907 first_parser = NULL;
2911 if (!_dbus_string_init (&filename))
2912 _dbus_assert_not_reached ("didn't allocate filename string");
2914 dbus_error_init (&error);
2915 dir = _dbus_directory_open (target_directory, &error);
2918 _dbus_warn ("Could not open %s: %s\n",
2919 _dbus_string_get_const_data (target_directory),
2921 dbus_error_free (&error);
2925 printf ("Comparing equivalent files:\n");
2928 while (_dbus_directory_get_next_file (dir, &filename, &error))
2930 DBusString full_path;
2932 if (!_dbus_string_init (&full_path))
2933 _dbus_assert_not_reached ("couldn't init string");
2935 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2936 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2938 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2939 _dbus_assert_not_reached ("couldn't concat file to dir");
2941 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2943 _dbus_verbose ("Skipping non-.conf file %s\n",
2944 _dbus_string_get_const_data (&filename));
2945 _dbus_string_free (&full_path);
2949 printf (" %s\n", _dbus_string_get_const_data (&filename));
2951 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2955 _dbus_warn ("Could not load file %s: %s\n",
2956 _dbus_string_get_const_data (&full_path),
2958 _dbus_string_free (&full_path);
2959 dbus_error_free (&error);
2962 else if (first_parser == NULL)
2964 _dbus_string_free (&full_path);
2965 first_parser = parser;
2969 _dbus_string_free (&full_path);
2970 equal = config_parsers_equal (first_parser, parser);
2971 bus_config_parser_unref (parser);
2980 _dbus_string_free (&filename);
2982 bus_config_parser_unref (first_parser);
2984 _dbus_directory_close (dir);
2991 process_test_equiv_subdir (const DBusString *test_base_dir,
2994 DBusString test_directory;
2995 DBusString filename;
3004 if (!_dbus_string_init (&test_directory))
3005 _dbus_assert_not_reached ("didn't allocate test_directory");
3007 _dbus_string_init_const (&filename, subdir);
3009 if (!_dbus_string_copy (test_base_dir, 0,
3010 &test_directory, 0))
3011 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3013 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3014 _dbus_assert_not_reached ("couldn't allocate full path");
3016 _dbus_string_free (&filename);
3017 if (!_dbus_string_init (&filename))
3018 _dbus_assert_not_reached ("didn't allocate filename string");
3020 dbus_error_init (&error);
3021 dir = _dbus_directory_open (&test_directory, &error);
3024 _dbus_warn ("Could not open %s: %s\n",
3025 _dbus_string_get_const_data (&test_directory),
3027 dbus_error_free (&error);
3031 while (_dbus_directory_get_next_file (dir, &filename, &error))
3033 DBusString full_path;
3035 /* Skip CVS's magic directories! */
3036 if (_dbus_string_equal_c_str (&filename, "CVS"))
3039 if (!_dbus_string_init (&full_path))
3040 _dbus_assert_not_reached ("couldn't init string");
3042 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3043 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3045 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3046 _dbus_assert_not_reached ("couldn't concat file to dir");
3048 equal = all_are_equiv (&full_path);
3049 _dbus_string_free (&full_path);
3058 _dbus_string_free (&test_directory);
3059 _dbus_string_free (&filename);
3061 _dbus_directory_close (dir);
3067 static const char *test_service_dir_matches[] =
3069 "/testusr/testlocal/testshare/dbus-1/services",
3070 "/testusr/testshare/dbus-1/services",
3071 DBUS_DATADIR"/dbus-1/services",
3072 "/testhome/foo/.testlocal/testshare/dbus-1/services",
3077 test_default_session_servicedirs (void)
3085 printf ("Testing retriving the default session service directories\n");
3086 if (!_dbus_get_standard_session_servicedirs (&dirs))
3087 _dbus_assert_not_reached ("couldn't get stardard dirs");
3089 /* make sure our defaults end with share/dbus-1/service */
3090 while ((link = _dbus_list_pop_first_link (&dirs)))
3094 printf (" default service dir: %s\n", (char *)link->data);
3095 _dbus_string_init_const (&path, (char *)link->data);
3096 if (!_dbus_string_ends_with_c_str (&path, "share/dbus-1/services"))
3098 printf ("error with default session service directories\n");
3102 dbus_free (link->data);
3103 _dbus_list_free_link (link);
3106 if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3107 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3109 if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3110 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3112 if (!_dbus_get_standard_session_servicedirs (&dirs))
3113 _dbus_assert_not_reached ("couldn't get stardard dirs");
3115 /* make sure we read and parse the env variable correctly */
3117 while ((link = _dbus_list_pop_first_link (&dirs)))
3119 printf (" test service dir: %s\n", (char *)link->data);
3120 if (test_service_dir_matches[i] == NULL)
3122 printf ("more directories parsed than in match set\n");
3126 if (strcmp (test_service_dir_matches[i],
3127 (char *)link->data) != 0)
3129 printf ("%s directory does not match %s in the match set\n",
3131 test_service_dir_matches[i]);
3137 dbus_free (link->data);
3138 _dbus_list_free_link (link);
3141 if (test_service_dir_matches[i] != NULL)
3143 printf ("extra data %s in the match set was not matched\n",
3144 test_service_dir_matches[i]);
3153 bus_config_parser_test (const DBusString *test_data_dir)
3155 if (test_data_dir == NULL ||
3156 _dbus_string_get_length (test_data_dir) == 0)
3158 printf ("No test data\n");
3162 if (!test_default_session_servicedirs())
3165 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3168 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3171 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3177 #endif /* DBUS_BUILD_TESTS */