1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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-common.h"
24 #include "config-parser.h"
29 #include <dbus/dbus-list.h>
30 #include <dbus/dbus-internals.h>
35 /* we ignore policies for unknown groups/users */
50 unsigned int had_content : 1;
56 unsigned int ignore_missing : 1;
57 unsigned int if_selinux_enabled : 1;
58 unsigned int selinux_root_relative : 1;
64 unsigned long gid_uid_or_at_console;
78 * Parser for bus configuration file.
80 struct BusConfigParser
82 int refcount; /**< Reference count */
84 DBusString basedir; /**< Directory we resolve paths relative to */
86 DBusList *stack; /**< stack of Element */
88 char *user; /**< user to run as */
90 char *servicehelper; /**< location of the setuid helper */
92 char *bus_type; /**< Message bus type */
94 DBusList *listen_on; /**< List of addresses to listen to */
96 DBusList *mechanisms; /**< Auth mechanisms */
98 DBusList *service_dirs; /**< Directories to look for session services in */
100 DBusList *conf_dirs; /**< Directories to look for policy configuration in */
102 BusPolicy *policy; /**< Security policy */
104 BusLimits limits; /**< Limits */
106 char *pidfile; /**< PID file */
108 DBusList *included_files; /**< Included files stack */
110 DBusHashTable *service_context_table; /**< Map service names to SELinux contexts */
112 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
114 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
118 push_element (BusConfigParser *parser,
123 _dbus_assert (type != ELEMENT_NONE);
125 e = dbus_new0 (Element, 1);
129 if (!_dbus_list_append (&parser->stack, e))
141 element_free (Element *e)
143 if (e->type == ELEMENT_LIMIT)
144 dbus_free (e->d.limit.name);
150 pop_element (BusConfigParser *parser)
154 e = _dbus_list_pop_last (&parser->stack);
160 peek_element (BusConfigParser *parser)
164 e = _dbus_list_get_last (&parser->stack);
170 top_element_type (BusConfigParser *parser)
174 e = _dbus_list_get_last (&parser->stack);
183 merge_service_context_hash (DBusHashTable *dest,
193 _dbus_hash_iter_init (from, &iter);
194 while (_dbus_hash_iter_next (&iter))
196 const char *service = _dbus_hash_iter_get_string_key (&iter);
197 const char *context = _dbus_hash_iter_get_value (&iter);
199 service_copy = _dbus_strdup (service);
200 if (service_copy == NULL)
202 context_copy = _dbus_strdup (context);
203 if (context_copy == NULL)
206 if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
217 dbus_free (service_copy);
220 dbus_free (context_copy);
226 service_dirs_find_dir (DBusList **service_dirs,
231 _dbus_assert (dir != NULL);
233 for (link = *service_dirs; link; link = _dbus_list_get_next_link(service_dirs, link))
235 const char *link_dir;
237 link_dir = (const char *)link->data;
238 if (strcmp (dir, link_dir) == 0)
246 service_dirs_append_unique_or_free (DBusList **service_dirs,
249 if (!service_dirs_find_dir (service_dirs, dir))
250 return _dbus_list_append (service_dirs, dir);
257 service_dirs_append_link_unique_or_free (DBusList **service_dirs,
260 if (!service_dirs_find_dir (service_dirs, dir_link->data))
262 _dbus_list_append_link (service_dirs, dir_link);
266 dbus_free (dir_link->data);
267 _dbus_list_free_link (dir_link);
272 merge_included (BusConfigParser *parser,
273 BusConfigParser *included,
278 if (!bus_policy_merge (parser->policy,
285 if (!merge_service_context_hash (parser->service_context_table,
286 included->service_context_table))
292 if (included->user != NULL)
294 dbus_free (parser->user);
295 parser->user = included->user;
296 included->user = NULL;
299 if (included->bus_type != NULL)
301 dbus_free (parser->bus_type);
302 parser->bus_type = included->bus_type;
303 included->bus_type = NULL;
309 if (included->pidfile != NULL)
311 dbus_free (parser->pidfile);
312 parser->pidfile = included->pidfile;
313 included->pidfile = NULL;
316 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
317 _dbus_list_append_link (&parser->listen_on, link);
319 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
320 _dbus_list_append_link (&parser->mechanisms, link);
322 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
323 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
325 while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
326 _dbus_list_append_link (&parser->conf_dirs, link);
332 seen_include (BusConfigParser *parser,
333 const DBusString *file)
337 iter = parser->included_files;
340 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
343 iter = _dbus_list_get_next_link (&parser->included_files, iter);
350 bus_config_parser_new (const DBusString *basedir,
351 dbus_bool_t is_toplevel,
352 const BusConfigParser *parent)
354 BusConfigParser *parser;
356 parser = dbus_new0 (BusConfigParser, 1);
360 parser->is_toplevel = !!is_toplevel;
362 if (!_dbus_string_init (&parser->basedir))
368 if (((parser->policy = bus_policy_new ()) == NULL) ||
369 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
370 ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
372 dbus_free)) == NULL))
375 bus_policy_unref (parser->policy);
377 _dbus_string_free (&parser->basedir);
385 /* Initialize the parser's limits from the parent. */
386 parser->limits = parent->limits;
388 /* Use the parent's list of included_files to avoid
389 circular inclusions. */
390 parser->included_files = parent->included_files;
395 /* Make up some numbers! woot! */
396 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 127;
397 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 127;
398 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
400 /* Making this long means the user has to wait longer for an error
401 * message if something screws up, but making it too short means
402 * they might see a false failure.
404 parser->limits.activation_timeout = 25000; /* 25 seconds */
406 /* Making this long risks making a DOS attack easier, but too short
407 * and legitimate auth will fail. If interactive auth (ask user for
408 * password) is allowed, then potentially it has to be quite long.
410 parser->limits.auth_timeout = 30000; /* 30 seconds */
412 parser->limits.max_incomplete_connections = 64;
413 parser->limits.max_connections_per_user = 256;
415 /* Note that max_completed_connections / max_connections_per_user
416 * is the number of users that would have to work together to
417 * DOS all the other users.
419 parser->limits.max_completed_connections = 2048;
421 parser->limits.max_pending_activations = 512;
422 parser->limits.max_services_per_connection = 512;
424 /* For this one, keep in mind that it isn't only the memory used
425 * by the match rules, but slowdown from linearly walking a big
426 * list of them. A client adding more than this is almost
427 * certainly a bad idea for that reason, and should change to a
428 * smaller number of wider-net match rules - getting every last
429 * message to the bus is probably better than having a thousand
432 parser->limits.max_match_rules_per_connection = 512;
434 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
436 /* this is effectively a limit on message queue size for messages
437 * that require a reply
439 parser->limits.max_replies_per_connection = 1024*8;
442 parser->refcount = 1;
448 bus_config_parser_ref (BusConfigParser *parser)
450 _dbus_assert (parser->refcount > 0);
452 parser->refcount += 1;
458 bus_config_parser_unref (BusConfigParser *parser)
460 _dbus_assert (parser->refcount > 0);
462 parser->refcount -= 1;
464 if (parser->refcount == 0)
466 while (parser->stack != NULL)
467 pop_element (parser);
469 dbus_free (parser->user);
470 dbus_free (parser->servicehelper);
471 dbus_free (parser->bus_type);
472 dbus_free (parser->pidfile);
474 _dbus_list_foreach (&parser->listen_on,
475 (DBusForeachFunction) dbus_free,
478 _dbus_list_clear (&parser->listen_on);
480 _dbus_list_foreach (&parser->service_dirs,
481 (DBusForeachFunction) dbus_free,
484 _dbus_list_clear (&parser->service_dirs);
486 _dbus_list_foreach (&parser->conf_dirs,
487 (DBusForeachFunction) dbus_free,
490 _dbus_list_clear (&parser->conf_dirs);
492 _dbus_list_foreach (&parser->mechanisms,
493 (DBusForeachFunction) dbus_free,
496 _dbus_list_clear (&parser->mechanisms);
498 _dbus_string_free (&parser->basedir);
501 bus_policy_unref (parser->policy);
503 if (parser->service_context_table)
504 _dbus_hash_table_unref (parser->service_context_table);
511 bus_config_parser_check_doctype (BusConfigParser *parser,
515 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
517 if (strcmp (doctype, "busconfig") != 0)
519 dbus_set_error (error,
521 "Configuration file has the wrong document type %s",
536 locate_attributes (BusConfigParser *parser,
537 const char *element_name,
538 const char **attribute_names,
539 const char **attribute_values,
541 const char *first_attribute_name,
542 const char **first_attribute_retloc,
550 LocateAttr attrs[MAX_ATTRS];
554 _dbus_assert (first_attribute_name != NULL);
555 _dbus_assert (first_attribute_retloc != NULL);
560 attrs[0].name = first_attribute_name;
561 attrs[0].retloc = first_attribute_retloc;
562 *first_attribute_retloc = NULL;
564 va_start (args, first_attribute_retloc);
566 name = va_arg (args, const char*);
567 retloc = va_arg (args, const char**);
571 _dbus_assert (retloc != NULL);
572 _dbus_assert (n_attrs < MAX_ATTRS);
574 attrs[n_attrs].name = name;
575 attrs[n_attrs].retloc = retloc;
579 name = va_arg (args, const char*);
580 retloc = va_arg (args, const char**);
586 while (attribute_names[i])
595 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
597 retloc = attrs[j].retloc;
601 dbus_set_error (error, DBUS_ERROR_FAILED,
602 "Attribute \"%s\" repeated twice on the same <%s> element",
603 attrs[j].name, element_name);
608 *retloc = attribute_values[i];
617 dbus_set_error (error, DBUS_ERROR_FAILED,
618 "Attribute \"%s\" is invalid on <%s> element in this context",
619 attribute_names[i], element_name);
632 check_no_attributes (BusConfigParser *parser,
633 const char *element_name,
634 const char **attribute_names,
635 const char **attribute_values,
638 if (attribute_names[0] != NULL)
640 dbus_set_error (error, DBUS_ERROR_FAILED,
641 "Attribute \"%s\" is invalid on <%s> element in this context",
642 attribute_names[0], element_name);
650 start_busconfig_child (BusConfigParser *parser,
651 const char *element_name,
652 const char **attribute_names,
653 const char **attribute_values,
656 ElementType element_type;
658 element_type = bus_config_parser_element_name_to_type (element_name);
660 if (element_type == ELEMENT_USER)
662 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
665 if (push_element (parser, ELEMENT_USER) == NULL)
673 else if (element_type == ELEMENT_TYPE)
675 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
678 if (push_element (parser, ELEMENT_TYPE) == NULL)
686 else if (element_type == ELEMENT_FORK)
688 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
691 if (push_element (parser, ELEMENT_FORK) == NULL)
701 else if (element_type == ELEMENT_PIDFILE)
703 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
706 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
714 else if (element_type == ELEMENT_LISTEN)
716 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
719 if (push_element (parser, ELEMENT_LISTEN) == NULL)
727 else if (element_type == ELEMENT_AUTH)
729 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
732 if (push_element (parser, ELEMENT_AUTH) == NULL)
740 else if (element_type == ELEMENT_SERVICEHELPER)
742 if (!check_no_attributes (parser, "servicehelper", attribute_names, attribute_values, error))
745 if (push_element (parser, ELEMENT_SERVICEHELPER) == NULL)
753 else if (element_type == ELEMENT_INCLUDEDIR)
755 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
758 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
766 else if (element_type == ELEMENT_STANDARD_SESSION_SERVICEDIRS)
772 if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
775 if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
781 if (!_dbus_get_standard_session_servicedirs (&dirs))
787 while ((link = _dbus_list_pop_first_link (&dirs)))
788 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
792 else if (element_type == ELEMENT_STANDARD_SYSTEM_SERVICEDIRS)
798 if (!check_no_attributes (parser, "standard_system_servicedirs", attribute_names, attribute_values, error))
801 if (push_element (parser, ELEMENT_STANDARD_SYSTEM_SERVICEDIRS) == NULL)
807 if (!_dbus_get_standard_system_servicedirs (&dirs))
813 while ((link = _dbus_list_pop_first_link (&dirs)))
814 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
818 else if (element_type == ELEMENT_SERVICEDIR)
820 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
823 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
831 else if (element_type == ELEMENT_INCLUDE)
834 const char *if_selinux_enabled;
835 const char *ignore_missing;
836 const char *selinux_root_relative;
838 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
844 e->d.include.ignore_missing = FALSE;
845 e->d.include.if_selinux_enabled = FALSE;
846 e->d.include.selinux_root_relative = FALSE;
848 if (!locate_attributes (parser, "include",
852 "ignore_missing", &ignore_missing,
853 "if_selinux_enabled", &if_selinux_enabled,
854 "selinux_root_relative", &selinux_root_relative,
858 if (ignore_missing != NULL)
860 if (strcmp (ignore_missing, "yes") == 0)
861 e->d.include.ignore_missing = TRUE;
862 else if (strcmp (ignore_missing, "no") == 0)
863 e->d.include.ignore_missing = FALSE;
866 dbus_set_error (error, DBUS_ERROR_FAILED,
867 "ignore_missing attribute must have value \"yes\" or \"no\"");
872 if (if_selinux_enabled != NULL)
874 if (strcmp (if_selinux_enabled, "yes") == 0)
875 e->d.include.if_selinux_enabled = TRUE;
876 else if (strcmp (if_selinux_enabled, "no") == 0)
877 e->d.include.if_selinux_enabled = FALSE;
880 dbus_set_error (error, DBUS_ERROR_FAILED,
881 "if_selinux_enabled attribute must have value"
882 " \"yes\" or \"no\"");
887 if (selinux_root_relative != NULL)
889 if (strcmp (selinux_root_relative, "yes") == 0)
890 e->d.include.selinux_root_relative = TRUE;
891 else if (strcmp (selinux_root_relative, "no") == 0)
892 e->d.include.selinux_root_relative = FALSE;
895 dbus_set_error (error, DBUS_ERROR_FAILED,
896 "selinux_root_relative attribute must have value"
897 " \"yes\" or \"no\"");
904 else if (element_type == ELEMENT_POLICY)
910 const char *at_console;
912 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
918 e->d.policy.type = POLICY_IGNORED;
920 if (!locate_attributes (parser, "policy",
927 "at_console", &at_console,
931 if (((context && user) ||
932 (context && group) ||
933 (context && at_console)) ||
935 (user && at_console)) ||
936 (group && at_console) ||
937 !(context || user || group || at_console))
939 dbus_set_error (error, DBUS_ERROR_FAILED,
940 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
946 if (strcmp (context, "default") == 0)
948 e->d.policy.type = POLICY_DEFAULT;
950 else if (strcmp (context, "mandatory") == 0)
952 e->d.policy.type = POLICY_MANDATORY;
956 dbus_set_error (error, DBUS_ERROR_FAILED,
957 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
962 else if (user != NULL)
965 _dbus_string_init_const (&username, user);
967 if (_dbus_parse_unix_user_from_config (&username,
968 &e->d.policy.gid_uid_or_at_console))
969 e->d.policy.type = POLICY_USER;
971 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
974 else if (group != NULL)
976 DBusString group_name;
977 _dbus_string_init_const (&group_name, group);
979 if (_dbus_parse_unix_group_from_config (&group_name,
980 &e->d.policy.gid_uid_or_at_console))
981 e->d.policy.type = POLICY_GROUP;
983 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
986 else if (at_console != NULL)
989 t = (strcmp (at_console, "true") == 0);
990 if (t || strcmp (at_console, "false") == 0)
992 e->d.policy.gid_uid_or_at_console = t;
993 e->d.policy.type = POLICY_CONSOLE;
997 dbus_set_error (error, DBUS_ERROR_FAILED,
998 "Unknown value \"%s\" for at_console in message bus configuration file",
1006 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
1011 else if (element_type == ELEMENT_LIMIT)
1016 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1018 BUS_SET_OOM (error);
1022 if (!locate_attributes (parser, "limit",
1032 dbus_set_error (error, DBUS_ERROR_FAILED,
1033 "<limit> element must have a \"name\" attribute");
1037 e->d.limit.name = _dbus_strdup (name);
1038 if (e->d.limit.name == NULL)
1040 BUS_SET_OOM (error);
1046 else if (element_type == ELEMENT_SELINUX)
1048 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1051 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1053 BUS_SET_OOM (error);
1061 dbus_set_error (error, DBUS_ERROR_FAILED,
1062 "Element <%s> not allowed inside <%s> in configuration file",
1063 element_name, "busconfig");
1069 append_rule_from_element (BusConfigParser *parser,
1070 const char *element_name,
1071 const char **attribute_names,
1072 const char **attribute_values,
1076 const char *send_interface;
1077 const char *send_member;
1078 const char *send_error;
1079 const char *send_destination;
1080 const char *send_path;
1081 const char *send_type;
1082 const char *receive_interface;
1083 const char *receive_member;
1084 const char *receive_error;
1085 const char *receive_sender;
1086 const char *receive_path;
1087 const char *receive_type;
1088 const char *eavesdrop;
1089 const char *send_requested_reply;
1090 const char *receive_requested_reply;
1095 BusPolicyRule *rule;
1097 if (!locate_attributes (parser, element_name,
1101 "send_interface", &send_interface,
1102 "send_member", &send_member,
1103 "send_error", &send_error,
1104 "send_destination", &send_destination,
1105 "send_path", &send_path,
1106 "send_type", &send_type,
1107 "receive_interface", &receive_interface,
1108 "receive_member", &receive_member,
1109 "receive_error", &receive_error,
1110 "receive_sender", &receive_sender,
1111 "receive_path", &receive_path,
1112 "receive_type", &receive_type,
1113 "eavesdrop", &eavesdrop,
1114 "send_requested_reply", &send_requested_reply,
1115 "receive_requested_reply", &receive_requested_reply,
1122 if (!(send_interface || send_member || send_error || send_destination ||
1123 send_type || send_path ||
1124 receive_interface || receive_member || receive_error || receive_sender ||
1125 receive_type || receive_path || eavesdrop ||
1126 send_requested_reply || receive_requested_reply ||
1127 own || user || group))
1129 dbus_set_error (error, DBUS_ERROR_FAILED,
1130 "Element <%s> must have one or more attributes",
1135 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1136 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1138 dbus_set_error (error, DBUS_ERROR_FAILED,
1139 "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.",
1144 /* Allowed combinations of elements are:
1146 * base, must be all send or all receive:
1149 * interface + member
1152 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1153 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1155 * user, group, own must occur alone
1157 * Pretty sure the below stuff is broken, FIXME think about it more.
1160 if (((send_interface && send_error) ||
1161 (send_interface && receive_interface) ||
1162 (send_interface && receive_member) ||
1163 (send_interface && receive_error) ||
1164 (send_interface && receive_sender) ||
1165 (send_interface && receive_requested_reply) ||
1166 (send_interface && own) ||
1167 (send_interface && user) ||
1168 (send_interface && group)) ||
1170 ((send_member && send_error) ||
1171 (send_member && receive_interface) ||
1172 (send_member && receive_member) ||
1173 (send_member && receive_error) ||
1174 (send_member && receive_sender) ||
1175 (send_member && receive_requested_reply) ||
1176 (send_member && own) ||
1177 (send_member && user) ||
1178 (send_member && group)) ||
1180 ((send_error && receive_interface) ||
1181 (send_error && receive_member) ||
1182 (send_error && receive_error) ||
1183 (send_error && receive_sender) ||
1184 (send_error && receive_requested_reply) ||
1185 (send_error && own) ||
1186 (send_error && user) ||
1187 (send_error && group)) ||
1189 ((send_destination && receive_interface) ||
1190 (send_destination && receive_member) ||
1191 (send_destination && receive_error) ||
1192 (send_destination && receive_sender) ||
1193 (send_destination && receive_requested_reply) ||
1194 (send_destination && own) ||
1195 (send_destination && user) ||
1196 (send_destination && group)) ||
1198 ((send_type && receive_interface) ||
1199 (send_type && receive_member) ||
1200 (send_type && receive_error) ||
1201 (send_type && receive_sender) ||
1202 (send_type && receive_requested_reply) ||
1203 (send_type && own) ||
1204 (send_type && user) ||
1205 (send_type && group)) ||
1207 ((send_path && receive_interface) ||
1208 (send_path && receive_member) ||
1209 (send_path && receive_error) ||
1210 (send_path && receive_sender) ||
1211 (send_path && receive_requested_reply) ||
1212 (send_path && own) ||
1213 (send_path && user) ||
1214 (send_path && group)) ||
1216 ((send_requested_reply && receive_interface) ||
1217 (send_requested_reply && receive_member) ||
1218 (send_requested_reply && receive_error) ||
1219 (send_requested_reply && receive_sender) ||
1220 (send_requested_reply && receive_requested_reply) ||
1221 (send_requested_reply && own) ||
1222 (send_requested_reply && user) ||
1223 (send_requested_reply && group)) ||
1225 ((receive_interface && receive_error) ||
1226 (receive_interface && own) ||
1227 (receive_interface && user) ||
1228 (receive_interface && group)) ||
1230 ((receive_member && receive_error) ||
1231 (receive_member && own) ||
1232 (receive_member && user) ||
1233 (receive_member && group)) ||
1235 ((receive_error && own) ||
1236 (receive_error && user) ||
1237 (receive_error && group)) ||
1239 ((eavesdrop && own) ||
1240 (eavesdrop && user) ||
1241 (eavesdrop && group)) ||
1243 ((receive_requested_reply && own) ||
1244 (receive_requested_reply && user) ||
1245 (receive_requested_reply && group)) ||
1252 dbus_set_error (error, DBUS_ERROR_FAILED,
1253 "Invalid combination of attributes on element <%s>",
1260 /* In BusPolicyRule, NULL represents wildcard.
1261 * In the config file, '*' represents it.
1263 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1265 if (send_interface || send_member || send_error || send_destination ||
1266 send_path || send_type || send_requested_reply)
1270 if (IS_WILDCARD (send_interface))
1271 send_interface = NULL;
1272 if (IS_WILDCARD (send_member))
1274 if (IS_WILDCARD (send_error))
1276 if (IS_WILDCARD (send_destination))
1277 send_destination = NULL;
1278 if (IS_WILDCARD (send_path))
1280 if (IS_WILDCARD (send_type))
1283 message_type = DBUS_MESSAGE_TYPE_INVALID;
1284 if (send_type != NULL)
1286 message_type = dbus_message_type_from_string (send_type);
1287 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1289 dbus_set_error (error, DBUS_ERROR_FAILED,
1290 "Bad message type \"%s\"",
1297 !(strcmp (eavesdrop, "true") == 0 ||
1298 strcmp (eavesdrop, "false") == 0))
1300 dbus_set_error (error, DBUS_ERROR_FAILED,
1301 "Bad value \"%s\" for %s attribute, must be true or false",
1302 "eavesdrop", eavesdrop);
1306 if (send_requested_reply &&
1307 !(strcmp (send_requested_reply, "true") == 0 ||
1308 strcmp (send_requested_reply, "false") == 0))
1310 dbus_set_error (error, DBUS_ERROR_FAILED,
1311 "Bad value \"%s\" for %s attribute, must be true or false",
1312 "send_requested_reply", send_requested_reply);
1316 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1321 rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1323 if (send_requested_reply)
1324 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1326 rule->d.send.message_type = message_type;
1327 rule->d.send.path = _dbus_strdup (send_path);
1328 rule->d.send.interface = _dbus_strdup (send_interface);
1329 rule->d.send.member = _dbus_strdup (send_member);
1330 rule->d.send.error = _dbus_strdup (send_error);
1331 rule->d.send.destination = _dbus_strdup (send_destination);
1332 if (send_path && rule->d.send.path == NULL)
1334 if (send_interface && rule->d.send.interface == NULL)
1336 if (send_member && rule->d.send.member == NULL)
1338 if (send_error && rule->d.send.error == NULL)
1340 if (send_destination && rule->d.send.destination == NULL)
1343 else if (receive_interface || receive_member || receive_error || receive_sender ||
1344 receive_path || receive_type || eavesdrop || receive_requested_reply)
1348 if (IS_WILDCARD (receive_interface))
1349 receive_interface = NULL;
1350 if (IS_WILDCARD (receive_member))
1351 receive_member = NULL;
1352 if (IS_WILDCARD (receive_error))
1353 receive_error = NULL;
1354 if (IS_WILDCARD (receive_sender))
1355 receive_sender = NULL;
1356 if (IS_WILDCARD (receive_path))
1357 receive_path = NULL;
1358 if (IS_WILDCARD (receive_type))
1359 receive_type = NULL;
1361 message_type = DBUS_MESSAGE_TYPE_INVALID;
1362 if (receive_type != NULL)
1364 message_type = dbus_message_type_from_string (receive_type);
1365 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1367 dbus_set_error (error, DBUS_ERROR_FAILED,
1368 "Bad message type \"%s\"",
1376 !(strcmp (eavesdrop, "true") == 0 ||
1377 strcmp (eavesdrop, "false") == 0))
1379 dbus_set_error (error, DBUS_ERROR_FAILED,
1380 "Bad value \"%s\" for %s attribute, must be true or false",
1381 "eavesdrop", eavesdrop);
1385 if (receive_requested_reply &&
1386 !(strcmp (receive_requested_reply, "true") == 0 ||
1387 strcmp (receive_requested_reply, "false") == 0))
1389 dbus_set_error (error, DBUS_ERROR_FAILED,
1390 "Bad value \"%s\" for %s attribute, must be true or false",
1391 "receive_requested_reply", receive_requested_reply);
1395 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1400 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1402 if (receive_requested_reply)
1403 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1405 rule->d.receive.message_type = message_type;
1406 rule->d.receive.path = _dbus_strdup (receive_path);
1407 rule->d.receive.interface = _dbus_strdup (receive_interface);
1408 rule->d.receive.member = _dbus_strdup (receive_member);
1409 rule->d.receive.error = _dbus_strdup (receive_error);
1410 rule->d.receive.origin = _dbus_strdup (receive_sender);
1412 if (receive_path && rule->d.receive.path == NULL)
1414 if (receive_interface && rule->d.receive.interface == NULL)
1416 if (receive_member && rule->d.receive.member == NULL)
1418 if (receive_error && rule->d.receive.error == NULL)
1420 if (receive_sender && rule->d.receive.origin == NULL)
1425 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1429 if (IS_WILDCARD (own))
1432 rule->d.own.service_name = _dbus_strdup (own);
1433 if (own && rule->d.own.service_name == NULL)
1438 if (IS_WILDCARD (user))
1440 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1444 rule->d.user.uid = DBUS_UID_UNSET;
1448 DBusString username;
1451 _dbus_string_init_const (&username, user);
1453 if (_dbus_parse_unix_user_from_config (&username, &uid))
1455 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1459 rule->d.user.uid = uid;
1463 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1464 user, element_name);
1470 if (IS_WILDCARD (group))
1472 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1476 rule->d.group.gid = DBUS_GID_UNSET;
1480 DBusString groupname;
1483 _dbus_string_init_const (&groupname, group);
1485 if (_dbus_parse_unix_group_from_config (&groupname, &gid))
1487 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1491 rule->d.group.gid = gid;
1495 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1496 group, element_name);
1501 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1507 pe = peek_element (parser);
1508 _dbus_assert (pe != NULL);
1509 _dbus_assert (pe->type == ELEMENT_POLICY);
1511 switch (pe->d.policy.type)
1513 case POLICY_IGNORED:
1514 /* drop the rule on the floor */
1517 case POLICY_DEFAULT:
1518 if (!bus_policy_append_default_rule (parser->policy, rule))
1521 case POLICY_MANDATORY:
1522 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1526 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1528 dbus_set_error (error, DBUS_ERROR_FAILED,
1529 "<%s> rule cannot be per-user because it has bus-global semantics",
1534 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1539 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1541 dbus_set_error (error, DBUS_ERROR_FAILED,
1542 "<%s> rule cannot be per-group because it has bus-global semantics",
1547 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1553 case POLICY_CONSOLE:
1554 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1560 bus_policy_rule_unref (rule);
1567 BUS_SET_OOM (error);
1570 bus_policy_rule_unref (rule);
1575 start_policy_child (BusConfigParser *parser,
1576 const char *element_name,
1577 const char **attribute_names,
1578 const char **attribute_values,
1581 if (strcmp (element_name, "allow") == 0)
1583 if (!append_rule_from_element (parser, element_name,
1584 attribute_names, attribute_values,
1588 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1590 BUS_SET_OOM (error);
1596 else if (strcmp (element_name, "deny") == 0)
1598 if (!append_rule_from_element (parser, element_name,
1599 attribute_names, attribute_values,
1603 if (push_element (parser, ELEMENT_DENY) == NULL)
1605 BUS_SET_OOM (error);
1613 dbus_set_error (error, DBUS_ERROR_FAILED,
1614 "Element <%s> not allowed inside <%s> in configuration file",
1615 element_name, "policy");
1621 start_selinux_child (BusConfigParser *parser,
1622 const char *element_name,
1623 const char **attribute_names,
1624 const char **attribute_values,
1631 context_copy = NULL;
1633 if (strcmp (element_name, "associate") == 0)
1636 const char *context;
1638 if (!locate_attributes (parser, "associate",
1643 "context", &context,
1647 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1649 BUS_SET_OOM (error);
1653 if (own == NULL || context == NULL)
1655 dbus_set_error (error, DBUS_ERROR_FAILED,
1656 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1660 own_copy = _dbus_strdup (own);
1661 if (own_copy == NULL)
1663 context_copy = _dbus_strdup (context);
1664 if (context_copy == NULL)
1667 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1668 own_copy, context_copy))
1675 dbus_set_error (error, DBUS_ERROR_FAILED,
1676 "Element <%s> not allowed inside <%s> in configuration file",
1677 element_name, "selinux");
1683 dbus_free (own_copy);
1686 dbus_free (context_copy);
1688 BUS_SET_OOM (error);
1693 bus_config_parser_start_element (BusConfigParser *parser,
1694 const char *element_name,
1695 const char **attribute_names,
1696 const char **attribute_values,
1701 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1703 /* printf ("START: %s\n", element_name); */
1705 t = top_element_type (parser);
1707 if (t == ELEMENT_NONE)
1709 if (strcmp (element_name, "busconfig") == 0)
1711 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1714 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1716 BUS_SET_OOM (error);
1724 dbus_set_error (error, DBUS_ERROR_FAILED,
1725 "Unknown element <%s> at root of configuration file",
1730 else if (t == ELEMENT_BUSCONFIG)
1732 return start_busconfig_child (parser, element_name,
1733 attribute_names, attribute_values,
1736 else if (t == ELEMENT_POLICY)
1738 return start_policy_child (parser, element_name,
1739 attribute_names, attribute_values,
1742 else if (t == ELEMENT_SELINUX)
1744 return start_selinux_child (parser, element_name,
1745 attribute_names, attribute_values,
1750 dbus_set_error (error, DBUS_ERROR_FAILED,
1751 "Element <%s> is not allowed in this context",
1758 set_limit (BusConfigParser *parser,
1763 dbus_bool_t must_be_positive;
1764 dbus_bool_t must_be_int;
1766 must_be_int = FALSE;
1767 must_be_positive = FALSE;
1769 if (strcmp (name, "max_incoming_bytes") == 0)
1771 must_be_positive = TRUE;
1772 parser->limits.max_incoming_bytes = value;
1774 else if (strcmp (name, "max_outgoing_bytes") == 0)
1776 must_be_positive = TRUE;
1777 parser->limits.max_outgoing_bytes = value;
1779 else if (strcmp (name, "max_message_size") == 0)
1781 must_be_positive = TRUE;
1782 parser->limits.max_message_size = value;
1784 else if (strcmp (name, "service_start_timeout") == 0)
1786 must_be_positive = TRUE;
1788 parser->limits.activation_timeout = value;
1790 else if (strcmp (name, "auth_timeout") == 0)
1792 must_be_positive = TRUE;
1794 parser->limits.auth_timeout = value;
1796 else if (strcmp (name, "reply_timeout") == 0)
1798 must_be_positive = TRUE;
1800 parser->limits.reply_timeout = value;
1802 else if (strcmp (name, "max_completed_connections") == 0)
1804 must_be_positive = TRUE;
1806 parser->limits.max_completed_connections = value;
1808 else if (strcmp (name, "max_incomplete_connections") == 0)
1810 must_be_positive = TRUE;
1812 parser->limits.max_incomplete_connections = value;
1814 else if (strcmp (name, "max_connections_per_user") == 0)
1816 must_be_positive = TRUE;
1818 parser->limits.max_connections_per_user = value;
1820 else if (strcmp (name, "max_pending_service_starts") == 0)
1822 must_be_positive = TRUE;
1824 parser->limits.max_pending_activations = value;
1826 else if (strcmp (name, "max_names_per_connection") == 0)
1828 must_be_positive = TRUE;
1830 parser->limits.max_services_per_connection = value;
1832 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1834 must_be_positive = TRUE;
1836 parser->limits.max_match_rules_per_connection = value;
1838 else if (strcmp (name, "max_replies_per_connection") == 0)
1840 must_be_positive = TRUE;
1842 parser->limits.max_replies_per_connection = value;
1846 dbus_set_error (error, DBUS_ERROR_FAILED,
1847 "There is no limit called \"%s\"\n",
1852 if (must_be_positive && value < 0)
1854 dbus_set_error (error, DBUS_ERROR_FAILED,
1855 "<limit name=\"%s\"> must be a positive number\n",
1861 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1863 dbus_set_error (error, DBUS_ERROR_FAILED,
1864 "<limit name=\"%s\"> value is too large\n",
1873 bus_config_parser_end_element (BusConfigParser *parser,
1874 const char *element_name,
1881 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1883 /* printf ("END: %s\n", element_name); */
1885 t = top_element_type (parser);
1887 if (t == ELEMENT_NONE)
1889 /* should probably be an assertion failure but
1890 * being paranoid about XML parsers
1892 dbus_set_error (error, DBUS_ERROR_FAILED,
1893 "XML parser ended element with no element on the stack");
1897 n = bus_config_parser_element_type_to_name (t);
1898 _dbus_assert (n != NULL);
1899 if (strcmp (n, element_name) != 0)
1901 /* should probably be an assertion failure but
1902 * being paranoid about XML parsers
1904 dbus_set_error (error, DBUS_ERROR_FAILED,
1905 "XML element <%s> ended but topmost element on the stack was <%s>",
1910 e = peek_element (parser);
1911 _dbus_assert (e != NULL);
1916 _dbus_assert_not_reached ("element in stack has no type");
1919 case ELEMENT_INCLUDE:
1922 case ELEMENT_LISTEN:
1923 case ELEMENT_PIDFILE:
1925 case ELEMENT_SERVICEDIR:
1926 case ELEMENT_SERVICEHELPER:
1927 case ELEMENT_INCLUDEDIR:
1929 if (!e->had_content)
1931 dbus_set_error (error, DBUS_ERROR_FAILED,
1932 "XML element <%s> was expected to have content inside it",
1933 bus_config_parser_element_type_to_name (e->type));
1937 if (e->type == ELEMENT_LIMIT)
1939 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1945 case ELEMENT_BUSCONFIG:
1946 case ELEMENT_POLICY:
1950 case ELEMENT_SELINUX:
1951 case ELEMENT_ASSOCIATE:
1952 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
1953 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
1957 pop_element (parser);
1963 all_whitespace (const DBusString *str)
1967 _dbus_string_skip_white (str, 0, &i);
1969 return i == _dbus_string_get_length (str);
1973 make_full_path (const DBusString *basedir,
1974 const DBusString *filename,
1975 DBusString *full_path)
1977 if (_dbus_path_is_absolute (filename))
1979 return _dbus_string_copy (filename, 0, full_path, 0);
1983 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1986 if (!_dbus_concat_dir_and_file (full_path, filename))
1994 include_file (BusConfigParser *parser,
1995 const DBusString *filename,
1996 dbus_bool_t ignore_missing,
1999 /* FIXME good test case for this would load each config file in the
2000 * test suite both alone, and as an include, and check
2001 * that the result is the same
2003 BusConfigParser *included;
2004 const char *filename_str;
2005 DBusError tmp_error;
2007 dbus_error_init (&tmp_error);
2009 filename_str = _dbus_string_get_const_data (filename);
2011 /* Check to make sure this file hasn't already been included. */
2012 if (seen_include (parser, filename))
2014 dbus_set_error (error, DBUS_ERROR_FAILED,
2015 "Circular inclusion of file '%s'",
2020 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2022 BUS_SET_OOM (error);
2026 /* Since parser is passed in as the parent, included
2027 inherits parser's limits. */
2028 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2030 _dbus_list_pop_last (&parser->included_files);
2032 if (included == NULL)
2034 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2036 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2039 dbus_error_free (&tmp_error);
2044 dbus_move_error (&tmp_error, error);
2050 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2052 if (!merge_included (parser, included, error))
2054 bus_config_parser_unref (included);
2058 /* Copy included's limits back to parser. */
2059 parser->limits = included->limits;
2061 bus_config_parser_unref (included);
2067 servicehelper_path (BusConfigParser *parser,
2068 const DBusString *filename,
2071 const char *filename_str;
2072 char *servicehelper;
2074 filename_str = _dbus_string_get_const_data (filename);
2076 /* copy to avoid overwriting with NULL on OOM */
2077 servicehelper = _dbus_strdup (filename_str);
2080 if (servicehelper == NULL)
2082 BUS_SET_OOM (error);
2086 /* save the latest servicehelper only if not OOM */
2087 dbus_free (parser->servicehelper);
2088 parser->servicehelper = servicehelper;
2090 /* We don't check whether the helper exists; instead we
2091 * would just fail to ever activate anything if it doesn't.
2092 * This allows an admin to fix the problem if it doesn't exist.
2093 * It also allows the parser test suite to successfully parse
2094 * test cases without installing the helper. ;-)
2101 include_dir (BusConfigParser *parser,
2102 const DBusString *dirname,
2105 DBusString filename;
2107 DBusError tmp_error;
2111 if (!_dbus_string_init (&filename))
2113 BUS_SET_OOM (error);
2119 dir = _dbus_directory_open (dirname, error);
2124 dbus_error_init (&tmp_error);
2125 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2127 DBusString full_path;
2129 if (!_dbus_string_init (&full_path))
2131 BUS_SET_OOM (error);
2135 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2137 BUS_SET_OOM (error);
2138 _dbus_string_free (&full_path);
2142 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2144 BUS_SET_OOM (error);
2145 _dbus_string_free (&full_path);
2149 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2151 if (!include_file (parser, &full_path, TRUE, error))
2153 _dbus_string_free (&full_path);
2158 _dbus_string_free (&full_path);
2161 if (dbus_error_is_set (&tmp_error))
2163 dbus_move_error (&tmp_error, error);
2168 if (!_dbus_string_copy_data (dirname, &s))
2170 BUS_SET_OOM (error);
2174 if (!_dbus_list_append (&parser->conf_dirs, s))
2177 BUS_SET_OOM (error);
2184 _dbus_string_free (&filename);
2187 _dbus_directory_close (dir);
2193 bus_config_parser_content (BusConfigParser *parser,
2194 const DBusString *content,
2199 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2205 _dbus_string_get_const_data (content, &c_str);
2207 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2211 e = peek_element (parser);
2214 dbus_set_error (error, DBUS_ERROR_FAILED,
2215 "Text content outside of any XML element in configuration file");
2218 else if (e->had_content)
2220 _dbus_assert_not_reached ("Element had multiple content blocks");
2224 switch (top_element_type (parser))
2227 _dbus_assert_not_reached ("element at top of stack has no type");
2230 case ELEMENT_BUSCONFIG:
2231 case ELEMENT_POLICY:
2235 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2236 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2237 case ELEMENT_SELINUX:
2238 case ELEMENT_ASSOCIATE:
2239 if (all_whitespace (content))
2243 dbus_set_error (error, DBUS_ERROR_FAILED,
2244 "No text content expected inside XML element %s in configuration file",
2245 bus_config_parser_element_type_to_name (top_element_type (parser)));
2249 case ELEMENT_PIDFILE:
2253 e->had_content = TRUE;
2255 if (!_dbus_string_copy_data (content, &s))
2258 dbus_free (parser->pidfile);
2259 parser->pidfile = s;
2263 case ELEMENT_INCLUDE:
2265 DBusString full_path, selinux_policy_root;
2267 e->had_content = TRUE;
2269 if (e->d.include.if_selinux_enabled
2270 && !bus_selinux_enabled ())
2273 if (!_dbus_string_init (&full_path))
2276 if (e->d.include.selinux_root_relative)
2278 if (!bus_selinux_get_policy_root ())
2280 dbus_set_error (error, DBUS_ERROR_FAILED,
2281 "Could not determine SELinux policy root for relative inclusion");
2282 _dbus_string_free (&full_path);
2285 _dbus_string_init_const (&selinux_policy_root,
2286 bus_selinux_get_policy_root ());
2287 if (!make_full_path (&selinux_policy_root, content, &full_path))
2289 _dbus_string_free (&full_path);
2293 else if (!make_full_path (&parser->basedir, content, &full_path))
2295 _dbus_string_free (&full_path);
2299 if (!include_file (parser, &full_path,
2300 e->d.include.ignore_missing, error))
2302 _dbus_string_free (&full_path);
2306 _dbus_string_free (&full_path);
2310 case ELEMENT_SERVICEHELPER:
2312 DBusString full_path;
2314 e->had_content = TRUE;
2316 if (!_dbus_string_init (&full_path))
2319 if (!make_full_path (&parser->basedir, content, &full_path))
2321 _dbus_string_free (&full_path);
2325 if (!servicehelper_path (parser, &full_path, error))
2327 _dbus_string_free (&full_path);
2331 _dbus_string_free (&full_path);
2335 case ELEMENT_INCLUDEDIR:
2337 DBusString full_path;
2339 e->had_content = TRUE;
2341 if (!_dbus_string_init (&full_path))
2344 if (!make_full_path (&parser->basedir, content, &full_path))
2346 _dbus_string_free (&full_path);
2350 if (!include_dir (parser, &full_path, error))
2352 _dbus_string_free (&full_path);
2356 _dbus_string_free (&full_path);
2364 e->had_content = TRUE;
2366 if (!_dbus_string_copy_data (content, &s))
2369 dbus_free (parser->user);
2378 e->had_content = TRUE;
2380 if (!_dbus_string_copy_data (content, &s))
2383 dbus_free (parser->bus_type);
2384 parser->bus_type = s;
2388 case ELEMENT_LISTEN:
2392 e->had_content = TRUE;
2394 if (!_dbus_string_copy_data (content, &s))
2397 if (!_dbus_list_append (&parser->listen_on,
2410 e->had_content = TRUE;
2412 if (!_dbus_string_copy_data (content, &s))
2415 if (!_dbus_list_append (&parser->mechanisms,
2424 case ELEMENT_SERVICEDIR:
2427 DBusString full_path;
2429 e->had_content = TRUE;
2431 if (!_dbus_string_init (&full_path))
2434 if (!make_full_path (&parser->basedir, content, &full_path))
2436 _dbus_string_free (&full_path);
2440 if (!_dbus_string_copy_data (&full_path, &s))
2442 _dbus_string_free (&full_path);
2446 /* _only_ extra session directories can be specified */
2447 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2449 _dbus_string_free (&full_path);
2454 _dbus_string_free (&full_path);
2462 e->had_content = TRUE;
2465 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2467 dbus_set_error (error, DBUS_ERROR_FAILED,
2468 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2473 e->d.limit.value = val;
2475 _dbus_verbose ("Loaded value %ld for limit %s\n",
2482 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2486 BUS_SET_OOM (error);
2491 bus_config_parser_finished (BusConfigParser *parser,
2494 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2496 if (parser->stack != NULL)
2498 dbus_set_error (error, DBUS_ERROR_FAILED,
2499 "Element <%s> was not closed in configuration file",
2500 bus_config_parser_element_type_to_name (top_element_type (parser)));
2505 if (parser->is_toplevel && parser->listen_on == NULL)
2507 dbus_set_error (error, DBUS_ERROR_FAILED,
2508 "Configuration file needs one or more <listen> elements giving addresses");
2516 bus_config_parser_get_user (BusConfigParser *parser)
2518 return parser->user;
2522 bus_config_parser_get_type (BusConfigParser *parser)
2524 return parser->bus_type;
2528 bus_config_parser_get_addresses (BusConfigParser *parser)
2530 return &parser->listen_on;
2534 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2536 return &parser->mechanisms;
2540 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2542 return &parser->service_dirs;
2546 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2548 return &parser->conf_dirs;
2552 bus_config_parser_get_fork (BusConfigParser *parser)
2554 return parser->fork;
2558 bus_config_parser_get_pidfile (BusConfigParser *parser)
2560 return parser->pidfile;
2564 bus_config_parser_get_servicehelper (BusConfigParser *parser)
2566 return parser->servicehelper;
2570 bus_config_parser_steal_policy (BusConfigParser *parser)
2574 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2576 policy = parser->policy;
2578 parser->policy = NULL;
2583 /* Overwrite any limits that were set in the configuration file */
2585 bus_config_parser_get_limits (BusConfigParser *parser,
2588 *limits = parser->limits;
2592 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2594 DBusHashTable *table;
2596 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2598 table = parser->service_context_table;
2600 parser->service_context_table = NULL;
2605 #ifdef DBUS_BUILD_TESTS
2616 do_load (const DBusString *full_path,
2618 dbus_bool_t oom_possible)
2620 BusConfigParser *parser;
2623 dbus_error_init (&error);
2625 parser = bus_config_load (full_path, TRUE, NULL, &error);
2628 _DBUS_ASSERT_ERROR_IS_SET (&error);
2631 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2633 _dbus_verbose ("Failed to load valid file due to OOM\n");
2634 dbus_error_free (&error);
2637 else if (validity == VALID)
2639 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2642 dbus_error_free (&error);
2647 dbus_error_free (&error);
2653 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2655 bus_config_parser_unref (parser);
2657 if (validity == INVALID)
2659 _dbus_warn ("Accepted invalid file\n");
2669 const DBusString *full_path;
2674 check_loader_oom_func (void *data)
2676 LoaderOomData *d = data;
2678 return do_load (d->full_path, d->validity, TRUE);
2682 process_test_valid_subdir (const DBusString *test_base_dir,
2686 DBusString test_directory;
2687 DBusString filename;
2695 if (!_dbus_string_init (&test_directory))
2696 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2698 _dbus_string_init_const (&filename, subdir);
2700 if (!_dbus_string_copy (test_base_dir, 0,
2701 &test_directory, 0))
2702 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2704 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2705 _dbus_assert_not_reached ("couldn't allocate full path");
2707 _dbus_string_free (&filename);
2708 if (!_dbus_string_init (&filename))
2709 _dbus_assert_not_reached ("didn't allocate filename string\n");
2711 dbus_error_init (&error);
2712 dir = _dbus_directory_open (&test_directory, &error);
2715 _dbus_warn ("Could not open %s: %s\n",
2716 _dbus_string_get_const_data (&test_directory),
2718 dbus_error_free (&error);
2722 if (validity == VALID)
2723 printf ("Testing valid files:\n");
2724 else if (validity == INVALID)
2725 printf ("Testing invalid files:\n");
2727 printf ("Testing unknown files:\n");
2730 while (_dbus_directory_get_next_file (dir, &filename, &error))
2732 DBusString full_path;
2735 if (!_dbus_string_init (&full_path))
2736 _dbus_assert_not_reached ("couldn't init string");
2738 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2739 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2741 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2742 _dbus_assert_not_reached ("couldn't concat file to dir");
2744 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2746 _dbus_verbose ("Skipping non-.conf file %s\n",
2747 _dbus_string_get_const_data (&filename));
2748 _dbus_string_free (&full_path);
2752 printf (" %s\n", _dbus_string_get_const_data (&filename));
2754 _dbus_verbose (" expecting %s\n",
2755 validity == VALID ? "valid" :
2756 (validity == INVALID ? "invalid" :
2757 (validity == UNKNOWN ? "unknown" : "???")));
2759 d.full_path = &full_path;
2760 d.validity = validity;
2762 /* FIXME hackaround for an expat problem, see
2763 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2764 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2766 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2767 if (!check_loader_oom_func (&d))
2768 _dbus_assert_not_reached ("test failed");
2770 _dbus_string_free (&full_path);
2773 if (dbus_error_is_set (&error))
2775 _dbus_warn ("Could not get next file in %s: %s\n",
2776 _dbus_string_get_const_data (&test_directory),
2778 dbus_error_free (&error);
2787 _dbus_directory_close (dir);
2788 _dbus_string_free (&test_directory);
2789 _dbus_string_free (&filename);
2795 bools_equal (dbus_bool_t a,
2802 strings_equal_or_both_null (const char *a,
2805 if (a == NULL || b == NULL)
2808 return !strcmp (a, b);
2812 elements_equal (const Element *a,
2815 if (a->type != b->type)
2818 if (!bools_equal (a->had_content, b->had_content))
2824 case ELEMENT_INCLUDE:
2825 if (!bools_equal (a->d.include.ignore_missing,
2826 b->d.include.ignore_missing))
2830 case ELEMENT_POLICY:
2831 if (a->d.policy.type != b->d.policy.type)
2833 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
2838 if (strcmp (a->d.limit.name, b->d.limit.name))
2840 if (a->d.limit.value != b->d.limit.value)
2854 lists_of_elements_equal (DBusList *a,
2863 while (ia != NULL && ib != NULL)
2865 if (elements_equal (ia->data, ib->data))
2867 ia = _dbus_list_get_next_link (&a, ia);
2868 ib = _dbus_list_get_next_link (&b, ib);
2871 return ia == NULL && ib == NULL;
2875 lists_of_c_strings_equal (DBusList *a,
2884 while (ia != NULL && ib != NULL)
2886 if (strcmp (ia->data, ib->data))
2888 ia = _dbus_list_get_next_link (&a, ia);
2889 ib = _dbus_list_get_next_link (&b, ib);
2892 return ia == NULL && ib == NULL;
2896 limits_equal (const BusLimits *a,
2900 (a->max_incoming_bytes == b->max_incoming_bytes
2901 || a->max_outgoing_bytes == b->max_outgoing_bytes
2902 || a->max_message_size == b->max_message_size
2903 || a->activation_timeout == b->activation_timeout
2904 || a->auth_timeout == b->auth_timeout
2905 || a->max_completed_connections == b->max_completed_connections
2906 || a->max_incomplete_connections == b->max_incomplete_connections
2907 || a->max_connections_per_user == b->max_connections_per_user
2908 || a->max_pending_activations == b->max_pending_activations
2909 || a->max_services_per_connection == b->max_services_per_connection
2910 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2911 || a->max_replies_per_connection == b->max_replies_per_connection
2912 || a->reply_timeout == b->reply_timeout);
2916 config_parsers_equal (const BusConfigParser *a,
2917 const BusConfigParser *b)
2919 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2922 if (!lists_of_elements_equal (a->stack, b->stack))
2925 if (!strings_equal_or_both_null (a->user, b->user))
2928 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2931 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2934 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2937 /* FIXME: compare policy */
2939 /* FIXME: compare service selinux ID table */
2941 if (! limits_equal (&a->limits, &b->limits))
2944 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2947 if (! bools_equal (a->fork, b->fork))
2950 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2957 all_are_equiv (const DBusString *target_directory)
2959 DBusString filename;
2961 BusConfigParser *first_parser;
2962 BusConfigParser *parser;
2968 first_parser = NULL;
2972 if (!_dbus_string_init (&filename))
2973 _dbus_assert_not_reached ("didn't allocate filename string");
2975 dbus_error_init (&error);
2976 dir = _dbus_directory_open (target_directory, &error);
2979 _dbus_warn ("Could not open %s: %s\n",
2980 _dbus_string_get_const_data (target_directory),
2982 dbus_error_free (&error);
2986 printf ("Comparing equivalent files:\n");
2989 while (_dbus_directory_get_next_file (dir, &filename, &error))
2991 DBusString full_path;
2993 if (!_dbus_string_init (&full_path))
2994 _dbus_assert_not_reached ("couldn't init string");
2996 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2997 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2999 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3000 _dbus_assert_not_reached ("couldn't concat file to dir");
3002 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3004 _dbus_verbose ("Skipping non-.conf file %s\n",
3005 _dbus_string_get_const_data (&filename));
3006 _dbus_string_free (&full_path);
3010 printf (" %s\n", _dbus_string_get_const_data (&filename));
3012 parser = bus_config_load (&full_path, TRUE, NULL, &error);
3016 _dbus_warn ("Could not load file %s: %s\n",
3017 _dbus_string_get_const_data (&full_path),
3019 _dbus_string_free (&full_path);
3020 dbus_error_free (&error);
3023 else if (first_parser == NULL)
3025 _dbus_string_free (&full_path);
3026 first_parser = parser;
3030 _dbus_string_free (&full_path);
3031 equal = config_parsers_equal (first_parser, parser);
3032 bus_config_parser_unref (parser);
3041 _dbus_string_free (&filename);
3043 bus_config_parser_unref (first_parser);
3045 _dbus_directory_close (dir);
3052 process_test_equiv_subdir (const DBusString *test_base_dir,
3055 DBusString test_directory;
3056 DBusString filename;
3065 if (!_dbus_string_init (&test_directory))
3066 _dbus_assert_not_reached ("didn't allocate test_directory");
3068 _dbus_string_init_const (&filename, subdir);
3070 if (!_dbus_string_copy (test_base_dir, 0,
3071 &test_directory, 0))
3072 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3074 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3075 _dbus_assert_not_reached ("couldn't allocate full path");
3077 _dbus_string_free (&filename);
3078 if (!_dbus_string_init (&filename))
3079 _dbus_assert_not_reached ("didn't allocate filename string");
3081 dbus_error_init (&error);
3082 dir = _dbus_directory_open (&test_directory, &error);
3085 _dbus_warn ("Could not open %s: %s\n",
3086 _dbus_string_get_const_data (&test_directory),
3088 dbus_error_free (&error);
3092 while (_dbus_directory_get_next_file (dir, &filename, &error))
3094 DBusString full_path;
3096 /* Skip CVS's magic directories! */
3097 if (_dbus_string_equal_c_str (&filename, "CVS"))
3100 if (!_dbus_string_init (&full_path))
3101 _dbus_assert_not_reached ("couldn't init string");
3103 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3104 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3106 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3107 _dbus_assert_not_reached ("couldn't concat file to dir");
3109 equal = all_are_equiv (&full_path);
3110 _dbus_string_free (&full_path);
3119 _dbus_string_free (&test_directory);
3120 _dbus_string_free (&filename);
3122 _dbus_directory_close (dir);
3128 static const char *test_session_service_dir_matches[] =
3131 "/testusr/testlocal/testshare/dbus-1/services",
3132 "/testusr/testshare/dbus-1/services",
3134 DBUS_DATADIR"/dbus-1/services",
3136 "/testhome/foo/.testlocal/testshare/dbus-1/services",
3142 test_default_session_servicedirs (void)
3147 const char *common_progs;
3150 /* On Unix we don't actually use this variable, but it's easier to handle the
3151 * deallocation if we always allocate it, whether needed or not */
3152 if (!_dbus_string_init (&progs))
3153 _dbus_assert_not_reached ("OOM allocating progs");
3155 common_progs = _dbus_getenv ("CommonProgramFiles");
3159 if (!_dbus_string_append (&progs, common_progs))
3161 _dbus_string_free (&progs);
3165 if (!_dbus_string_append (&progs, "/dbus-1/services"))
3167 _dbus_string_free (&progs);
3170 test_session_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3175 printf ("Testing retrieving the default session service directories\n");
3176 if (!_dbus_get_standard_session_servicedirs (&dirs))
3177 _dbus_assert_not_reached ("couldn't get stardard dirs");
3179 /* make sure our defaults end with share/dbus-1/service */
3180 while ((link = _dbus_list_pop_first_link (&dirs)))
3184 printf (" default service dir: %s\n", (char *)link->data);
3185 _dbus_string_init_const (&path, (char *)link->data);
3186 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/services"))
3188 printf ("error with default session service directories\n");
3189 dbus_free (link->data);
3190 _dbus_list_free_link (link);
3191 _dbus_string_free (&progs);
3195 dbus_free (link->data);
3196 _dbus_list_free_link (link);
3200 if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3201 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3203 if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3204 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3206 if (!_dbus_get_standard_session_servicedirs (&dirs))
3207 _dbus_assert_not_reached ("couldn't get stardard dirs");
3209 /* make sure we read and parse the env variable correctly */
3211 while ((link = _dbus_list_pop_first_link (&dirs)))
3213 printf (" test service dir: %s\n", (char *)link->data);
3214 if (test_session_service_dir_matches[i] == NULL)
3216 printf ("more directories parsed than in match set\n");
3217 dbus_free (link->data);
3218 _dbus_list_free_link (link);
3219 _dbus_string_free (&progs);
3223 if (strcmp (test_session_service_dir_matches[i],
3224 (char *)link->data) != 0)
3226 printf ("%s directory does not match %s in the match set\n",
3228 test_session_service_dir_matches[i]);
3229 dbus_free (link->data);
3230 _dbus_list_free_link (link);
3231 _dbus_string_free (&progs);
3237 dbus_free (link->data);
3238 _dbus_list_free_link (link);
3241 if (test_session_service_dir_matches[i] != NULL)
3243 printf ("extra data %s in the match set was not matched\n",
3244 test_session_service_dir_matches[i]);
3246 _dbus_string_free (&progs);
3250 _dbus_string_free (&progs);
3254 static const char *test_system_service_dir_matches[] =
3257 "/testusr/testlocal/testshare/dbus-1/system-services",
3258 "/testusr/testshare/dbus-1/system-services",
3260 DBUS_DATADIR"/dbus-1/system-services",
3265 test_default_system_servicedirs (void)
3270 const char *common_progs;
3273 /* On Unix we don't actually use this variable, but it's easier to handle the
3274 * deallocation if we always allocate it, whether needed or not */
3275 if (!_dbus_string_init (&progs))
3276 _dbus_assert_not_reached ("OOM allocating progs");
3278 common_progs = _dbus_getenv ("CommonProgramFiles");
3282 if (!_dbus_string_append (&progs, common_progs))
3284 _dbus_string_free (&progs);
3288 if (!_dbus_string_append (&progs, "/dbus-1/system-services"))
3290 _dbus_string_free (&progs);
3293 test_system_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3298 printf ("Testing retrieving the default system service directories\n");
3299 if (!_dbus_get_standard_system_servicedirs (&dirs))
3300 _dbus_assert_not_reached ("couldn't get stardard dirs");
3302 /* make sure our defaults end with share/dbus-1/system-service */
3303 while ((link = _dbus_list_pop_first_link (&dirs)))
3307 printf (" default service dir: %s\n", (char *)link->data);
3308 _dbus_string_init_const (&path, (char *)link->data);
3309 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/system-services"))
3311 printf ("error with default system service directories\n");
3312 dbus_free (link->data);
3313 _dbus_list_free_link (link);
3314 _dbus_string_free (&progs);
3318 dbus_free (link->data);
3319 _dbus_list_free_link (link);
3323 if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3324 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3326 if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3327 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3329 if (!_dbus_get_standard_system_servicedirs (&dirs))
3330 _dbus_assert_not_reached ("couldn't get stardard dirs");
3332 /* make sure we read and parse the env variable correctly */
3334 while ((link = _dbus_list_pop_first_link (&dirs)))
3336 printf (" test service dir: %s\n", (char *)link->data);
3337 if (test_system_service_dir_matches[i] == NULL)
3339 printf ("more directories parsed than in match set\n");
3340 dbus_free (link->data);
3341 _dbus_list_free_link (link);
3342 _dbus_string_free (&progs);
3346 if (strcmp (test_system_service_dir_matches[i],
3347 (char *)link->data) != 0)
3349 printf ("%s directory does not match %s in the match set\n",
3351 test_system_service_dir_matches[i]);
3352 dbus_free (link->data);
3353 _dbus_list_free_link (link);
3354 _dbus_string_free (&progs);
3360 dbus_free (link->data);
3361 _dbus_list_free_link (link);
3364 if (test_system_service_dir_matches[i] != NULL)
3366 printf ("extra data %s in the match set was not matched\n",
3367 test_system_service_dir_matches[i]);
3369 _dbus_string_free (&progs);
3373 _dbus_string_free (&progs);
3378 bus_config_parser_test (const DBusString *test_data_dir)
3380 if (test_data_dir == NULL ||
3381 _dbus_string_get_length (test_data_dir) == 0)
3383 printf ("No test data\n");
3387 if (!test_default_session_servicedirs())
3390 if (!test_default_system_servicedirs())
3393 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3396 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3399 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3405 #endif /* DBUS_BUILD_TESTS */