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 * 127;
464 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 127;
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 = 64;
480 parser->limits.max_connections_per_user = 256;
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 = 2048;
488 parser->limits.max_pending_activations = 512;
489 parser->limits.max_services_per_connection = 512;
491 parser->limits.max_match_rules_per_connection = 512;
493 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
495 /* this is effectively a limit on message queue size for messages
496 * that require a reply
498 parser->limits.max_replies_per_connection = 1024*8;
501 parser->refcount = 1;
507 bus_config_parser_ref (BusConfigParser *parser)
509 _dbus_assert (parser->refcount > 0);
511 parser->refcount += 1;
517 bus_config_parser_unref (BusConfigParser *parser)
519 _dbus_assert (parser->refcount > 0);
521 parser->refcount -= 1;
523 if (parser->refcount == 0)
525 while (parser->stack != NULL)
526 pop_element (parser);
528 dbus_free (parser->user);
529 dbus_free (parser->bus_type);
530 dbus_free (parser->pidfile);
532 _dbus_list_foreach (&parser->listen_on,
533 (DBusForeachFunction) dbus_free,
536 _dbus_list_clear (&parser->listen_on);
538 _dbus_list_foreach (&parser->service_dirs,
539 (DBusForeachFunction) dbus_free,
542 _dbus_list_clear (&parser->service_dirs);
544 _dbus_list_foreach (&parser->conf_dirs,
545 (DBusForeachFunction) dbus_free,
548 _dbus_list_clear (&parser->conf_dirs);
550 _dbus_list_foreach (&parser->mechanisms,
551 (DBusForeachFunction) dbus_free,
554 _dbus_list_clear (&parser->mechanisms);
556 _dbus_string_free (&parser->basedir);
559 bus_policy_unref (parser->policy);
561 if (parser->service_context_table)
562 _dbus_hash_table_unref (parser->service_context_table);
569 bus_config_parser_check_doctype (BusConfigParser *parser,
573 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
575 if (strcmp (doctype, "busconfig") != 0)
577 dbus_set_error (error,
579 "Configuration file has the wrong document type %s",
594 locate_attributes (BusConfigParser *parser,
595 const char *element_name,
596 const char **attribute_names,
597 const char **attribute_values,
599 const char *first_attribute_name,
600 const char **first_attribute_retloc,
608 LocateAttr attrs[MAX_ATTRS];
612 _dbus_assert (first_attribute_name != NULL);
613 _dbus_assert (first_attribute_retloc != NULL);
618 attrs[0].name = first_attribute_name;
619 attrs[0].retloc = first_attribute_retloc;
620 *first_attribute_retloc = NULL;
622 va_start (args, first_attribute_retloc);
624 name = va_arg (args, const char*);
625 retloc = va_arg (args, const char**);
629 _dbus_assert (retloc != NULL);
630 _dbus_assert (n_attrs < MAX_ATTRS);
632 attrs[n_attrs].name = name;
633 attrs[n_attrs].retloc = retloc;
637 name = va_arg (args, const char*);
638 retloc = va_arg (args, const char**);
647 while (attribute_names[i])
656 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
658 retloc = attrs[j].retloc;
662 dbus_set_error (error, DBUS_ERROR_FAILED,
663 "Attribute \"%s\" repeated twice on the same <%s> element",
664 attrs[j].name, element_name);
669 *retloc = attribute_values[i];
678 dbus_set_error (error, DBUS_ERROR_FAILED,
679 "Attribute \"%s\" is invalid on <%s> element in this context",
680 attribute_names[i], element_name);
693 check_no_attributes (BusConfigParser *parser,
694 const char *element_name,
695 const char **attribute_names,
696 const char **attribute_values,
699 if (attribute_names[0] != NULL)
701 dbus_set_error (error, DBUS_ERROR_FAILED,
702 "Attribute \"%s\" is invalid on <%s> element in this context",
703 attribute_names[0], element_name);
711 start_busconfig_child (BusConfigParser *parser,
712 const char *element_name,
713 const char **attribute_names,
714 const char **attribute_values,
717 if (strcmp (element_name, "user") == 0)
719 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
722 if (push_element (parser, ELEMENT_USER) == NULL)
730 else if (strcmp (element_name, "type") == 0)
732 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
735 if (push_element (parser, ELEMENT_TYPE) == NULL)
743 else if (strcmp (element_name, "fork") == 0)
745 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
748 if (push_element (parser, ELEMENT_FORK) == NULL)
758 else if (strcmp (element_name, "pidfile") == 0)
760 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
763 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
771 else if (strcmp (element_name, "listen") == 0)
773 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
776 if (push_element (parser, ELEMENT_LISTEN) == NULL)
784 else if (strcmp (element_name, "auth") == 0)
786 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
789 if (push_element (parser, ELEMENT_AUTH) == NULL)
797 else if (strcmp (element_name, "includedir") == 0)
799 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
802 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
810 else if (strcmp (element_name, "standard_session_servicedirs") == 0)
816 if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
819 if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
825 if (!_dbus_get_standard_session_servicedirs (&dirs))
831 while ((link = _dbus_list_pop_first_link (&dirs)))
832 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
836 else if (strcmp (element_name, "servicedir") == 0)
838 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
841 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
849 else if (strcmp (element_name, "include") == 0)
852 const char *if_selinux_enabled;
853 const char *ignore_missing;
854 const char *selinux_root_relative;
856 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
862 e->d.include.ignore_missing = FALSE;
863 e->d.include.if_selinux_enabled = FALSE;
864 e->d.include.selinux_root_relative = FALSE;
866 if (!locate_attributes (parser, "include",
870 "ignore_missing", &ignore_missing,
871 "if_selinux_enabled", &if_selinux_enabled,
872 "selinux_root_relative", &selinux_root_relative,
876 if (ignore_missing != NULL)
878 if (strcmp (ignore_missing, "yes") == 0)
879 e->d.include.ignore_missing = TRUE;
880 else if (strcmp (ignore_missing, "no") == 0)
881 e->d.include.ignore_missing = FALSE;
884 dbus_set_error (error, DBUS_ERROR_FAILED,
885 "ignore_missing attribute must have value \"yes\" or \"no\"");
890 if (if_selinux_enabled != NULL)
892 if (strcmp (if_selinux_enabled, "yes") == 0)
893 e->d.include.if_selinux_enabled = TRUE;
894 else if (strcmp (if_selinux_enabled, "no") == 0)
895 e->d.include.if_selinux_enabled = FALSE;
898 dbus_set_error (error, DBUS_ERROR_FAILED,
899 "if_selinux_enabled attribute must have value"
900 " \"yes\" or \"no\"");
905 if (selinux_root_relative != NULL)
907 if (strcmp (selinux_root_relative, "yes") == 0)
908 e->d.include.selinux_root_relative = TRUE;
909 else if (strcmp (selinux_root_relative, "no") == 0)
910 e->d.include.selinux_root_relative = FALSE;
913 dbus_set_error (error, DBUS_ERROR_FAILED,
914 "selinux_root_relative attribute must have value"
915 " \"yes\" or \"no\"");
922 else if (strcmp (element_name, "policy") == 0)
928 const char *at_console;
930 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
936 e->d.policy.type = POLICY_IGNORED;
938 if (!locate_attributes (parser, "policy",
945 "at_console", &at_console,
949 if (((context && user) ||
950 (context && group) ||
951 (context && at_console)) ||
953 (user && at_console)) ||
954 (group && at_console) ||
955 !(context || user || group || at_console))
957 dbus_set_error (error, DBUS_ERROR_FAILED,
958 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
964 if (strcmp (context, "default") == 0)
966 e->d.policy.type = POLICY_DEFAULT;
968 else if (strcmp (context, "mandatory") == 0)
970 e->d.policy.type = POLICY_MANDATORY;
974 dbus_set_error (error, DBUS_ERROR_FAILED,
975 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
980 else if (user != NULL)
983 _dbus_string_init_const (&username, user);
985 if (_dbus_parse_unix_user_from_config (&username,
986 &e->d.policy.gid_uid_or_at_console))
987 e->d.policy.type = POLICY_USER;
989 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
992 else if (group != NULL)
994 DBusString group_name;
995 _dbus_string_init_const (&group_name, group);
997 if (_dbus_parse_unix_group_from_config (&group_name,
998 &e->d.policy.gid_uid_or_at_console))
999 e->d.policy.type = POLICY_GROUP;
1001 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
1004 else if (at_console != NULL)
1007 t = (strcmp (at_console, "true") == 0);
1008 if (t || strcmp (at_console, "false") == 0)
1010 e->d.policy.gid_uid_or_at_console = t;
1011 e->d.policy.type = POLICY_CONSOLE;
1015 dbus_set_error (error, DBUS_ERROR_FAILED,
1016 "Unknown value \"%s\" for at_console in message bus configuration file",
1024 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
1029 else if (strcmp (element_name, "limit") == 0)
1034 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1036 BUS_SET_OOM (error);
1040 if (!locate_attributes (parser, "limit",
1050 dbus_set_error (error, DBUS_ERROR_FAILED,
1051 "<limit> element must have a \"name\" attribute");
1055 e->d.limit.name = _dbus_strdup (name);
1056 if (e->d.limit.name == NULL)
1058 BUS_SET_OOM (error);
1064 else if (strcmp (element_name, "selinux") == 0)
1066 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1069 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1071 BUS_SET_OOM (error);
1079 dbus_set_error (error, DBUS_ERROR_FAILED,
1080 "Element <%s> not allowed inside <%s> in configuration file",
1081 element_name, "busconfig");
1087 append_rule_from_element (BusConfigParser *parser,
1088 const char *element_name,
1089 const char **attribute_names,
1090 const char **attribute_values,
1094 const char *send_interface;
1095 const char *send_member;
1096 const char *send_error;
1097 const char *send_destination;
1098 const char *send_path;
1099 const char *send_type;
1100 const char *receive_interface;
1101 const char *receive_member;
1102 const char *receive_error;
1103 const char *receive_sender;
1104 const char *receive_path;
1105 const char *receive_type;
1106 const char *eavesdrop;
1107 const char *send_requested_reply;
1108 const char *receive_requested_reply;
1113 BusPolicyRule *rule;
1115 if (!locate_attributes (parser, element_name,
1119 "send_interface", &send_interface,
1120 "send_member", &send_member,
1121 "send_error", &send_error,
1122 "send_destination", &send_destination,
1123 "send_path", &send_path,
1124 "send_type", &send_type,
1125 "receive_interface", &receive_interface,
1126 "receive_member", &receive_member,
1127 "receive_error", &receive_error,
1128 "receive_sender", &receive_sender,
1129 "receive_path", &receive_path,
1130 "receive_type", &receive_type,
1131 "eavesdrop", &eavesdrop,
1132 "send_requested_reply", &send_requested_reply,
1133 "receive_requested_reply", &receive_requested_reply,
1140 if (!(send_interface || send_member || send_error || send_destination ||
1141 send_type || send_path ||
1142 receive_interface || receive_member || receive_error || receive_sender ||
1143 receive_type || receive_path || eavesdrop ||
1144 send_requested_reply || receive_requested_reply ||
1145 own || user || group))
1147 dbus_set_error (error, DBUS_ERROR_FAILED,
1148 "Element <%s> must have one or more attributes",
1153 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1154 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1156 dbus_set_error (error, DBUS_ERROR_FAILED,
1157 "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.",
1162 /* Allowed combinations of elements are:
1164 * base, must be all send or all receive:
1167 * interface + member
1170 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1171 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1173 * user, group, own must occur alone
1175 * Pretty sure the below stuff is broken, FIXME think about it more.
1178 if (((send_interface && send_error) ||
1179 (send_interface && receive_interface) ||
1180 (send_interface && receive_member) ||
1181 (send_interface && receive_error) ||
1182 (send_interface && receive_sender) ||
1183 (send_interface && receive_requested_reply) ||
1184 (send_interface && own) ||
1185 (send_interface && user) ||
1186 (send_interface && group)) ||
1188 ((send_member && send_error) ||
1189 (send_member && receive_interface) ||
1190 (send_member && receive_member) ||
1191 (send_member && receive_error) ||
1192 (send_member && receive_sender) ||
1193 (send_member && receive_requested_reply) ||
1194 (send_member && own) ||
1195 (send_member && user) ||
1196 (send_member && group)) ||
1198 ((send_error && receive_interface) ||
1199 (send_error && receive_member) ||
1200 (send_error && receive_error) ||
1201 (send_error && receive_sender) ||
1202 (send_error && receive_requested_reply) ||
1203 (send_error && own) ||
1204 (send_error && user) ||
1205 (send_error && group)) ||
1207 ((send_destination && receive_interface) ||
1208 (send_destination && receive_member) ||
1209 (send_destination && receive_error) ||
1210 (send_destination && receive_sender) ||
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 && receive_requested_reply) ||
1221 (send_type && own) ||
1222 (send_type && user) ||
1223 (send_type && group)) ||
1225 ((send_path && receive_interface) ||
1226 (send_path && receive_member) ||
1227 (send_path && receive_error) ||
1228 (send_path && receive_sender) ||
1229 (send_path && receive_requested_reply) ||
1230 (send_path && own) ||
1231 (send_path && user) ||
1232 (send_path && group)) ||
1234 ((send_requested_reply && receive_interface) ||
1235 (send_requested_reply && receive_member) ||
1236 (send_requested_reply && receive_error) ||
1237 (send_requested_reply && receive_sender) ||
1238 (send_requested_reply && receive_requested_reply) ||
1239 (send_requested_reply && own) ||
1240 (send_requested_reply && user) ||
1241 (send_requested_reply && group)) ||
1243 ((receive_interface && receive_error) ||
1244 (receive_interface && own) ||
1245 (receive_interface && user) ||
1246 (receive_interface && group)) ||
1248 ((receive_member && receive_error) ||
1249 (receive_member && own) ||
1250 (receive_member && user) ||
1251 (receive_member && group)) ||
1253 ((receive_error && own) ||
1254 (receive_error && user) ||
1255 (receive_error && group)) ||
1257 ((eavesdrop && own) ||
1258 (eavesdrop && user) ||
1259 (eavesdrop && group)) ||
1261 ((receive_requested_reply && own) ||
1262 (receive_requested_reply && user) ||
1263 (receive_requested_reply && group)) ||
1270 dbus_set_error (error, DBUS_ERROR_FAILED,
1271 "Invalid combination of attributes on element <%s>",
1278 /* In BusPolicyRule, NULL represents wildcard.
1279 * In the config file, '*' represents it.
1281 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1283 if (send_interface || send_member || send_error || send_destination ||
1284 send_path || send_type || send_requested_reply)
1288 if (IS_WILDCARD (send_interface))
1289 send_interface = NULL;
1290 if (IS_WILDCARD (send_member))
1292 if (IS_WILDCARD (send_error))
1294 if (IS_WILDCARD (send_destination))
1295 send_destination = NULL;
1296 if (IS_WILDCARD (send_path))
1298 if (IS_WILDCARD (send_type))
1301 message_type = DBUS_MESSAGE_TYPE_INVALID;
1302 if (send_type != NULL)
1304 message_type = dbus_message_type_from_string (send_type);
1305 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1307 dbus_set_error (error, DBUS_ERROR_FAILED,
1308 "Bad message type \"%s\"",
1315 !(strcmp (eavesdrop, "true") == 0 ||
1316 strcmp (eavesdrop, "false") == 0))
1318 dbus_set_error (error, DBUS_ERROR_FAILED,
1319 "Bad value \"%s\" for %s attribute, must be true or false",
1320 "eavesdrop", eavesdrop);
1324 if (send_requested_reply &&
1325 !(strcmp (send_requested_reply, "true") == 0 ||
1326 strcmp (send_requested_reply, "false") == 0))
1328 dbus_set_error (error, DBUS_ERROR_FAILED,
1329 "Bad value \"%s\" for %s attribute, must be true or false",
1330 "send_requested_reply", send_requested_reply);
1334 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1339 rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1341 if (send_requested_reply)
1342 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1344 rule->d.send.message_type = message_type;
1345 rule->d.send.path = _dbus_strdup (send_path);
1346 rule->d.send.interface = _dbus_strdup (send_interface);
1347 rule->d.send.member = _dbus_strdup (send_member);
1348 rule->d.send.error = _dbus_strdup (send_error);
1349 rule->d.send.destination = _dbus_strdup (send_destination);
1350 if (send_path && rule->d.send.path == NULL)
1352 if (send_interface && rule->d.send.interface == NULL)
1354 if (send_member && rule->d.send.member == NULL)
1356 if (send_error && rule->d.send.error == NULL)
1358 if (send_destination && rule->d.send.destination == NULL)
1361 else if (receive_interface || receive_member || receive_error || receive_sender ||
1362 receive_path || receive_type || eavesdrop || receive_requested_reply)
1366 if (IS_WILDCARD (receive_interface))
1367 receive_interface = NULL;
1368 if (IS_WILDCARD (receive_member))
1369 receive_member = NULL;
1370 if (IS_WILDCARD (receive_error))
1371 receive_error = NULL;
1372 if (IS_WILDCARD (receive_sender))
1373 receive_sender = NULL;
1374 if (IS_WILDCARD (receive_path))
1375 receive_path = NULL;
1376 if (IS_WILDCARD (receive_type))
1377 receive_type = NULL;
1379 message_type = DBUS_MESSAGE_TYPE_INVALID;
1380 if (receive_type != NULL)
1382 message_type = dbus_message_type_from_string (receive_type);
1383 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1385 dbus_set_error (error, DBUS_ERROR_FAILED,
1386 "Bad message type \"%s\"",
1394 !(strcmp (eavesdrop, "true") == 0 ||
1395 strcmp (eavesdrop, "false") == 0))
1397 dbus_set_error (error, DBUS_ERROR_FAILED,
1398 "Bad value \"%s\" for %s attribute, must be true or false",
1399 "eavesdrop", eavesdrop);
1403 if (receive_requested_reply &&
1404 !(strcmp (receive_requested_reply, "true") == 0 ||
1405 strcmp (receive_requested_reply, "false") == 0))
1407 dbus_set_error (error, DBUS_ERROR_FAILED,
1408 "Bad value \"%s\" for %s attribute, must be true or false",
1409 "receive_requested_reply", receive_requested_reply);
1413 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1418 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1420 if (receive_requested_reply)
1421 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1423 rule->d.receive.message_type = message_type;
1424 rule->d.receive.path = _dbus_strdup (receive_path);
1425 rule->d.receive.interface = _dbus_strdup (receive_interface);
1426 rule->d.receive.member = _dbus_strdup (receive_member);
1427 rule->d.receive.error = _dbus_strdup (receive_error);
1428 rule->d.receive.origin = _dbus_strdup (receive_sender);
1430 if (receive_path && rule->d.receive.path == NULL)
1432 if (receive_interface && rule->d.receive.interface == NULL)
1434 if (receive_member && rule->d.receive.member == NULL)
1436 if (receive_error && rule->d.receive.error == NULL)
1438 if (receive_sender && rule->d.receive.origin == NULL)
1443 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1447 if (IS_WILDCARD (own))
1450 rule->d.own.service_name = _dbus_strdup (own);
1451 if (own && rule->d.own.service_name == NULL)
1456 if (IS_WILDCARD (user))
1458 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1462 rule->d.user.uid = DBUS_UID_UNSET;
1466 DBusString username;
1469 _dbus_string_init_const (&username, user);
1471 if (_dbus_parse_unix_user_from_config (&username, &uid))
1473 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1477 rule->d.user.uid = uid;
1481 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1482 user, element_name);
1488 if (IS_WILDCARD (group))
1490 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1494 rule->d.group.gid = DBUS_GID_UNSET;
1498 DBusString groupname;
1501 _dbus_string_init_const (&groupname, group);
1503 if (_dbus_parse_unix_group_from_config (&groupname, &gid))
1505 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1509 rule->d.group.gid = gid;
1513 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1514 group, element_name);
1519 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1525 pe = peek_element (parser);
1526 _dbus_assert (pe != NULL);
1527 _dbus_assert (pe->type == ELEMENT_POLICY);
1529 switch (pe->d.policy.type)
1531 case POLICY_IGNORED:
1532 /* drop the rule on the floor */
1535 case POLICY_DEFAULT:
1536 if (!bus_policy_append_default_rule (parser->policy, rule))
1539 case POLICY_MANDATORY:
1540 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1544 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1546 dbus_set_error (error, DBUS_ERROR_FAILED,
1547 "<%s> rule cannot be per-user because it has bus-global semantics",
1552 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1557 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1559 dbus_set_error (error, DBUS_ERROR_FAILED,
1560 "<%s> rule cannot be per-group because it has bus-global semantics",
1565 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1571 case POLICY_CONSOLE:
1572 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1578 bus_policy_rule_unref (rule);
1585 BUS_SET_OOM (error);
1588 bus_policy_rule_unref (rule);
1593 start_policy_child (BusConfigParser *parser,
1594 const char *element_name,
1595 const char **attribute_names,
1596 const char **attribute_values,
1599 if (strcmp (element_name, "allow") == 0)
1601 if (!append_rule_from_element (parser, element_name,
1602 attribute_names, attribute_values,
1606 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1608 BUS_SET_OOM (error);
1614 else if (strcmp (element_name, "deny") == 0)
1616 if (!append_rule_from_element (parser, element_name,
1617 attribute_names, attribute_values,
1621 if (push_element (parser, ELEMENT_DENY) == NULL)
1623 BUS_SET_OOM (error);
1631 dbus_set_error (error, DBUS_ERROR_FAILED,
1632 "Element <%s> not allowed inside <%s> in configuration file",
1633 element_name, "policy");
1639 start_selinux_child (BusConfigParser *parser,
1640 const char *element_name,
1641 const char **attribute_names,
1642 const char **attribute_values,
1649 context_copy = NULL;
1651 if (strcmp (element_name, "associate") == 0)
1654 const char *context;
1656 if (!locate_attributes (parser, "associate",
1661 "context", &context,
1665 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1667 BUS_SET_OOM (error);
1671 if (own == NULL || context == NULL)
1673 dbus_set_error (error, DBUS_ERROR_FAILED,
1674 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1678 own_copy = _dbus_strdup (own);
1679 if (own_copy == NULL)
1681 context_copy = _dbus_strdup (context);
1682 if (context_copy == NULL)
1685 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1686 own_copy, context_copy))
1693 dbus_set_error (error, DBUS_ERROR_FAILED,
1694 "Element <%s> not allowed inside <%s> in configuration file",
1695 element_name, "selinux");
1701 dbus_free (own_copy);
1704 dbus_free (context_copy);
1706 BUS_SET_OOM (error);
1711 bus_config_parser_start_element (BusConfigParser *parser,
1712 const char *element_name,
1713 const char **attribute_names,
1714 const char **attribute_values,
1719 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1721 /* printf ("START: %s\n", element_name); */
1723 t = top_element_type (parser);
1725 if (t == ELEMENT_NONE)
1727 if (strcmp (element_name, "busconfig") == 0)
1729 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1732 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1734 BUS_SET_OOM (error);
1742 dbus_set_error (error, DBUS_ERROR_FAILED,
1743 "Unknown element <%s> at root of configuration file",
1748 else if (t == ELEMENT_BUSCONFIG)
1750 return start_busconfig_child (parser, element_name,
1751 attribute_names, attribute_values,
1754 else if (t == ELEMENT_POLICY)
1756 return start_policy_child (parser, element_name,
1757 attribute_names, attribute_values,
1760 else if (t == ELEMENT_SELINUX)
1762 return start_selinux_child (parser, element_name,
1763 attribute_names, attribute_values,
1768 dbus_set_error (error, DBUS_ERROR_FAILED,
1769 "Element <%s> is not allowed in this context",
1776 set_limit (BusConfigParser *parser,
1781 dbus_bool_t must_be_positive;
1782 dbus_bool_t must_be_int;
1784 must_be_int = FALSE;
1785 must_be_positive = FALSE;
1787 if (strcmp (name, "max_incoming_bytes") == 0)
1789 must_be_positive = TRUE;
1790 parser->limits.max_incoming_bytes = value;
1792 else if (strcmp (name, "max_outgoing_bytes") == 0)
1794 must_be_positive = TRUE;
1795 parser->limits.max_outgoing_bytes = value;
1797 else if (strcmp (name, "max_message_size") == 0)
1799 must_be_positive = TRUE;
1800 parser->limits.max_message_size = value;
1802 else if (strcmp (name, "service_start_timeout") == 0)
1804 must_be_positive = TRUE;
1806 parser->limits.activation_timeout = value;
1808 else if (strcmp (name, "auth_timeout") == 0)
1810 must_be_positive = TRUE;
1812 parser->limits.auth_timeout = value;
1814 else if (strcmp (name, "reply_timeout") == 0)
1816 must_be_positive = TRUE;
1818 parser->limits.reply_timeout = value;
1820 else if (strcmp (name, "max_completed_connections") == 0)
1822 must_be_positive = TRUE;
1824 parser->limits.max_completed_connections = value;
1826 else if (strcmp (name, "max_incomplete_connections") == 0)
1828 must_be_positive = TRUE;
1830 parser->limits.max_incomplete_connections = value;
1832 else if (strcmp (name, "max_connections_per_user") == 0)
1834 must_be_positive = TRUE;
1836 parser->limits.max_connections_per_user = value;
1838 else if (strcmp (name, "max_pending_service_starts") == 0)
1840 must_be_positive = TRUE;
1842 parser->limits.max_pending_activations = value;
1844 else if (strcmp (name, "max_names_per_connection") == 0)
1846 must_be_positive = TRUE;
1848 parser->limits.max_services_per_connection = value;
1850 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1852 must_be_positive = TRUE;
1854 parser->limits.max_match_rules_per_connection = value;
1856 else if (strcmp (name, "max_replies_per_connection") == 0)
1858 must_be_positive = TRUE;
1860 parser->limits.max_replies_per_connection = value;
1864 dbus_set_error (error, DBUS_ERROR_FAILED,
1865 "There is no limit called \"%s\"\n",
1870 if (must_be_positive && value < 0)
1872 dbus_set_error (error, DBUS_ERROR_FAILED,
1873 "<limit name=\"%s\"> must be a positive number\n",
1879 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1881 dbus_set_error (error, DBUS_ERROR_FAILED,
1882 "<limit name=\"%s\"> value is too large\n",
1891 bus_config_parser_end_element (BusConfigParser *parser,
1892 const char *element_name,
1899 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1901 /* printf ("END: %s\n", element_name); */
1903 t = top_element_type (parser);
1905 if (t == ELEMENT_NONE)
1907 /* should probably be an assertion failure but
1908 * being paranoid about XML parsers
1910 dbus_set_error (error, DBUS_ERROR_FAILED,
1911 "XML parser ended element with no element on the stack");
1915 n = element_type_to_name (t);
1916 _dbus_assert (n != NULL);
1917 if (strcmp (n, element_name) != 0)
1919 /* should probably be an assertion failure but
1920 * being paranoid about XML parsers
1922 dbus_set_error (error, DBUS_ERROR_FAILED,
1923 "XML element <%s> ended but topmost element on the stack was <%s>",
1928 e = peek_element (parser);
1929 _dbus_assert (e != NULL);
1934 _dbus_assert_not_reached ("element in stack has no type");
1937 case ELEMENT_INCLUDE:
1940 case ELEMENT_LISTEN:
1941 case ELEMENT_PIDFILE:
1943 case ELEMENT_SERVICEDIR:
1944 case ELEMENT_INCLUDEDIR:
1946 if (!e->had_content)
1948 dbus_set_error (error, DBUS_ERROR_FAILED,
1949 "XML element <%s> was expected to have content inside it",
1950 element_type_to_name (e->type));
1954 if (e->type == ELEMENT_LIMIT)
1956 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1962 case ELEMENT_BUSCONFIG:
1963 case ELEMENT_POLICY:
1967 case ELEMENT_SELINUX:
1968 case ELEMENT_ASSOCIATE:
1969 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
1973 pop_element (parser);
1979 all_whitespace (const DBusString *str)
1983 _dbus_string_skip_white (str, 0, &i);
1985 return i == _dbus_string_get_length (str);
1989 make_full_path (const DBusString *basedir,
1990 const DBusString *filename,
1991 DBusString *full_path)
1993 if (_dbus_path_is_absolute (filename))
1995 return _dbus_string_copy (filename, 0, full_path, 0);
1999 if (!_dbus_string_copy (basedir, 0, full_path, 0))
2002 if (!_dbus_concat_dir_and_file (full_path, filename))
2010 include_file (BusConfigParser *parser,
2011 const DBusString *filename,
2012 dbus_bool_t ignore_missing,
2015 /* FIXME good test case for this would load each config file in the
2016 * test suite both alone, and as an include, and check
2017 * that the result is the same
2019 BusConfigParser *included;
2020 const char *filename_str;
2021 DBusError tmp_error;
2023 dbus_error_init (&tmp_error);
2025 filename_str = _dbus_string_get_const_data (filename);
2027 /* Check to make sure this file hasn't already been included. */
2028 if (seen_include (parser, filename))
2030 dbus_set_error (error, DBUS_ERROR_FAILED,
2031 "Circular inclusion of file '%s'",
2036 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2038 BUS_SET_OOM (error);
2042 /* Since parser is passed in as the parent, included
2043 inherits parser's limits. */
2044 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2046 _dbus_list_pop_last (&parser->included_files);
2048 if (included == NULL)
2050 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2052 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2055 dbus_error_free (&tmp_error);
2060 dbus_move_error (&tmp_error, error);
2066 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2068 if (!merge_included (parser, included, error))
2070 bus_config_parser_unref (included);
2074 /* Copy included's limits back to parser. */
2075 parser->limits = included->limits;
2077 bus_config_parser_unref (included);
2083 include_dir (BusConfigParser *parser,
2084 const DBusString *dirname,
2087 DBusString filename;
2089 DBusError tmp_error;
2093 if (!_dbus_string_init (&filename))
2095 BUS_SET_OOM (error);
2101 dir = _dbus_directory_open (dirname, error);
2106 dbus_error_init (&tmp_error);
2107 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2109 DBusString full_path;
2111 if (!_dbus_string_init (&full_path))
2113 BUS_SET_OOM (error);
2117 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2119 BUS_SET_OOM (error);
2120 _dbus_string_free (&full_path);
2124 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2126 BUS_SET_OOM (error);
2127 _dbus_string_free (&full_path);
2131 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2133 if (!include_file (parser, &full_path, TRUE, error))
2135 _dbus_string_free (&full_path);
2140 _dbus_string_free (&full_path);
2143 if (dbus_error_is_set (&tmp_error))
2145 dbus_move_error (&tmp_error, error);
2150 if (!_dbus_string_copy_data (dirname, &s))
2152 BUS_SET_OOM (error);
2156 if (!_dbus_list_append (&parser->conf_dirs, s))
2159 BUS_SET_OOM (error);
2166 _dbus_string_free (&filename);
2169 _dbus_directory_close (dir);
2175 bus_config_parser_content (BusConfigParser *parser,
2176 const DBusString *content,
2181 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2187 _dbus_string_get_const_data (content, &c_str);
2189 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2193 e = peek_element (parser);
2196 dbus_set_error (error, DBUS_ERROR_FAILED,
2197 "Text content outside of any XML element in configuration file");
2200 else if (e->had_content)
2202 _dbus_assert_not_reached ("Element had multiple content blocks");
2206 switch (top_element_type (parser))
2209 _dbus_assert_not_reached ("element at top of stack has no type");
2212 case ELEMENT_BUSCONFIG:
2213 case ELEMENT_POLICY:
2217 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2218 case ELEMENT_SELINUX:
2219 case ELEMENT_ASSOCIATE:
2220 if (all_whitespace (content))
2224 dbus_set_error (error, DBUS_ERROR_FAILED,
2225 "No text content expected inside XML element %s in configuration file",
2226 element_type_to_name (top_element_type (parser)));
2230 case ELEMENT_PIDFILE:
2234 e->had_content = TRUE;
2236 if (!_dbus_string_copy_data (content, &s))
2239 dbus_free (parser->pidfile);
2240 parser->pidfile = s;
2244 case ELEMENT_INCLUDE:
2246 DBusString full_path, selinux_policy_root;
2248 e->had_content = TRUE;
2250 if (e->d.include.if_selinux_enabled
2251 && !bus_selinux_enabled ())
2254 if (!_dbus_string_init (&full_path))
2257 if (e->d.include.selinux_root_relative)
2259 if (!bus_selinux_get_policy_root ())
2261 dbus_set_error (error, DBUS_ERROR_FAILED,
2262 "Could not determine SELinux policy root for relative inclusion");
2263 _dbus_string_free (&full_path);
2266 _dbus_string_init_const (&selinux_policy_root,
2267 bus_selinux_get_policy_root ());
2268 if (!make_full_path (&selinux_policy_root, content, &full_path))
2270 _dbus_string_free (&full_path);
2274 else if (!make_full_path (&parser->basedir, content, &full_path))
2276 _dbus_string_free (&full_path);
2280 if (!include_file (parser, &full_path,
2281 e->d.include.ignore_missing, error))
2283 _dbus_string_free (&full_path);
2287 _dbus_string_free (&full_path);
2291 case ELEMENT_INCLUDEDIR:
2293 DBusString full_path;
2295 e->had_content = TRUE;
2297 if (!_dbus_string_init (&full_path))
2300 if (!make_full_path (&parser->basedir, content, &full_path))
2302 _dbus_string_free (&full_path);
2306 if (!include_dir (parser, &full_path, error))
2308 _dbus_string_free (&full_path);
2312 _dbus_string_free (&full_path);
2320 e->had_content = TRUE;
2322 if (!_dbus_string_copy_data (content, &s))
2325 dbus_free (parser->user);
2334 e->had_content = TRUE;
2336 if (!_dbus_string_copy_data (content, &s))
2339 dbus_free (parser->bus_type);
2340 parser->bus_type = s;
2344 case ELEMENT_LISTEN:
2348 e->had_content = TRUE;
2350 if (!_dbus_string_copy_data (content, &s))
2353 if (!_dbus_list_append (&parser->listen_on,
2366 e->had_content = TRUE;
2368 if (!_dbus_string_copy_data (content, &s))
2371 if (!_dbus_list_append (&parser->mechanisms,
2380 case ELEMENT_SERVICEDIR:
2383 DBusString full_path;
2385 e->had_content = TRUE;
2387 if (!_dbus_string_init (&full_path))
2390 if (!make_full_path (&parser->basedir, content, &full_path))
2392 _dbus_string_free (&full_path);
2396 if (!_dbus_string_copy_data (&full_path, &s))
2398 _dbus_string_free (&full_path);
2402 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2404 _dbus_string_free (&full_path);
2409 _dbus_string_free (&full_path);
2417 e->had_content = TRUE;
2420 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2422 dbus_set_error (error, DBUS_ERROR_FAILED,
2423 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2428 e->d.limit.value = val;
2430 _dbus_verbose ("Loaded value %ld for limit %s\n",
2437 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2441 BUS_SET_OOM (error);
2446 bus_config_parser_finished (BusConfigParser *parser,
2449 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2451 if (parser->stack != NULL)
2453 dbus_set_error (error, DBUS_ERROR_FAILED,
2454 "Element <%s> was not closed in configuration file",
2455 element_type_to_name (top_element_type (parser)));
2460 if (parser->is_toplevel && parser->listen_on == NULL)
2462 dbus_set_error (error, DBUS_ERROR_FAILED,
2463 "Configuration file needs one or more <listen> elements giving addresses");
2471 bus_config_parser_get_user (BusConfigParser *parser)
2473 return parser->user;
2477 bus_config_parser_get_type (BusConfigParser *parser)
2479 return parser->bus_type;
2483 bus_config_parser_get_addresses (BusConfigParser *parser)
2485 return &parser->listen_on;
2489 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2491 return &parser->mechanisms;
2495 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2497 return &parser->service_dirs;
2501 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2503 return &parser->conf_dirs;
2507 bus_config_parser_get_fork (BusConfigParser *parser)
2509 return parser->fork;
2513 bus_config_parser_get_pidfile (BusConfigParser *parser)
2515 return parser->pidfile;
2519 bus_config_parser_steal_policy (BusConfigParser *parser)
2523 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2525 policy = parser->policy;
2527 parser->policy = NULL;
2532 /* Overwrite any limits that were set in the configuration file */
2534 bus_config_parser_get_limits (BusConfigParser *parser,
2537 *limits = parser->limits;
2541 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2543 DBusHashTable *table;
2545 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2547 table = parser->service_context_table;
2549 parser->service_context_table = NULL;
2554 #ifdef DBUS_BUILD_TESTS
2565 do_load (const DBusString *full_path,
2567 dbus_bool_t oom_possible)
2569 BusConfigParser *parser;
2572 dbus_error_init (&error);
2574 parser = bus_config_load (full_path, TRUE, NULL, &error);
2577 _DBUS_ASSERT_ERROR_IS_SET (&error);
2580 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2582 _dbus_verbose ("Failed to load valid file due to OOM\n");
2583 dbus_error_free (&error);
2586 else if (validity == VALID)
2588 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2591 dbus_error_free (&error);
2596 dbus_error_free (&error);
2602 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2604 bus_config_parser_unref (parser);
2606 if (validity == INVALID)
2608 _dbus_warn ("Accepted invalid file\n");
2618 const DBusString *full_path;
2623 check_loader_oom_func (void *data)
2625 LoaderOomData *d = data;
2627 return do_load (d->full_path, d->validity, TRUE);
2631 process_test_valid_subdir (const DBusString *test_base_dir,
2635 DBusString test_directory;
2636 DBusString filename;
2644 if (!_dbus_string_init (&test_directory))
2645 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2647 _dbus_string_init_const (&filename, subdir);
2649 if (!_dbus_string_copy (test_base_dir, 0,
2650 &test_directory, 0))
2651 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2653 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2654 _dbus_assert_not_reached ("couldn't allocate full path");
2656 _dbus_string_free (&filename);
2657 if (!_dbus_string_init (&filename))
2658 _dbus_assert_not_reached ("didn't allocate filename string\n");
2660 dbus_error_init (&error);
2661 dir = _dbus_directory_open (&test_directory, &error);
2664 _dbus_warn ("Could not open %s: %s\n",
2665 _dbus_string_get_const_data (&test_directory),
2667 dbus_error_free (&error);
2671 if (validity == VALID)
2672 printf ("Testing valid files:\n");
2673 else if (validity == INVALID)
2674 printf ("Testing invalid files:\n");
2676 printf ("Testing unknown files:\n");
2679 while (_dbus_directory_get_next_file (dir, &filename, &error))
2681 DBusString full_path;
2684 if (!_dbus_string_init (&full_path))
2685 _dbus_assert_not_reached ("couldn't init string");
2687 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2688 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2690 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2691 _dbus_assert_not_reached ("couldn't concat file to dir");
2693 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2695 _dbus_verbose ("Skipping non-.conf file %s\n",
2696 _dbus_string_get_const_data (&filename));
2697 _dbus_string_free (&full_path);
2701 printf (" %s\n", _dbus_string_get_const_data (&filename));
2703 _dbus_verbose (" expecting %s\n",
2704 validity == VALID ? "valid" :
2705 (validity == INVALID ? "invalid" :
2706 (validity == UNKNOWN ? "unknown" : "???")));
2708 d.full_path = &full_path;
2709 d.validity = validity;
2711 /* FIXME hackaround for an expat problem, see
2712 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2713 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2715 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2716 if (!check_loader_oom_func (&d))
2717 _dbus_assert_not_reached ("test failed");
2719 _dbus_string_free (&full_path);
2722 if (dbus_error_is_set (&error))
2724 _dbus_warn ("Could not get next file in %s: %s\n",
2725 _dbus_string_get_const_data (&test_directory),
2727 dbus_error_free (&error);
2736 _dbus_directory_close (dir);
2737 _dbus_string_free (&test_directory);
2738 _dbus_string_free (&filename);
2744 bools_equal (dbus_bool_t a,
2751 strings_equal_or_both_null (const char *a,
2754 if (a == NULL || b == NULL)
2757 return !strcmp (a, b);
2761 elements_equal (const Element *a,
2764 if (a->type != b->type)
2767 if (!bools_equal (a->had_content, b->had_content))
2773 case ELEMENT_INCLUDE:
2774 if (!bools_equal (a->d.include.ignore_missing,
2775 b->d.include.ignore_missing))
2779 case ELEMENT_POLICY:
2780 if (a->d.policy.type != b->d.policy.type)
2782 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
2787 if (strcmp (a->d.limit.name, b->d.limit.name))
2789 if (a->d.limit.value != b->d.limit.value)
2803 lists_of_elements_equal (DBusList *a,
2812 while (ia != NULL && ib != NULL)
2814 if (elements_equal (ia->data, ib->data))
2816 ia = _dbus_list_get_next_link (&a, ia);
2817 ib = _dbus_list_get_next_link (&b, ib);
2820 return ia == NULL && ib == NULL;
2824 lists_of_c_strings_equal (DBusList *a,
2833 while (ia != NULL && ib != NULL)
2835 if (strcmp (ia->data, ib->data))
2837 ia = _dbus_list_get_next_link (&a, ia);
2838 ib = _dbus_list_get_next_link (&b, ib);
2841 return ia == NULL && ib == NULL;
2845 limits_equal (const BusLimits *a,
2849 (a->max_incoming_bytes == b->max_incoming_bytes
2850 || a->max_outgoing_bytes == b->max_outgoing_bytes
2851 || a->max_message_size == b->max_message_size
2852 || a->activation_timeout == b->activation_timeout
2853 || a->auth_timeout == b->auth_timeout
2854 || a->max_completed_connections == b->max_completed_connections
2855 || a->max_incomplete_connections == b->max_incomplete_connections
2856 || a->max_connections_per_user == b->max_connections_per_user
2857 || a->max_pending_activations == b->max_pending_activations
2858 || a->max_services_per_connection == b->max_services_per_connection
2859 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2860 || a->max_replies_per_connection == b->max_replies_per_connection
2861 || a->reply_timeout == b->reply_timeout);
2865 config_parsers_equal (const BusConfigParser *a,
2866 const BusConfigParser *b)
2868 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2871 if (!lists_of_elements_equal (a->stack, b->stack))
2874 if (!strings_equal_or_both_null (a->user, b->user))
2877 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2880 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2883 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2886 /* FIXME: compare policy */
2888 /* FIXME: compare service selinux ID table */
2890 if (! limits_equal (&a->limits, &b->limits))
2893 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2896 if (! bools_equal (a->fork, b->fork))
2899 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2906 all_are_equiv (const DBusString *target_directory)
2908 DBusString filename;
2910 BusConfigParser *first_parser;
2911 BusConfigParser *parser;
2917 first_parser = NULL;
2921 if (!_dbus_string_init (&filename))
2922 _dbus_assert_not_reached ("didn't allocate filename string");
2924 dbus_error_init (&error);
2925 dir = _dbus_directory_open (target_directory, &error);
2928 _dbus_warn ("Could not open %s: %s\n",
2929 _dbus_string_get_const_data (target_directory),
2931 dbus_error_free (&error);
2935 printf ("Comparing equivalent files:\n");
2938 while (_dbus_directory_get_next_file (dir, &filename, &error))
2940 DBusString full_path;
2942 if (!_dbus_string_init (&full_path))
2943 _dbus_assert_not_reached ("couldn't init string");
2945 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2946 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2948 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2949 _dbus_assert_not_reached ("couldn't concat file to dir");
2951 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2953 _dbus_verbose ("Skipping non-.conf file %s\n",
2954 _dbus_string_get_const_data (&filename));
2955 _dbus_string_free (&full_path);
2959 printf (" %s\n", _dbus_string_get_const_data (&filename));
2961 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2965 _dbus_warn ("Could not load file %s: %s\n",
2966 _dbus_string_get_const_data (&full_path),
2968 _dbus_string_free (&full_path);
2969 dbus_error_free (&error);
2972 else if (first_parser == NULL)
2974 _dbus_string_free (&full_path);
2975 first_parser = parser;
2979 _dbus_string_free (&full_path);
2980 equal = config_parsers_equal (first_parser, parser);
2981 bus_config_parser_unref (parser);
2990 _dbus_string_free (&filename);
2992 bus_config_parser_unref (first_parser);
2994 _dbus_directory_close (dir);
3001 process_test_equiv_subdir (const DBusString *test_base_dir,
3004 DBusString test_directory;
3005 DBusString filename;
3014 if (!_dbus_string_init (&test_directory))
3015 _dbus_assert_not_reached ("didn't allocate test_directory");
3017 _dbus_string_init_const (&filename, subdir);
3019 if (!_dbus_string_copy (test_base_dir, 0,
3020 &test_directory, 0))
3021 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3023 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3024 _dbus_assert_not_reached ("couldn't allocate full path");
3026 _dbus_string_free (&filename);
3027 if (!_dbus_string_init (&filename))
3028 _dbus_assert_not_reached ("didn't allocate filename string");
3030 dbus_error_init (&error);
3031 dir = _dbus_directory_open (&test_directory, &error);
3034 _dbus_warn ("Could not open %s: %s\n",
3035 _dbus_string_get_const_data (&test_directory),
3037 dbus_error_free (&error);
3041 while (_dbus_directory_get_next_file (dir, &filename, &error))
3043 DBusString full_path;
3045 /* Skip CVS's magic directories! */
3046 if (_dbus_string_equal_c_str (&filename, "CVS"))
3049 if (!_dbus_string_init (&full_path))
3050 _dbus_assert_not_reached ("couldn't init string");
3052 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3053 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3055 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3056 _dbus_assert_not_reached ("couldn't concat file to dir");
3058 equal = all_are_equiv (&full_path);
3059 _dbus_string_free (&full_path);
3068 _dbus_string_free (&test_directory);
3069 _dbus_string_free (&filename);
3071 _dbus_directory_close (dir);
3077 static const char *test_service_dir_matches[] =
3080 "/testusr/testlocal/testshare/dbus-1/services",
3081 "/testusr/testshare/dbus-1/services",
3083 DBUS_DATADIR"/dbus-1/services",
3085 "/testhome/foo/.testlocal/testshare/dbus-1/services",
3091 test_default_session_servicedirs (void)
3096 const char *common_progs;
3099 /* On Unix we don't actually use this variable, but it's easier to handle the
3100 * deallocation if we always allocate it, whether needed or not */
3101 if (!_dbus_string_init (&progs))
3102 _dbus_assert_not_reached ("OOM allocating progs");
3104 common_progs = _dbus_getenv ("CommonProgramFiles");
3108 if (!_dbus_string_append (&progs, common_progs))
3110 _dbus_string_free (&progs);
3114 if (!_dbus_string_append (&progs, "/dbus-1/services"))
3116 _dbus_string_free (&progs);
3119 test_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3124 printf ("Testing retrieving the default session service directories\n");
3125 if (!_dbus_get_standard_session_servicedirs (&dirs))
3126 _dbus_assert_not_reached ("couldn't get stardard dirs");
3128 /* make sure our defaults end with share/dbus-1/service */
3129 while ((link = _dbus_list_pop_first_link (&dirs)))
3133 printf (" default service dir: %s\n", (char *)link->data);
3134 _dbus_string_init_const (&path, (char *)link->data);
3135 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/services"))
3137 printf ("error with default session service directories\n");
3138 dbus_free (link->data);
3139 _dbus_list_free_link (link);
3140 _dbus_string_free (&progs);
3144 dbus_free (link->data);
3145 _dbus_list_free_link (link);
3149 if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3150 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3152 if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3153 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3155 if (!_dbus_get_standard_session_servicedirs (&dirs))
3156 _dbus_assert_not_reached ("couldn't get stardard dirs");
3158 /* make sure we read and parse the env variable correctly */
3160 while ((link = _dbus_list_pop_first_link (&dirs)))
3162 printf (" test service dir: %s\n", (char *)link->data);
3163 if (test_service_dir_matches[i] == NULL)
3165 printf ("more directories parsed than in match set\n");
3166 dbus_free (link->data);
3167 _dbus_list_free_link (link);
3168 _dbus_string_free (&progs);
3172 if (strcmp (test_service_dir_matches[i],
3173 (char *)link->data) != 0)
3175 printf ("%s directory does not match %s in the match set\n",
3177 test_service_dir_matches[i]);
3178 dbus_free (link->data);
3179 _dbus_list_free_link (link);
3180 _dbus_string_free (&progs);
3186 dbus_free (link->data);
3187 _dbus_list_free_link (link);
3190 if (test_service_dir_matches[i] != NULL)
3192 printf ("extra data %s in the match set was not matched\n",
3193 test_service_dir_matches[i]);
3195 _dbus_string_free (&progs);
3199 _dbus_string_free (&progs);
3204 bus_config_parser_test (const DBusString *test_data_dir)
3206 if (test_data_dir == NULL ||
3207 _dbus_string_get_length (test_data_dir) == 0)
3209 printf ("No test data\n");
3213 if (!test_default_session_servicedirs())
3216 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3219 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3222 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3228 #endif /* DBUS_BUILD_TESTS */