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 = link->next)
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)
808 if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
811 if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
819 else if (strcmp (element_name, "servicedir") == 0)
821 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
824 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
832 else if (strcmp (element_name, "include") == 0)
835 const char *if_selinux_enabled;
836 const char *ignore_missing;
837 const char *selinux_root_relative;
839 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
845 e->d.include.ignore_missing = FALSE;
846 e->d.include.if_selinux_enabled = FALSE;
847 e->d.include.selinux_root_relative = FALSE;
849 if (!locate_attributes (parser, "include",
853 "ignore_missing", &ignore_missing,
854 "if_selinux_enabled", &if_selinux_enabled,
855 "selinux_root_relative", &selinux_root_relative,
859 if (ignore_missing != NULL)
861 if (strcmp (ignore_missing, "yes") == 0)
862 e->d.include.ignore_missing = TRUE;
863 else if (strcmp (ignore_missing, "no") == 0)
864 e->d.include.ignore_missing = FALSE;
867 dbus_set_error (error, DBUS_ERROR_FAILED,
868 "ignore_missing attribute must have value \"yes\" or \"no\"");
873 if (if_selinux_enabled != NULL)
875 if (strcmp (if_selinux_enabled, "yes") == 0)
876 e->d.include.if_selinux_enabled = TRUE;
877 else if (strcmp (if_selinux_enabled, "no") == 0)
878 e->d.include.if_selinux_enabled = FALSE;
881 dbus_set_error (error, DBUS_ERROR_FAILED,
882 "if_selinux_enabled attribute must have value"
883 " \"yes\" or \"no\"");
888 if (selinux_root_relative != NULL)
890 if (strcmp (selinux_root_relative, "yes") == 0)
891 e->d.include.selinux_root_relative = TRUE;
892 else if (strcmp (selinux_root_relative, "no") == 0)
893 e->d.include.selinux_root_relative = FALSE;
896 dbus_set_error (error, DBUS_ERROR_FAILED,
897 "selinux_root_relative attribute must have value"
898 " \"yes\" or \"no\"");
905 else if (strcmp (element_name, "policy") == 0)
911 const char *at_console;
913 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
919 e->d.policy.type = POLICY_IGNORED;
921 if (!locate_attributes (parser, "policy",
928 "at_console", &at_console,
932 if (((context && user) ||
933 (context && group) ||
934 (context && at_console)) ||
936 (user && at_console)) ||
937 (group && at_console) ||
938 !(context || user || group || at_console))
940 dbus_set_error (error, DBUS_ERROR_FAILED,
941 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
947 if (strcmp (context, "default") == 0)
949 e->d.policy.type = POLICY_DEFAULT;
951 else if (strcmp (context, "mandatory") == 0)
953 e->d.policy.type = POLICY_MANDATORY;
957 dbus_set_error (error, DBUS_ERROR_FAILED,
958 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
963 else if (user != NULL)
966 _dbus_string_init_const (&username, user);
968 if (_dbus_get_user_id (&username,
969 &e->d.policy.gid_uid_or_at_console))
970 e->d.policy.type = POLICY_USER;
972 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
975 else if (group != NULL)
977 DBusString group_name;
978 _dbus_string_init_const (&group_name, group);
980 if (_dbus_get_group_id (&group_name,
981 &e->d.policy.gid_uid_or_at_console))
982 e->d.policy.type = POLICY_GROUP;
984 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
987 else if (at_console != NULL)
990 t = (strcmp (at_console, "true") == 0);
991 if (t || strcmp (at_console, "false") == 0)
993 e->d.policy.gid_uid_or_at_console = t;
994 e->d.policy.type = POLICY_CONSOLE;
998 dbus_set_error (error, DBUS_ERROR_FAILED,
999 "Unknown value \"%s\" for at_console in message bus configuration file",
1007 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
1012 else if (strcmp (element_name, "limit") == 0)
1017 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1019 BUS_SET_OOM (error);
1023 if (!locate_attributes (parser, "limit",
1033 dbus_set_error (error, DBUS_ERROR_FAILED,
1034 "<limit> element must have a \"name\" attribute");
1038 e->d.limit.name = _dbus_strdup (name);
1039 if (e->d.limit.name == NULL)
1041 BUS_SET_OOM (error);
1047 else if (strcmp (element_name, "selinux") == 0)
1049 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1052 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1054 BUS_SET_OOM (error);
1062 dbus_set_error (error, DBUS_ERROR_FAILED,
1063 "Element <%s> not allowed inside <%s> in configuration file",
1064 element_name, "busconfig");
1070 append_rule_from_element (BusConfigParser *parser,
1071 const char *element_name,
1072 const char **attribute_names,
1073 const char **attribute_values,
1077 const char *send_interface;
1078 const char *send_member;
1079 const char *send_error;
1080 const char *send_destination;
1081 const char *send_path;
1082 const char *send_type;
1083 const char *receive_interface;
1084 const char *receive_member;
1085 const char *receive_error;
1086 const char *receive_sender;
1087 const char *receive_path;
1088 const char *receive_type;
1089 const char *eavesdrop;
1090 const char *send_requested_reply;
1091 const char *receive_requested_reply;
1096 BusPolicyRule *rule;
1098 if (!locate_attributes (parser, element_name,
1102 "send_interface", &send_interface,
1103 "send_member", &send_member,
1104 "send_error", &send_error,
1105 "send_destination", &send_destination,
1106 "send_path", &send_path,
1107 "send_type", &send_type,
1108 "receive_interface", &receive_interface,
1109 "receive_member", &receive_member,
1110 "receive_error", &receive_error,
1111 "receive_sender", &receive_sender,
1112 "receive_path", &receive_path,
1113 "receive_type", &receive_type,
1114 "eavesdrop", &eavesdrop,
1115 "send_requested_reply", &send_requested_reply,
1116 "receive_requested_reply", &receive_requested_reply,
1123 if (!(send_interface || send_member || send_error || send_destination ||
1124 send_type || send_path ||
1125 receive_interface || receive_member || receive_error || receive_sender ||
1126 receive_type || receive_path || eavesdrop ||
1127 send_requested_reply || receive_requested_reply ||
1128 own || user || group))
1130 dbus_set_error (error, DBUS_ERROR_FAILED,
1131 "Element <%s> must have one or more attributes",
1136 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1137 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1139 dbus_set_error (error, DBUS_ERROR_FAILED,
1140 "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.",
1145 /* Allowed combinations of elements are:
1147 * base, must be all send or all receive:
1150 * interface + member
1153 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1154 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1156 * user, group, own must occur alone
1158 * Pretty sure the below stuff is broken, FIXME think about it more.
1161 if (((send_interface && send_error) ||
1162 (send_interface && receive_interface) ||
1163 (send_interface && receive_member) ||
1164 (send_interface && receive_error) ||
1165 (send_interface && receive_sender) ||
1166 (send_interface && eavesdrop) ||
1167 (send_interface && receive_requested_reply) ||
1168 (send_interface && own) ||
1169 (send_interface && user) ||
1170 (send_interface && group)) ||
1172 ((send_member && send_error) ||
1173 (send_member && receive_interface) ||
1174 (send_member && receive_member) ||
1175 (send_member && receive_error) ||
1176 (send_member && receive_sender) ||
1177 (send_member && eavesdrop) ||
1178 (send_member && receive_requested_reply) ||
1179 (send_member && own) ||
1180 (send_member && user) ||
1181 (send_member && group)) ||
1183 ((send_error && receive_interface) ||
1184 (send_error && receive_member) ||
1185 (send_error && receive_error) ||
1186 (send_error && receive_sender) ||
1187 (send_error && eavesdrop) ||
1188 (send_error && receive_requested_reply) ||
1189 (send_error && own) ||
1190 (send_error && user) ||
1191 (send_error && group)) ||
1193 ((send_destination && receive_interface) ||
1194 (send_destination && receive_member) ||
1195 (send_destination && receive_error) ||
1196 (send_destination && receive_sender) ||
1197 (send_destination && eavesdrop) ||
1198 (send_destination && receive_requested_reply) ||
1199 (send_destination && own) ||
1200 (send_destination && user) ||
1201 (send_destination && group)) ||
1203 ((send_type && receive_interface) ||
1204 (send_type && receive_member) ||
1205 (send_type && receive_error) ||
1206 (send_type && receive_sender) ||
1207 (send_type && eavesdrop) ||
1208 (send_type && receive_requested_reply) ||
1209 (send_type && own) ||
1210 (send_type && user) ||
1211 (send_type && group)) ||
1213 ((send_path && receive_interface) ||
1214 (send_path && receive_member) ||
1215 (send_path && receive_error) ||
1216 (send_path && receive_sender) ||
1217 (send_path && eavesdrop) ||
1218 (send_path && receive_requested_reply) ||
1219 (send_path && own) ||
1220 (send_path && user) ||
1221 (send_path && group)) ||
1223 ((send_requested_reply && receive_interface) ||
1224 (send_requested_reply && receive_member) ||
1225 (send_requested_reply && receive_error) ||
1226 (send_requested_reply && receive_sender) ||
1227 (send_requested_reply && eavesdrop) ||
1228 (send_requested_reply && receive_requested_reply) ||
1229 (send_requested_reply && own) ||
1230 (send_requested_reply && user) ||
1231 (send_requested_reply && group)) ||
1233 ((receive_interface && receive_error) ||
1234 (receive_interface && own) ||
1235 (receive_interface && user) ||
1236 (receive_interface && group)) ||
1238 ((receive_member && receive_error) ||
1239 (receive_member && own) ||
1240 (receive_member && user) ||
1241 (receive_member && group)) ||
1243 ((receive_error && own) ||
1244 (receive_error && user) ||
1245 (receive_error && group)) ||
1247 ((eavesdrop && own) ||
1248 (eavesdrop && user) ||
1249 (eavesdrop && group)) ||
1251 ((receive_requested_reply && own) ||
1252 (receive_requested_reply && user) ||
1253 (receive_requested_reply && group)) ||
1260 dbus_set_error (error, DBUS_ERROR_FAILED,
1261 "Invalid combination of attributes on element <%s>",
1268 /* In BusPolicyRule, NULL represents wildcard.
1269 * In the config file, '*' represents it.
1271 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1273 if (send_interface || send_member || send_error || send_destination ||
1274 send_path || send_type || send_requested_reply)
1278 if (IS_WILDCARD (send_interface))
1279 send_interface = NULL;
1280 if (IS_WILDCARD (send_member))
1282 if (IS_WILDCARD (send_error))
1284 if (IS_WILDCARD (send_destination))
1285 send_destination = NULL;
1286 if (IS_WILDCARD (send_path))
1288 if (IS_WILDCARD (send_type))
1291 message_type = DBUS_MESSAGE_TYPE_INVALID;
1292 if (send_type != NULL)
1294 message_type = dbus_message_type_from_string (send_type);
1295 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1297 dbus_set_error (error, DBUS_ERROR_FAILED,
1298 "Bad message type \"%s\"",
1304 if (send_requested_reply &&
1305 !(strcmp (send_requested_reply, "true") == 0 ||
1306 strcmp (send_requested_reply, "false") == 0))
1308 dbus_set_error (error, DBUS_ERROR_FAILED,
1309 "Bad value \"%s\" for %s attribute, must be true or false",
1310 "send_requested_reply", send_requested_reply);
1314 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1318 if (send_requested_reply)
1319 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1321 rule->d.send.message_type = message_type;
1322 rule->d.send.path = _dbus_strdup (send_path);
1323 rule->d.send.interface = _dbus_strdup (send_interface);
1324 rule->d.send.member = _dbus_strdup (send_member);
1325 rule->d.send.error = _dbus_strdup (send_error);
1326 rule->d.send.destination = _dbus_strdup (send_destination);
1327 if (send_path && rule->d.send.path == NULL)
1329 if (send_interface && rule->d.send.interface == NULL)
1331 if (send_member && rule->d.send.member == NULL)
1333 if (send_error && rule->d.send.error == NULL)
1335 if (send_destination && rule->d.send.destination == NULL)
1338 else if (receive_interface || receive_member || receive_error || receive_sender ||
1339 receive_path || receive_type || eavesdrop || receive_requested_reply)
1343 if (IS_WILDCARD (receive_interface))
1344 receive_interface = NULL;
1345 if (IS_WILDCARD (receive_member))
1346 receive_member = NULL;
1347 if (IS_WILDCARD (receive_error))
1348 receive_error = NULL;
1349 if (IS_WILDCARD (receive_sender))
1350 receive_sender = NULL;
1351 if (IS_WILDCARD (receive_path))
1352 receive_path = NULL;
1353 if (IS_WILDCARD (receive_type))
1354 receive_type = NULL;
1356 message_type = DBUS_MESSAGE_TYPE_INVALID;
1357 if (receive_type != NULL)
1359 message_type = dbus_message_type_from_string (receive_type);
1360 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1362 dbus_set_error (error, DBUS_ERROR_FAILED,
1363 "Bad message type \"%s\"",
1371 !(strcmp (eavesdrop, "true") == 0 ||
1372 strcmp (eavesdrop, "false") == 0))
1374 dbus_set_error (error, DBUS_ERROR_FAILED,
1375 "Bad value \"%s\" for %s attribute, must be true or false",
1376 "eavesdrop", eavesdrop);
1380 if (receive_requested_reply &&
1381 !(strcmp (receive_requested_reply, "true") == 0 ||
1382 strcmp (receive_requested_reply, "false") == 0))
1384 dbus_set_error (error, DBUS_ERROR_FAILED,
1385 "Bad value \"%s\" for %s attribute, must be true or false",
1386 "receive_requested_reply", receive_requested_reply);
1390 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1395 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1397 if (receive_requested_reply)
1398 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1400 rule->d.receive.message_type = message_type;
1401 rule->d.receive.path = _dbus_strdup (receive_path);
1402 rule->d.receive.interface = _dbus_strdup (receive_interface);
1403 rule->d.receive.member = _dbus_strdup (receive_member);
1404 rule->d.receive.error = _dbus_strdup (receive_error);
1405 rule->d.receive.origin = _dbus_strdup (receive_sender);
1407 if (receive_path && rule->d.receive.path == NULL)
1409 if (receive_interface && rule->d.receive.interface == NULL)
1411 if (receive_member && rule->d.receive.member == NULL)
1413 if (receive_error && rule->d.receive.error == NULL)
1415 if (receive_sender && rule->d.receive.origin == NULL)
1420 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1424 if (IS_WILDCARD (own))
1427 rule->d.own.service_name = _dbus_strdup (own);
1428 if (own && rule->d.own.service_name == NULL)
1433 if (IS_WILDCARD (user))
1435 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1439 rule->d.user.uid = DBUS_UID_UNSET;
1443 DBusString username;
1446 _dbus_string_init_const (&username, user);
1448 if (_dbus_get_user_id (&username, &uid))
1450 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1454 rule->d.user.uid = uid;
1458 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1459 user, element_name);
1465 if (IS_WILDCARD (group))
1467 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1471 rule->d.group.gid = DBUS_GID_UNSET;
1475 DBusString groupname;
1478 _dbus_string_init_const (&groupname, group);
1480 if (_dbus_get_user_id (&groupname, &gid))
1482 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1486 rule->d.group.gid = gid;
1490 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1491 group, element_name);
1496 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1502 pe = peek_element (parser);
1503 _dbus_assert (pe != NULL);
1504 _dbus_assert (pe->type == ELEMENT_POLICY);
1506 switch (pe->d.policy.type)
1508 case POLICY_IGNORED:
1509 /* drop the rule on the floor */
1512 case POLICY_DEFAULT:
1513 if (!bus_policy_append_default_rule (parser->policy, rule))
1516 case POLICY_MANDATORY:
1517 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1521 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1523 dbus_set_error (error, DBUS_ERROR_FAILED,
1524 "<%s> rule cannot be per-user because it has bus-global semantics",
1529 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1534 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1536 dbus_set_error (error, DBUS_ERROR_FAILED,
1537 "<%s> rule cannot be per-group because it has bus-global semantics",
1542 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1548 case POLICY_CONSOLE:
1549 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1555 bus_policy_rule_unref (rule);
1562 BUS_SET_OOM (error);
1565 bus_policy_rule_unref (rule);
1570 start_policy_child (BusConfigParser *parser,
1571 const char *element_name,
1572 const char **attribute_names,
1573 const char **attribute_values,
1576 if (strcmp (element_name, "allow") == 0)
1578 if (!append_rule_from_element (parser, element_name,
1579 attribute_names, attribute_values,
1583 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1585 BUS_SET_OOM (error);
1591 else if (strcmp (element_name, "deny") == 0)
1593 if (!append_rule_from_element (parser, element_name,
1594 attribute_names, attribute_values,
1598 if (push_element (parser, ELEMENT_DENY) == NULL)
1600 BUS_SET_OOM (error);
1608 dbus_set_error (error, DBUS_ERROR_FAILED,
1609 "Element <%s> not allowed inside <%s> in configuration file",
1610 element_name, "policy");
1616 start_selinux_child (BusConfigParser *parser,
1617 const char *element_name,
1618 const char **attribute_names,
1619 const char **attribute_values,
1626 context_copy = NULL;
1628 if (strcmp (element_name, "associate") == 0)
1631 const char *context;
1633 if (!locate_attributes (parser, "associate",
1638 "context", &context,
1642 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1644 BUS_SET_OOM (error);
1648 if (own == NULL || context == NULL)
1650 dbus_set_error (error, DBUS_ERROR_FAILED,
1651 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1655 own_copy = _dbus_strdup (own);
1656 if (own_copy == NULL)
1658 context_copy = _dbus_strdup (context);
1659 if (context_copy == NULL)
1662 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1663 own_copy, context_copy))
1670 dbus_set_error (error, DBUS_ERROR_FAILED,
1671 "Element <%s> not allowed inside <%s> in configuration file",
1672 element_name, "selinux");
1678 dbus_free (own_copy);
1681 dbus_free (context_copy);
1683 BUS_SET_OOM (error);
1688 bus_config_parser_start_element (BusConfigParser *parser,
1689 const char *element_name,
1690 const char **attribute_names,
1691 const char **attribute_values,
1696 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1698 /* printf ("START: %s\n", element_name); */
1700 t = top_element_type (parser);
1702 if (t == ELEMENT_NONE)
1704 if (strcmp (element_name, "busconfig") == 0)
1706 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1709 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1711 BUS_SET_OOM (error);
1719 dbus_set_error (error, DBUS_ERROR_FAILED,
1720 "Unknown element <%s> at root of configuration file",
1725 else if (t == ELEMENT_BUSCONFIG)
1727 return start_busconfig_child (parser, element_name,
1728 attribute_names, attribute_values,
1731 else if (t == ELEMENT_POLICY)
1733 return start_policy_child (parser, element_name,
1734 attribute_names, attribute_values,
1737 else if (t == ELEMENT_SELINUX)
1739 return start_selinux_child (parser, element_name,
1740 attribute_names, attribute_values,
1745 dbus_set_error (error, DBUS_ERROR_FAILED,
1746 "Element <%s> is not allowed in this context",
1753 set_limit (BusConfigParser *parser,
1758 dbus_bool_t must_be_positive;
1759 dbus_bool_t must_be_int;
1761 must_be_int = FALSE;
1762 must_be_positive = FALSE;
1764 if (strcmp (name, "max_incoming_bytes") == 0)
1766 must_be_positive = TRUE;
1767 parser->limits.max_incoming_bytes = value;
1769 else if (strcmp (name, "max_outgoing_bytes") == 0)
1771 must_be_positive = TRUE;
1772 parser->limits.max_outgoing_bytes = value;
1774 else if (strcmp (name, "max_message_size") == 0)
1776 must_be_positive = TRUE;
1777 parser->limits.max_message_size = value;
1779 else if (strcmp (name, "service_start_timeout") == 0)
1781 must_be_positive = TRUE;
1783 parser->limits.activation_timeout = value;
1785 else if (strcmp (name, "auth_timeout") == 0)
1787 must_be_positive = TRUE;
1789 parser->limits.auth_timeout = value;
1791 else if (strcmp (name, "reply_timeout") == 0)
1793 must_be_positive = TRUE;
1795 parser->limits.reply_timeout = value;
1797 else if (strcmp (name, "max_completed_connections") == 0)
1799 must_be_positive = TRUE;
1801 parser->limits.max_completed_connections = value;
1803 else if (strcmp (name, "max_incomplete_connections") == 0)
1805 must_be_positive = TRUE;
1807 parser->limits.max_incomplete_connections = value;
1809 else if (strcmp (name, "max_connections_per_user") == 0)
1811 must_be_positive = TRUE;
1813 parser->limits.max_connections_per_user = value;
1815 else if (strcmp (name, "max_pending_service_starts") == 0)
1817 must_be_positive = TRUE;
1819 parser->limits.max_pending_activations = value;
1821 else if (strcmp (name, "max_names_per_connection") == 0)
1823 must_be_positive = TRUE;
1825 parser->limits.max_services_per_connection = value;
1827 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1829 must_be_positive = TRUE;
1831 parser->limits.max_match_rules_per_connection = value;
1833 else if (strcmp (name, "max_replies_per_connection") == 0)
1835 must_be_positive = TRUE;
1837 parser->limits.max_replies_per_connection = value;
1841 dbus_set_error (error, DBUS_ERROR_FAILED,
1842 "There is no limit called \"%s\"\n",
1847 if (must_be_positive && value < 0)
1849 dbus_set_error (error, DBUS_ERROR_FAILED,
1850 "<limit name=\"%s\"> must be a positive number\n",
1856 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1858 dbus_set_error (error, DBUS_ERROR_FAILED,
1859 "<limit name=\"%s\"> value is too large\n",
1868 bus_config_parser_end_element (BusConfigParser *parser,
1869 const char *element_name,
1876 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1878 /* printf ("END: %s\n", element_name); */
1880 t = top_element_type (parser);
1882 if (t == ELEMENT_NONE)
1884 /* should probably be an assertion failure but
1885 * being paranoid about XML parsers
1887 dbus_set_error (error, DBUS_ERROR_FAILED,
1888 "XML parser ended element with no element on the stack");
1892 n = element_type_to_name (t);
1893 _dbus_assert (n != NULL);
1894 if (strcmp (n, element_name) != 0)
1896 /* should probably be an assertion failure but
1897 * being paranoid about XML parsers
1899 dbus_set_error (error, DBUS_ERROR_FAILED,
1900 "XML element <%s> ended but topmost element on the stack was <%s>",
1905 e = peek_element (parser);
1906 _dbus_assert (e != NULL);
1911 _dbus_assert_not_reached ("element in stack has no type");
1914 case ELEMENT_INCLUDE:
1917 case ELEMENT_LISTEN:
1918 case ELEMENT_PIDFILE:
1920 case ELEMENT_SERVICEDIR:
1921 case ELEMENT_INCLUDEDIR:
1923 if (!e->had_content)
1925 dbus_set_error (error, DBUS_ERROR_FAILED,
1926 "XML element <%s> was expected to have content inside it",
1927 element_type_to_name (e->type));
1931 if (e->type == ELEMENT_LIMIT)
1933 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1939 case ELEMENT_BUSCONFIG:
1940 case ELEMENT_POLICY:
1944 case ELEMENT_SELINUX:
1945 case ELEMENT_ASSOCIATE:
1946 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
1950 pop_element (parser);
1956 all_whitespace (const DBusString *str)
1960 _dbus_string_skip_white (str, 0, &i);
1962 return i == _dbus_string_get_length (str);
1966 make_full_path (const DBusString *basedir,
1967 const DBusString *filename,
1968 DBusString *full_path)
1970 if (_dbus_path_is_absolute (filename))
1972 return _dbus_string_copy (filename, 0, full_path, 0);
1976 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1979 if (!_dbus_concat_dir_and_file (full_path, filename))
1987 include_file (BusConfigParser *parser,
1988 const DBusString *filename,
1989 dbus_bool_t ignore_missing,
1992 /* FIXME good test case for this would load each config file in the
1993 * test suite both alone, and as an include, and check
1994 * that the result is the same
1996 BusConfigParser *included;
1997 const char *filename_str;
1998 DBusError tmp_error;
2000 dbus_error_init (&tmp_error);
2002 filename_str = _dbus_string_get_const_data (filename);
2004 /* Check to make sure this file hasn't already been included. */
2005 if (seen_include (parser, filename))
2007 dbus_set_error (error, DBUS_ERROR_FAILED,
2008 "Circular inclusion of file '%s'",
2013 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2015 BUS_SET_OOM (error);
2019 /* Since parser is passed in as the parent, included
2020 inherits parser's limits. */
2021 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2023 _dbus_list_pop_last (&parser->included_files);
2025 if (included == NULL)
2027 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2029 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2032 dbus_error_free (&tmp_error);
2037 dbus_move_error (&tmp_error, error);
2043 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2045 if (!merge_included (parser, included, error))
2047 bus_config_parser_unref (included);
2051 /* Copy included's limits back to parser. */
2052 parser->limits = included->limits;
2054 bus_config_parser_unref (included);
2060 include_dir (BusConfigParser *parser,
2061 const DBusString *dirname,
2064 DBusString filename;
2066 DBusError tmp_error;
2070 if (!_dbus_string_init (&filename))
2072 BUS_SET_OOM (error);
2078 dir = _dbus_directory_open (dirname, error);
2083 dbus_error_init (&tmp_error);
2084 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2086 DBusString full_path;
2088 if (!_dbus_string_init (&full_path))
2090 BUS_SET_OOM (error);
2094 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2096 BUS_SET_OOM (error);
2097 _dbus_string_free (&full_path);
2101 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2103 BUS_SET_OOM (error);
2104 _dbus_string_free (&full_path);
2108 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2110 if (!include_file (parser, &full_path, TRUE, error))
2112 _dbus_string_free (&full_path);
2117 _dbus_string_free (&full_path);
2120 if (dbus_error_is_set (&tmp_error))
2122 dbus_move_error (&tmp_error, error);
2127 if (!_dbus_string_copy_data (dirname, &s))
2129 BUS_SET_OOM (error);
2133 if (!_dbus_list_append (&parser->conf_dirs, s))
2136 BUS_SET_OOM (error);
2143 _dbus_string_free (&filename);
2146 _dbus_directory_close (dir);
2152 bus_config_parser_content (BusConfigParser *parser,
2153 const DBusString *content,
2158 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2164 _dbus_string_get_const_data (content, &c_str);
2166 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2170 e = peek_element (parser);
2173 dbus_set_error (error, DBUS_ERROR_FAILED,
2174 "Text content outside of any XML element in configuration file");
2177 else if (e->had_content)
2179 _dbus_assert_not_reached ("Element had multiple content blocks");
2183 switch (top_element_type (parser))
2186 _dbus_assert_not_reached ("element at top of stack has no type");
2189 case ELEMENT_BUSCONFIG:
2190 case ELEMENT_POLICY:
2194 case ELEMENT_SELINUX:
2195 case ELEMENT_ASSOCIATE:
2196 if (all_whitespace (content))
2200 dbus_set_error (error, DBUS_ERROR_FAILED,
2201 "No text content expected inside XML element %s in configuration file",
2202 element_type_to_name (top_element_type (parser)));
2206 case ELEMENT_PIDFILE:
2210 e->had_content = TRUE;
2212 if (!_dbus_string_copy_data (content, &s))
2215 dbus_free (parser->pidfile);
2216 parser->pidfile = s;
2220 case ELEMENT_INCLUDE:
2222 DBusString full_path, selinux_policy_root;
2224 e->had_content = TRUE;
2226 if (e->d.include.if_selinux_enabled
2227 && !bus_selinux_enabled ())
2230 if (!_dbus_string_init (&full_path))
2233 if (e->d.include.selinux_root_relative)
2235 if (!bus_selinux_get_policy_root ())
2237 dbus_set_error (error, DBUS_ERROR_FAILED,
2238 "Could not determine SELinux policy root for relative inclusion");
2239 _dbus_string_free (&full_path);
2242 _dbus_string_init_const (&selinux_policy_root,
2243 bus_selinux_get_policy_root ());
2244 if (!make_full_path (&selinux_policy_root, content, &full_path))
2246 _dbus_string_free (&full_path);
2250 else if (!make_full_path (&parser->basedir, content, &full_path))
2252 _dbus_string_free (&full_path);
2256 if (!include_file (parser, &full_path,
2257 e->d.include.ignore_missing, error))
2259 _dbus_string_free (&full_path);
2263 _dbus_string_free (&full_path);
2267 case ELEMENT_INCLUDEDIR:
2269 DBusString full_path;
2271 e->had_content = TRUE;
2273 if (!_dbus_string_init (&full_path))
2276 if (!make_full_path (&parser->basedir, content, &full_path))
2278 _dbus_string_free (&full_path);
2282 if (!include_dir (parser, &full_path, error))
2284 _dbus_string_free (&full_path);
2288 _dbus_string_free (&full_path);
2296 e->had_content = TRUE;
2298 if (!_dbus_string_copy_data (content, &s))
2301 dbus_free (parser->user);
2310 e->had_content = TRUE;
2312 if (!_dbus_string_copy_data (content, &s))
2315 dbus_free (parser->bus_type);
2316 parser->bus_type = s;
2320 case ELEMENT_LISTEN:
2324 e->had_content = TRUE;
2326 if (!_dbus_string_copy_data (content, &s))
2329 if (!_dbus_list_append (&parser->listen_on,
2342 e->had_content = TRUE;
2344 if (!_dbus_string_copy_data (content, &s))
2347 if (!_dbus_list_append (&parser->mechanisms,
2355 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2361 if (!_dbus_get_standard_session_servicedirs (&dirs))
2364 while ((link = _dbus_list_pop_first_link (&dirs)))
2365 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
2369 case ELEMENT_SERVICEDIR:
2372 DBusString full_path;
2374 e->had_content = TRUE;
2376 if (!_dbus_string_init (&full_path))
2379 if (!make_full_path (&parser->basedir, content, &full_path))
2381 _dbus_string_free (&full_path);
2385 if (!_dbus_string_copy_data (&full_path, &s))
2387 _dbus_string_free (&full_path);
2391 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2393 _dbus_string_free (&full_path);
2398 _dbus_string_free (&full_path);
2406 e->had_content = TRUE;
2409 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2411 dbus_set_error (error, DBUS_ERROR_FAILED,
2412 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2417 e->d.limit.value = val;
2419 _dbus_verbose ("Loaded value %ld for limit %s\n",
2426 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2430 BUS_SET_OOM (error);
2435 bus_config_parser_finished (BusConfigParser *parser,
2438 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2440 if (parser->stack != NULL)
2442 dbus_set_error (error, DBUS_ERROR_FAILED,
2443 "Element <%s> was not closed in configuration file",
2444 element_type_to_name (top_element_type (parser)));
2449 if (parser->is_toplevel && parser->listen_on == NULL)
2451 dbus_set_error (error, DBUS_ERROR_FAILED,
2452 "Configuration file needs one or more <listen> elements giving addresses");
2460 bus_config_parser_get_user (BusConfigParser *parser)
2462 return parser->user;
2466 bus_config_parser_get_type (BusConfigParser *parser)
2468 return parser->bus_type;
2472 bus_config_parser_get_addresses (BusConfigParser *parser)
2474 return &parser->listen_on;
2478 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2480 return &parser->mechanisms;
2484 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2486 return &parser->service_dirs;
2490 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2492 return &parser->conf_dirs;
2496 bus_config_parser_get_fork (BusConfigParser *parser)
2498 return parser->fork;
2502 bus_config_parser_get_pidfile (BusConfigParser *parser)
2504 return parser->pidfile;
2508 bus_config_parser_steal_policy (BusConfigParser *parser)
2512 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2514 policy = parser->policy;
2516 parser->policy = NULL;
2521 /* Overwrite any limits that were set in the configuration file */
2523 bus_config_parser_get_limits (BusConfigParser *parser,
2526 *limits = parser->limits;
2530 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2532 DBusHashTable *table;
2534 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2536 table = parser->service_context_table;
2538 parser->service_context_table = NULL;
2543 #ifdef DBUS_BUILD_TESTS
2554 do_load (const DBusString *full_path,
2556 dbus_bool_t oom_possible)
2558 BusConfigParser *parser;
2561 dbus_error_init (&error);
2563 parser = bus_config_load (full_path, TRUE, NULL, &error);
2566 _DBUS_ASSERT_ERROR_IS_SET (&error);
2569 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2571 _dbus_verbose ("Failed to load valid file due to OOM\n");
2572 dbus_error_free (&error);
2575 else if (validity == VALID)
2577 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2580 dbus_error_free (&error);
2585 dbus_error_free (&error);
2591 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2593 bus_config_parser_unref (parser);
2595 if (validity == INVALID)
2597 _dbus_warn ("Accepted invalid file\n");
2607 const DBusString *full_path;
2612 check_loader_oom_func (void *data)
2614 LoaderOomData *d = data;
2616 return do_load (d->full_path, d->validity, TRUE);
2620 process_test_valid_subdir (const DBusString *test_base_dir,
2624 DBusString test_directory;
2625 DBusString filename;
2633 if (!_dbus_string_init (&test_directory))
2634 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2636 _dbus_string_init_const (&filename, subdir);
2638 if (!_dbus_string_copy (test_base_dir, 0,
2639 &test_directory, 0))
2640 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2642 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2643 _dbus_assert_not_reached ("couldn't allocate full path");
2645 _dbus_string_free (&filename);
2646 if (!_dbus_string_init (&filename))
2647 _dbus_assert_not_reached ("didn't allocate filename string\n");
2649 dbus_error_init (&error);
2650 dir = _dbus_directory_open (&test_directory, &error);
2653 _dbus_warn ("Could not open %s: %s\n",
2654 _dbus_string_get_const_data (&test_directory),
2656 dbus_error_free (&error);
2660 if (validity == VALID)
2661 printf ("Testing valid files:\n");
2662 else if (validity == INVALID)
2663 printf ("Testing invalid files:\n");
2665 printf ("Testing unknown files:\n");
2668 while (_dbus_directory_get_next_file (dir, &filename, &error))
2670 DBusString full_path;
2673 if (!_dbus_string_init (&full_path))
2674 _dbus_assert_not_reached ("couldn't init string");
2676 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2677 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2679 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2680 _dbus_assert_not_reached ("couldn't concat file to dir");
2682 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2684 _dbus_verbose ("Skipping non-.conf file %s\n",
2685 _dbus_string_get_const_data (&filename));
2686 _dbus_string_free (&full_path);
2690 printf (" %s\n", _dbus_string_get_const_data (&filename));
2692 _dbus_verbose (" expecting %s\n",
2693 validity == VALID ? "valid" :
2694 (validity == INVALID ? "invalid" :
2695 (validity == UNKNOWN ? "unknown" : "???")));
2697 d.full_path = &full_path;
2698 d.validity = validity;
2700 /* FIXME hackaround for an expat problem, see
2701 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2702 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2704 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2705 if (!check_loader_oom_func (&d))
2706 _dbus_assert_not_reached ("test failed");
2708 _dbus_string_free (&full_path);
2711 if (dbus_error_is_set (&error))
2713 _dbus_warn ("Could not get next file in %s: %s\n",
2714 _dbus_string_get_const_data (&test_directory),
2716 dbus_error_free (&error);
2725 _dbus_directory_close (dir);
2726 _dbus_string_free (&test_directory);
2727 _dbus_string_free (&filename);
2733 bools_equal (dbus_bool_t a,
2740 strings_equal_or_both_null (const char *a,
2743 if (a == NULL || b == NULL)
2746 return !strcmp (a, b);
2750 elements_equal (const Element *a,
2753 if (a->type != b->type)
2756 if (!bools_equal (a->had_content, b->had_content))
2762 case ELEMENT_INCLUDE:
2763 if (!bools_equal (a->d.include.ignore_missing,
2764 b->d.include.ignore_missing))
2768 case ELEMENT_POLICY:
2769 if (a->d.policy.type != b->d.policy.type)
2771 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
2776 if (strcmp (a->d.limit.name, b->d.limit.name))
2778 if (a->d.limit.value != b->d.limit.value)
2792 lists_of_elements_equal (DBusList *a,
2801 while (ia != NULL && ib != NULL)
2803 if (elements_equal (ia->data, ib->data))
2805 ia = _dbus_list_get_next_link (&a, ia);
2806 ib = _dbus_list_get_next_link (&b, ib);
2809 return ia == NULL && ib == NULL;
2813 lists_of_c_strings_equal (DBusList *a,
2822 while (ia != NULL && ib != NULL)
2824 if (strcmp (ia->data, ib->data))
2826 ia = _dbus_list_get_next_link (&a, ia);
2827 ib = _dbus_list_get_next_link (&b, ib);
2830 return ia == NULL && ib == NULL;
2834 limits_equal (const BusLimits *a,
2838 (a->max_incoming_bytes == b->max_incoming_bytes
2839 || a->max_outgoing_bytes == b->max_outgoing_bytes
2840 || a->max_message_size == b->max_message_size
2841 || a->activation_timeout == b->activation_timeout
2842 || a->auth_timeout == b->auth_timeout
2843 || a->max_completed_connections == b->max_completed_connections
2844 || a->max_incomplete_connections == b->max_incomplete_connections
2845 || a->max_connections_per_user == b->max_connections_per_user
2846 || a->max_pending_activations == b->max_pending_activations
2847 || a->max_services_per_connection == b->max_services_per_connection
2848 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2849 || a->max_replies_per_connection == b->max_replies_per_connection
2850 || a->reply_timeout == b->reply_timeout);
2854 config_parsers_equal (const BusConfigParser *a,
2855 const BusConfigParser *b)
2857 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2860 if (!lists_of_elements_equal (a->stack, b->stack))
2863 if (!strings_equal_or_both_null (a->user, b->user))
2866 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2869 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2872 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2875 /* FIXME: compare policy */
2877 /* FIXME: compare service selinux ID table */
2879 if (! limits_equal (&a->limits, &b->limits))
2882 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2885 if (! bools_equal (a->fork, b->fork))
2888 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2895 all_are_equiv (const DBusString *target_directory)
2897 DBusString filename;
2899 BusConfigParser *first_parser;
2900 BusConfigParser *parser;
2906 first_parser = NULL;
2910 if (!_dbus_string_init (&filename))
2911 _dbus_assert_not_reached ("didn't allocate filename string");
2913 dbus_error_init (&error);
2914 dir = _dbus_directory_open (target_directory, &error);
2917 _dbus_warn ("Could not open %s: %s\n",
2918 _dbus_string_get_const_data (target_directory),
2920 dbus_error_free (&error);
2924 printf ("Comparing equivalent files:\n");
2927 while (_dbus_directory_get_next_file (dir, &filename, &error))
2929 DBusString full_path;
2931 if (!_dbus_string_init (&full_path))
2932 _dbus_assert_not_reached ("couldn't init string");
2934 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2935 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2937 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2938 _dbus_assert_not_reached ("couldn't concat file to dir");
2940 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2942 _dbus_verbose ("Skipping non-.conf file %s\n",
2943 _dbus_string_get_const_data (&filename));
2944 _dbus_string_free (&full_path);
2948 printf (" %s\n", _dbus_string_get_const_data (&filename));
2950 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2954 _dbus_warn ("Could not load file %s: %s\n",
2955 _dbus_string_get_const_data (&full_path),
2957 _dbus_string_free (&full_path);
2958 dbus_error_free (&error);
2961 else if (first_parser == NULL)
2963 _dbus_string_free (&full_path);
2964 first_parser = parser;
2968 _dbus_string_free (&full_path);
2969 equal = config_parsers_equal (first_parser, parser);
2970 bus_config_parser_unref (parser);
2979 _dbus_string_free (&filename);
2981 bus_config_parser_unref (first_parser);
2983 _dbus_directory_close (dir);
2990 process_test_equiv_subdir (const DBusString *test_base_dir,
2993 DBusString test_directory;
2994 DBusString filename;
3003 if (!_dbus_string_init (&test_directory))
3004 _dbus_assert_not_reached ("didn't allocate test_directory");
3006 _dbus_string_init_const (&filename, subdir);
3008 if (!_dbus_string_copy (test_base_dir, 0,
3009 &test_directory, 0))
3010 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3012 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3013 _dbus_assert_not_reached ("couldn't allocate full path");
3015 _dbus_string_free (&filename);
3016 if (!_dbus_string_init (&filename))
3017 _dbus_assert_not_reached ("didn't allocate filename string");
3019 dbus_error_init (&error);
3020 dir = _dbus_directory_open (&test_directory, &error);
3023 _dbus_warn ("Could not open %s: %s\n",
3024 _dbus_string_get_const_data (&test_directory),
3026 dbus_error_free (&error);
3030 while (_dbus_directory_get_next_file (dir, &filename, &error))
3032 DBusString full_path;
3034 /* Skip CVS's magic directories! */
3035 if (_dbus_string_equal_c_str (&filename, "CVS"))
3038 if (!_dbus_string_init (&full_path))
3039 _dbus_assert_not_reached ("couldn't init string");
3041 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3042 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3044 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3045 _dbus_assert_not_reached ("couldn't concat file to dir");
3047 equal = all_are_equiv (&full_path);
3048 _dbus_string_free (&full_path);
3057 _dbus_string_free (&test_directory);
3058 _dbus_string_free (&filename);
3060 _dbus_directory_close (dir);
3066 static const char *test_service_dir_matches[] =
3068 "/testusr/testlocal/testshare/dbus-1/services",
3069 "/testusr/testshare/dbus-1/services",
3070 DBUS_DATADIR"/dbus-1/services",
3071 "/testhome/foo/.testlocal/testshare/dbus-1/services",
3076 test_default_session_servicedirs (void)
3084 printf ("Testing retriving the default session service directories\n");
3085 if (!_dbus_get_standard_session_servicedirs (&dirs))
3086 _dbus_assert_not_reached ("couldn't get stardard dirs");
3088 /* make sure our defaults end with share/dbus-1/service */
3089 while ((link = _dbus_list_pop_first_link (&dirs)))
3093 printf (" default service dir: %s\n", (char *)link->data);
3094 _dbus_string_init_const (&path, (char *)link->data);
3095 if (!_dbus_string_ends_with_c_str (&path, "share/dbus-1/services"))
3097 printf ("error with default session service directories\n");
3101 dbus_free (link->data);
3102 _dbus_list_free_link (link);
3105 if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3106 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3108 if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3109 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3111 if (!_dbus_get_standard_session_servicedirs (&dirs))
3112 _dbus_assert_not_reached ("couldn't get stardard dirs");
3114 /* make sure we read and parse the env variable correctly */
3116 while ((link = _dbus_list_pop_first_link (&dirs)))
3118 printf (" test service dir: %s\n", (char *)link->data);
3119 if (test_service_dir_matches[i] == NULL)
3121 printf ("more directories parsed than in match set\n");
3125 if (strcmp (test_service_dir_matches[i],
3126 (char *)link->data) != 0)
3128 printf ("%s directory does not match %s in the match set\n",
3130 test_service_dir_matches[i]);
3136 dbus_free (link->data);
3137 _dbus_list_free_link (link);
3140 if (test_service_dir_matches[i] != NULL)
3142 printf ("extra data %s in the match set was not matched\n",
3143 test_service_dir_matches[i]);
3152 bus_config_parser_test (const DBusString *test_data_dir)
3154 if (test_data_dir == NULL ||
3155 _dbus_string_get_length (test_data_dir) == 0)
3157 printf ("No test data\n");
3161 if (!test_default_session_servicedirs())
3164 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3167 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3170 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3176 #endif /* DBUS_BUILD_TESTS */