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**);
589 while (attribute_names[i])
598 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
600 retloc = attrs[j].retloc;
604 dbus_set_error (error, DBUS_ERROR_FAILED,
605 "Attribute \"%s\" repeated twice on the same <%s> element",
606 attrs[j].name, element_name);
611 *retloc = attribute_values[i];
620 dbus_set_error (error, DBUS_ERROR_FAILED,
621 "Attribute \"%s\" is invalid on <%s> element in this context",
622 attribute_names[i], element_name);
635 check_no_attributes (BusConfigParser *parser,
636 const char *element_name,
637 const char **attribute_names,
638 const char **attribute_values,
641 if (attribute_names[0] != NULL)
643 dbus_set_error (error, DBUS_ERROR_FAILED,
644 "Attribute \"%s\" is invalid on <%s> element in this context",
645 attribute_names[0], element_name);
653 start_busconfig_child (BusConfigParser *parser,
654 const char *element_name,
655 const char **attribute_names,
656 const char **attribute_values,
659 ElementType element_type;
661 element_type = bus_config_parser_element_name_to_type (element_name);
663 if (element_type == ELEMENT_USER)
665 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
668 if (push_element (parser, ELEMENT_USER) == NULL)
676 else if (element_type == ELEMENT_TYPE)
678 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
681 if (push_element (parser, ELEMENT_TYPE) == NULL)
689 else if (element_type == ELEMENT_FORK)
691 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
694 if (push_element (parser, ELEMENT_FORK) == NULL)
704 else if (element_type == ELEMENT_PIDFILE)
706 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
709 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
717 else if (element_type == ELEMENT_LISTEN)
719 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
722 if (push_element (parser, ELEMENT_LISTEN) == NULL)
730 else if (element_type == ELEMENT_AUTH)
732 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
735 if (push_element (parser, ELEMENT_AUTH) == NULL)
743 else if (element_type == ELEMENT_SERVICEHELPER)
745 if (!check_no_attributes (parser, "servicehelper", attribute_names, attribute_values, error))
748 if (push_element (parser, ELEMENT_SERVICEHELPER) == NULL)
756 else if (element_type == ELEMENT_INCLUDEDIR)
758 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
761 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
769 else if (element_type == ELEMENT_STANDARD_SESSION_SERVICEDIRS)
775 if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
778 if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
784 if (!_dbus_get_standard_session_servicedirs (&dirs))
790 while ((link = _dbus_list_pop_first_link (&dirs)))
791 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
795 else if (element_type == ELEMENT_STANDARD_SYSTEM_SERVICEDIRS)
801 if (!check_no_attributes (parser, "standard_system_servicedirs", attribute_names, attribute_values, error))
804 if (push_element (parser, ELEMENT_STANDARD_SYSTEM_SERVICEDIRS) == NULL)
810 if (!_dbus_get_standard_system_servicedirs (&dirs))
816 while ((link = _dbus_list_pop_first_link (&dirs)))
817 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
821 else if (element_type == ELEMENT_SERVICEDIR)
823 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
826 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
834 else if (element_type == ELEMENT_INCLUDE)
837 const char *if_selinux_enabled;
838 const char *ignore_missing;
839 const char *selinux_root_relative;
841 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
847 e->d.include.ignore_missing = FALSE;
848 e->d.include.if_selinux_enabled = FALSE;
849 e->d.include.selinux_root_relative = FALSE;
851 if (!locate_attributes (parser, "include",
855 "ignore_missing", &ignore_missing,
856 "if_selinux_enabled", &if_selinux_enabled,
857 "selinux_root_relative", &selinux_root_relative,
861 if (ignore_missing != NULL)
863 if (strcmp (ignore_missing, "yes") == 0)
864 e->d.include.ignore_missing = TRUE;
865 else if (strcmp (ignore_missing, "no") == 0)
866 e->d.include.ignore_missing = FALSE;
869 dbus_set_error (error, DBUS_ERROR_FAILED,
870 "ignore_missing attribute must have value \"yes\" or \"no\"");
875 if (if_selinux_enabled != NULL)
877 if (strcmp (if_selinux_enabled, "yes") == 0)
878 e->d.include.if_selinux_enabled = TRUE;
879 else if (strcmp (if_selinux_enabled, "no") == 0)
880 e->d.include.if_selinux_enabled = FALSE;
883 dbus_set_error (error, DBUS_ERROR_FAILED,
884 "if_selinux_enabled attribute must have value"
885 " \"yes\" or \"no\"");
890 if (selinux_root_relative != NULL)
892 if (strcmp (selinux_root_relative, "yes") == 0)
893 e->d.include.selinux_root_relative = TRUE;
894 else if (strcmp (selinux_root_relative, "no") == 0)
895 e->d.include.selinux_root_relative = FALSE;
898 dbus_set_error (error, DBUS_ERROR_FAILED,
899 "selinux_root_relative attribute must have value"
900 " \"yes\" or \"no\"");
907 else if (element_type == ELEMENT_POLICY)
913 const char *at_console;
915 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
921 e->d.policy.type = POLICY_IGNORED;
923 if (!locate_attributes (parser, "policy",
930 "at_console", &at_console,
934 if (((context && user) ||
935 (context && group) ||
936 (context && at_console)) ||
938 (user && at_console)) ||
939 (group && at_console) ||
940 !(context || user || group || at_console))
942 dbus_set_error (error, DBUS_ERROR_FAILED,
943 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
949 if (strcmp (context, "default") == 0)
951 e->d.policy.type = POLICY_DEFAULT;
953 else if (strcmp (context, "mandatory") == 0)
955 e->d.policy.type = POLICY_MANDATORY;
959 dbus_set_error (error, DBUS_ERROR_FAILED,
960 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
965 else if (user != NULL)
968 _dbus_string_init_const (&username, user);
970 if (_dbus_parse_unix_user_from_config (&username,
971 &e->d.policy.gid_uid_or_at_console))
972 e->d.policy.type = POLICY_USER;
974 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
977 else if (group != NULL)
979 DBusString group_name;
980 _dbus_string_init_const (&group_name, group);
982 if (_dbus_parse_unix_group_from_config (&group_name,
983 &e->d.policy.gid_uid_or_at_console))
984 e->d.policy.type = POLICY_GROUP;
986 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
989 else if (at_console != NULL)
992 t = (strcmp (at_console, "true") == 0);
993 if (t || strcmp (at_console, "false") == 0)
995 e->d.policy.gid_uid_or_at_console = t;
996 e->d.policy.type = POLICY_CONSOLE;
1000 dbus_set_error (error, DBUS_ERROR_FAILED,
1001 "Unknown value \"%s\" for at_console in message bus configuration file",
1009 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
1014 else if (element_type == ELEMENT_LIMIT)
1019 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1021 BUS_SET_OOM (error);
1025 if (!locate_attributes (parser, "limit",
1035 dbus_set_error (error, DBUS_ERROR_FAILED,
1036 "<limit> element must have a \"name\" attribute");
1040 e->d.limit.name = _dbus_strdup (name);
1041 if (e->d.limit.name == NULL)
1043 BUS_SET_OOM (error);
1049 else if (element_type == ELEMENT_SELINUX)
1051 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1054 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1056 BUS_SET_OOM (error);
1064 dbus_set_error (error, DBUS_ERROR_FAILED,
1065 "Element <%s> not allowed inside <%s> in configuration file",
1066 element_name, "busconfig");
1072 append_rule_from_element (BusConfigParser *parser,
1073 const char *element_name,
1074 const char **attribute_names,
1075 const char **attribute_values,
1079 const char *send_interface;
1080 const char *send_member;
1081 const char *send_error;
1082 const char *send_destination;
1083 const char *send_path;
1084 const char *send_type;
1085 const char *receive_interface;
1086 const char *receive_member;
1087 const char *receive_error;
1088 const char *receive_sender;
1089 const char *receive_path;
1090 const char *receive_type;
1091 const char *eavesdrop;
1092 const char *send_requested_reply;
1093 const char *receive_requested_reply;
1098 BusPolicyRule *rule;
1100 if (!locate_attributes (parser, element_name,
1104 "send_interface", &send_interface,
1105 "send_member", &send_member,
1106 "send_error", &send_error,
1107 "send_destination", &send_destination,
1108 "send_path", &send_path,
1109 "send_type", &send_type,
1110 "receive_interface", &receive_interface,
1111 "receive_member", &receive_member,
1112 "receive_error", &receive_error,
1113 "receive_sender", &receive_sender,
1114 "receive_path", &receive_path,
1115 "receive_type", &receive_type,
1116 "eavesdrop", &eavesdrop,
1117 "send_requested_reply", &send_requested_reply,
1118 "receive_requested_reply", &receive_requested_reply,
1125 if (!(send_interface || send_member || send_error || send_destination ||
1126 send_type || send_path ||
1127 receive_interface || receive_member || receive_error || receive_sender ||
1128 receive_type || receive_path || eavesdrop ||
1129 send_requested_reply || receive_requested_reply ||
1130 own || user || group))
1132 dbus_set_error (error, DBUS_ERROR_FAILED,
1133 "Element <%s> must have one or more attributes",
1138 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1139 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1141 dbus_set_error (error, DBUS_ERROR_FAILED,
1142 "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.",
1147 /* Allowed combinations of elements are:
1149 * base, must be all send or all receive:
1152 * interface + member
1155 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1156 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1158 * user, group, own must occur alone
1160 * Pretty sure the below stuff is broken, FIXME think about it more.
1163 if (((send_interface && send_error) ||
1164 (send_interface && receive_interface) ||
1165 (send_interface && receive_member) ||
1166 (send_interface && receive_error) ||
1167 (send_interface && receive_sender) ||
1168 (send_interface && receive_requested_reply) ||
1169 (send_interface && own) ||
1170 (send_interface && user) ||
1171 (send_interface && group)) ||
1173 ((send_member && send_error) ||
1174 (send_member && receive_interface) ||
1175 (send_member && receive_member) ||
1176 (send_member && receive_error) ||
1177 (send_member && receive_sender) ||
1178 (send_member && receive_requested_reply) ||
1179 (send_member && own) ||
1180 (send_member && user) ||
1181 (send_member && group)) ||
1183 ((send_error && receive_interface) ||
1184 (send_error && receive_member) ||
1185 (send_error && receive_error) ||
1186 (send_error && receive_sender) ||
1187 (send_error && receive_requested_reply) ||
1188 (send_error && own) ||
1189 (send_error && user) ||
1190 (send_error && group)) ||
1192 ((send_destination && receive_interface) ||
1193 (send_destination && receive_member) ||
1194 (send_destination && receive_error) ||
1195 (send_destination && receive_sender) ||
1196 (send_destination && receive_requested_reply) ||
1197 (send_destination && own) ||
1198 (send_destination && user) ||
1199 (send_destination && group)) ||
1201 ((send_type && receive_interface) ||
1202 (send_type && receive_member) ||
1203 (send_type && receive_error) ||
1204 (send_type && receive_sender) ||
1205 (send_type && receive_requested_reply) ||
1206 (send_type && own) ||
1207 (send_type && user) ||
1208 (send_type && group)) ||
1210 ((send_path && receive_interface) ||
1211 (send_path && receive_member) ||
1212 (send_path && receive_error) ||
1213 (send_path && receive_sender) ||
1214 (send_path && receive_requested_reply) ||
1215 (send_path && own) ||
1216 (send_path && user) ||
1217 (send_path && group)) ||
1219 ((send_requested_reply && receive_interface) ||
1220 (send_requested_reply && receive_member) ||
1221 (send_requested_reply && receive_error) ||
1222 (send_requested_reply && receive_sender) ||
1223 (send_requested_reply && receive_requested_reply) ||
1224 (send_requested_reply && own) ||
1225 (send_requested_reply && user) ||
1226 (send_requested_reply && group)) ||
1228 ((receive_interface && receive_error) ||
1229 (receive_interface && own) ||
1230 (receive_interface && user) ||
1231 (receive_interface && group)) ||
1233 ((receive_member && receive_error) ||
1234 (receive_member && own) ||
1235 (receive_member && user) ||
1236 (receive_member && group)) ||
1238 ((receive_error && own) ||
1239 (receive_error && user) ||
1240 (receive_error && group)) ||
1242 ((eavesdrop && own) ||
1243 (eavesdrop && user) ||
1244 (eavesdrop && group)) ||
1246 ((receive_requested_reply && own) ||
1247 (receive_requested_reply && user) ||
1248 (receive_requested_reply && group)) ||
1255 dbus_set_error (error, DBUS_ERROR_FAILED,
1256 "Invalid combination of attributes on element <%s>",
1263 /* In BusPolicyRule, NULL represents wildcard.
1264 * In the config file, '*' represents it.
1266 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1268 if (send_interface || send_member || send_error || send_destination ||
1269 send_path || send_type || send_requested_reply)
1273 if (IS_WILDCARD (send_interface))
1274 send_interface = NULL;
1275 if (IS_WILDCARD (send_member))
1277 if (IS_WILDCARD (send_error))
1279 if (IS_WILDCARD (send_destination))
1280 send_destination = NULL;
1281 if (IS_WILDCARD (send_path))
1283 if (IS_WILDCARD (send_type))
1286 message_type = DBUS_MESSAGE_TYPE_INVALID;
1287 if (send_type != NULL)
1289 message_type = dbus_message_type_from_string (send_type);
1290 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1292 dbus_set_error (error, DBUS_ERROR_FAILED,
1293 "Bad message type \"%s\"",
1300 !(strcmp (eavesdrop, "true") == 0 ||
1301 strcmp (eavesdrop, "false") == 0))
1303 dbus_set_error (error, DBUS_ERROR_FAILED,
1304 "Bad value \"%s\" for %s attribute, must be true or false",
1305 "eavesdrop", eavesdrop);
1309 if (send_requested_reply &&
1310 !(strcmp (send_requested_reply, "true") == 0 ||
1311 strcmp (send_requested_reply, "false") == 0))
1313 dbus_set_error (error, DBUS_ERROR_FAILED,
1314 "Bad value \"%s\" for %s attribute, must be true or false",
1315 "send_requested_reply", send_requested_reply);
1319 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1324 rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1326 if (send_requested_reply)
1327 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1329 rule->d.send.message_type = message_type;
1330 rule->d.send.path = _dbus_strdup (send_path);
1331 rule->d.send.interface = _dbus_strdup (send_interface);
1332 rule->d.send.member = _dbus_strdup (send_member);
1333 rule->d.send.error = _dbus_strdup (send_error);
1334 rule->d.send.destination = _dbus_strdup (send_destination);
1335 if (send_path && rule->d.send.path == NULL)
1337 if (send_interface && rule->d.send.interface == NULL)
1339 if (send_member && rule->d.send.member == NULL)
1341 if (send_error && rule->d.send.error == NULL)
1343 if (send_destination && rule->d.send.destination == NULL)
1346 else if (receive_interface || receive_member || receive_error || receive_sender ||
1347 receive_path || receive_type || eavesdrop || receive_requested_reply)
1351 if (IS_WILDCARD (receive_interface))
1352 receive_interface = NULL;
1353 if (IS_WILDCARD (receive_member))
1354 receive_member = NULL;
1355 if (IS_WILDCARD (receive_error))
1356 receive_error = NULL;
1357 if (IS_WILDCARD (receive_sender))
1358 receive_sender = NULL;
1359 if (IS_WILDCARD (receive_path))
1360 receive_path = NULL;
1361 if (IS_WILDCARD (receive_type))
1362 receive_type = NULL;
1364 message_type = DBUS_MESSAGE_TYPE_INVALID;
1365 if (receive_type != NULL)
1367 message_type = dbus_message_type_from_string (receive_type);
1368 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1370 dbus_set_error (error, DBUS_ERROR_FAILED,
1371 "Bad message type \"%s\"",
1379 !(strcmp (eavesdrop, "true") == 0 ||
1380 strcmp (eavesdrop, "false") == 0))
1382 dbus_set_error (error, DBUS_ERROR_FAILED,
1383 "Bad value \"%s\" for %s attribute, must be true or false",
1384 "eavesdrop", eavesdrop);
1388 if (receive_requested_reply &&
1389 !(strcmp (receive_requested_reply, "true") == 0 ||
1390 strcmp (receive_requested_reply, "false") == 0))
1392 dbus_set_error (error, DBUS_ERROR_FAILED,
1393 "Bad value \"%s\" for %s attribute, must be true or false",
1394 "receive_requested_reply", receive_requested_reply);
1398 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1403 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1405 if (receive_requested_reply)
1406 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1408 rule->d.receive.message_type = message_type;
1409 rule->d.receive.path = _dbus_strdup (receive_path);
1410 rule->d.receive.interface = _dbus_strdup (receive_interface);
1411 rule->d.receive.member = _dbus_strdup (receive_member);
1412 rule->d.receive.error = _dbus_strdup (receive_error);
1413 rule->d.receive.origin = _dbus_strdup (receive_sender);
1415 if (receive_path && rule->d.receive.path == NULL)
1417 if (receive_interface && rule->d.receive.interface == NULL)
1419 if (receive_member && rule->d.receive.member == NULL)
1421 if (receive_error && rule->d.receive.error == NULL)
1423 if (receive_sender && rule->d.receive.origin == NULL)
1428 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1432 if (IS_WILDCARD (own))
1435 rule->d.own.service_name = _dbus_strdup (own);
1436 if (own && rule->d.own.service_name == NULL)
1441 if (IS_WILDCARD (user))
1443 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1447 rule->d.user.uid = DBUS_UID_UNSET;
1451 DBusString username;
1454 _dbus_string_init_const (&username, user);
1456 if (_dbus_parse_unix_user_from_config (&username, &uid))
1458 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1462 rule->d.user.uid = uid;
1466 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1467 user, element_name);
1473 if (IS_WILDCARD (group))
1475 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1479 rule->d.group.gid = DBUS_GID_UNSET;
1483 DBusString groupname;
1486 _dbus_string_init_const (&groupname, group);
1488 if (_dbus_parse_unix_group_from_config (&groupname, &gid))
1490 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1494 rule->d.group.gid = gid;
1498 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1499 group, element_name);
1504 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1510 pe = peek_element (parser);
1511 _dbus_assert (pe != NULL);
1512 _dbus_assert (pe->type == ELEMENT_POLICY);
1514 switch (pe->d.policy.type)
1516 case POLICY_IGNORED:
1517 /* drop the rule on the floor */
1520 case POLICY_DEFAULT:
1521 if (!bus_policy_append_default_rule (parser->policy, rule))
1524 case POLICY_MANDATORY:
1525 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1529 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1531 dbus_set_error (error, DBUS_ERROR_FAILED,
1532 "<%s> rule cannot be per-user because it has bus-global semantics",
1537 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1542 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1544 dbus_set_error (error, DBUS_ERROR_FAILED,
1545 "<%s> rule cannot be per-group because it has bus-global semantics",
1550 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1556 case POLICY_CONSOLE:
1557 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1563 bus_policy_rule_unref (rule);
1570 BUS_SET_OOM (error);
1573 bus_policy_rule_unref (rule);
1578 start_policy_child (BusConfigParser *parser,
1579 const char *element_name,
1580 const char **attribute_names,
1581 const char **attribute_values,
1584 if (strcmp (element_name, "allow") == 0)
1586 if (!append_rule_from_element (parser, element_name,
1587 attribute_names, attribute_values,
1591 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1593 BUS_SET_OOM (error);
1599 else if (strcmp (element_name, "deny") == 0)
1601 if (!append_rule_from_element (parser, element_name,
1602 attribute_names, attribute_values,
1606 if (push_element (parser, ELEMENT_DENY) == NULL)
1608 BUS_SET_OOM (error);
1616 dbus_set_error (error, DBUS_ERROR_FAILED,
1617 "Element <%s> not allowed inside <%s> in configuration file",
1618 element_name, "policy");
1624 start_selinux_child (BusConfigParser *parser,
1625 const char *element_name,
1626 const char **attribute_names,
1627 const char **attribute_values,
1634 context_copy = NULL;
1636 if (strcmp (element_name, "associate") == 0)
1639 const char *context;
1641 if (!locate_attributes (parser, "associate",
1646 "context", &context,
1650 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1652 BUS_SET_OOM (error);
1656 if (own == NULL || context == NULL)
1658 dbus_set_error (error, DBUS_ERROR_FAILED,
1659 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1663 own_copy = _dbus_strdup (own);
1664 if (own_copy == NULL)
1666 context_copy = _dbus_strdup (context);
1667 if (context_copy == NULL)
1670 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1671 own_copy, context_copy))
1678 dbus_set_error (error, DBUS_ERROR_FAILED,
1679 "Element <%s> not allowed inside <%s> in configuration file",
1680 element_name, "selinux");
1686 dbus_free (own_copy);
1689 dbus_free (context_copy);
1691 BUS_SET_OOM (error);
1696 bus_config_parser_start_element (BusConfigParser *parser,
1697 const char *element_name,
1698 const char **attribute_names,
1699 const char **attribute_values,
1704 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1706 /* printf ("START: %s\n", element_name); */
1708 t = top_element_type (parser);
1710 if (t == ELEMENT_NONE)
1712 if (strcmp (element_name, "busconfig") == 0)
1714 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1717 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1719 BUS_SET_OOM (error);
1727 dbus_set_error (error, DBUS_ERROR_FAILED,
1728 "Unknown element <%s> at root of configuration file",
1733 else if (t == ELEMENT_BUSCONFIG)
1735 return start_busconfig_child (parser, element_name,
1736 attribute_names, attribute_values,
1739 else if (t == ELEMENT_POLICY)
1741 return start_policy_child (parser, element_name,
1742 attribute_names, attribute_values,
1745 else if (t == ELEMENT_SELINUX)
1747 return start_selinux_child (parser, element_name,
1748 attribute_names, attribute_values,
1753 dbus_set_error (error, DBUS_ERROR_FAILED,
1754 "Element <%s> is not allowed in this context",
1761 set_limit (BusConfigParser *parser,
1766 dbus_bool_t must_be_positive;
1767 dbus_bool_t must_be_int;
1769 must_be_int = FALSE;
1770 must_be_positive = FALSE;
1772 if (strcmp (name, "max_incoming_bytes") == 0)
1774 must_be_positive = TRUE;
1775 parser->limits.max_incoming_bytes = value;
1777 else if (strcmp (name, "max_outgoing_bytes") == 0)
1779 must_be_positive = TRUE;
1780 parser->limits.max_outgoing_bytes = value;
1782 else if (strcmp (name, "max_message_size") == 0)
1784 must_be_positive = TRUE;
1785 parser->limits.max_message_size = value;
1787 else if (strcmp (name, "service_start_timeout") == 0)
1789 must_be_positive = TRUE;
1791 parser->limits.activation_timeout = value;
1793 else if (strcmp (name, "auth_timeout") == 0)
1795 must_be_positive = TRUE;
1797 parser->limits.auth_timeout = value;
1799 else if (strcmp (name, "reply_timeout") == 0)
1801 must_be_positive = TRUE;
1803 parser->limits.reply_timeout = value;
1805 else if (strcmp (name, "max_completed_connections") == 0)
1807 must_be_positive = TRUE;
1809 parser->limits.max_completed_connections = value;
1811 else if (strcmp (name, "max_incomplete_connections") == 0)
1813 must_be_positive = TRUE;
1815 parser->limits.max_incomplete_connections = value;
1817 else if (strcmp (name, "max_connections_per_user") == 0)
1819 must_be_positive = TRUE;
1821 parser->limits.max_connections_per_user = value;
1823 else if (strcmp (name, "max_pending_service_starts") == 0)
1825 must_be_positive = TRUE;
1827 parser->limits.max_pending_activations = value;
1829 else if (strcmp (name, "max_names_per_connection") == 0)
1831 must_be_positive = TRUE;
1833 parser->limits.max_services_per_connection = value;
1835 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1837 must_be_positive = TRUE;
1839 parser->limits.max_match_rules_per_connection = value;
1841 else if (strcmp (name, "max_replies_per_connection") == 0)
1843 must_be_positive = TRUE;
1845 parser->limits.max_replies_per_connection = value;
1849 dbus_set_error (error, DBUS_ERROR_FAILED,
1850 "There is no limit called \"%s\"\n",
1855 if (must_be_positive && value < 0)
1857 dbus_set_error (error, DBUS_ERROR_FAILED,
1858 "<limit name=\"%s\"> must be a positive number\n",
1864 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1866 dbus_set_error (error, DBUS_ERROR_FAILED,
1867 "<limit name=\"%s\"> value is too large\n",
1876 bus_config_parser_end_element (BusConfigParser *parser,
1877 const char *element_name,
1884 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1886 /* printf ("END: %s\n", element_name); */
1888 t = top_element_type (parser);
1890 if (t == ELEMENT_NONE)
1892 /* should probably be an assertion failure but
1893 * being paranoid about XML parsers
1895 dbus_set_error (error, DBUS_ERROR_FAILED,
1896 "XML parser ended element with no element on the stack");
1900 n = bus_config_parser_element_type_to_name (t);
1901 _dbus_assert (n != NULL);
1902 if (strcmp (n, element_name) != 0)
1904 /* should probably be an assertion failure but
1905 * being paranoid about XML parsers
1907 dbus_set_error (error, DBUS_ERROR_FAILED,
1908 "XML element <%s> ended but topmost element on the stack was <%s>",
1913 e = peek_element (parser);
1914 _dbus_assert (e != NULL);
1919 _dbus_assert_not_reached ("element in stack has no type");
1922 case ELEMENT_INCLUDE:
1925 case ELEMENT_LISTEN:
1926 case ELEMENT_PIDFILE:
1928 case ELEMENT_SERVICEDIR:
1929 case ELEMENT_SERVICEHELPER:
1930 case ELEMENT_INCLUDEDIR:
1932 if (!e->had_content)
1934 dbus_set_error (error, DBUS_ERROR_FAILED,
1935 "XML element <%s> was expected to have content inside it",
1936 bus_config_parser_element_type_to_name (e->type));
1940 if (e->type == ELEMENT_LIMIT)
1942 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1948 case ELEMENT_BUSCONFIG:
1949 case ELEMENT_POLICY:
1953 case ELEMENT_SELINUX:
1954 case ELEMENT_ASSOCIATE:
1955 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
1956 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
1960 pop_element (parser);
1966 all_whitespace (const DBusString *str)
1970 _dbus_string_skip_white (str, 0, &i);
1972 return i == _dbus_string_get_length (str);
1976 make_full_path (const DBusString *basedir,
1977 const DBusString *filename,
1978 DBusString *full_path)
1980 if (_dbus_path_is_absolute (filename))
1982 return _dbus_string_copy (filename, 0, full_path, 0);
1986 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1989 if (!_dbus_concat_dir_and_file (full_path, filename))
1997 include_file (BusConfigParser *parser,
1998 const DBusString *filename,
1999 dbus_bool_t ignore_missing,
2002 /* FIXME good test case for this would load each config file in the
2003 * test suite both alone, and as an include, and check
2004 * that the result is the same
2006 BusConfigParser *included;
2007 const char *filename_str;
2008 DBusError tmp_error;
2010 dbus_error_init (&tmp_error);
2012 filename_str = _dbus_string_get_const_data (filename);
2014 /* Check to make sure this file hasn't already been included. */
2015 if (seen_include (parser, filename))
2017 dbus_set_error (error, DBUS_ERROR_FAILED,
2018 "Circular inclusion of file '%s'",
2023 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2025 BUS_SET_OOM (error);
2029 /* Since parser is passed in as the parent, included
2030 inherits parser's limits. */
2031 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2033 _dbus_list_pop_last (&parser->included_files);
2035 if (included == NULL)
2037 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2039 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2042 dbus_error_free (&tmp_error);
2047 dbus_move_error (&tmp_error, error);
2053 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2055 if (!merge_included (parser, included, error))
2057 bus_config_parser_unref (included);
2061 /* Copy included's limits back to parser. */
2062 parser->limits = included->limits;
2064 bus_config_parser_unref (included);
2070 servicehelper_path (BusConfigParser *parser,
2071 const DBusString *filename,
2074 const char *filename_str;
2075 char *servicehelper;
2076 filename_str = _dbus_string_get_const_data (filename);
2078 /* check if helper exists... */
2079 if (!_dbus_file_exists (filename_str))
2081 dbus_set_error (error, DBUS_ERROR_FILE_NOT_FOUND,
2082 "setuid helper '%s'not found", filename_str);
2086 /* copy to avoid overwriting with NULL on OOM */
2087 servicehelper = _dbus_strdup (filename_str);
2090 if (servicehelper == NULL)
2092 BUS_SET_OOM (error);
2096 /* save the latest servicehelper only if not OOM */
2097 dbus_free (parser->servicehelper);
2098 parser->servicehelper = servicehelper;
2104 include_dir (BusConfigParser *parser,
2105 const DBusString *dirname,
2108 DBusString filename;
2110 DBusError tmp_error;
2114 if (!_dbus_string_init (&filename))
2116 BUS_SET_OOM (error);
2122 dir = _dbus_directory_open (dirname, error);
2127 dbus_error_init (&tmp_error);
2128 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2130 DBusString full_path;
2132 if (!_dbus_string_init (&full_path))
2134 BUS_SET_OOM (error);
2138 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2140 BUS_SET_OOM (error);
2141 _dbus_string_free (&full_path);
2145 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2147 BUS_SET_OOM (error);
2148 _dbus_string_free (&full_path);
2152 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2154 if (!include_file (parser, &full_path, TRUE, error))
2156 _dbus_string_free (&full_path);
2161 _dbus_string_free (&full_path);
2164 if (dbus_error_is_set (&tmp_error))
2166 dbus_move_error (&tmp_error, error);
2171 if (!_dbus_string_copy_data (dirname, &s))
2173 BUS_SET_OOM (error);
2177 if (!_dbus_list_append (&parser->conf_dirs, s))
2180 BUS_SET_OOM (error);
2187 _dbus_string_free (&filename);
2190 _dbus_directory_close (dir);
2196 bus_config_parser_content (BusConfigParser *parser,
2197 const DBusString *content,
2202 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2208 _dbus_string_get_const_data (content, &c_str);
2210 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2214 e = peek_element (parser);
2217 dbus_set_error (error, DBUS_ERROR_FAILED,
2218 "Text content outside of any XML element in configuration file");
2221 else if (e->had_content)
2223 _dbus_assert_not_reached ("Element had multiple content blocks");
2227 switch (top_element_type (parser))
2230 _dbus_assert_not_reached ("element at top of stack has no type");
2233 case ELEMENT_BUSCONFIG:
2234 case ELEMENT_POLICY:
2238 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2239 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2240 case ELEMENT_SELINUX:
2241 case ELEMENT_ASSOCIATE:
2242 if (all_whitespace (content))
2246 dbus_set_error (error, DBUS_ERROR_FAILED,
2247 "No text content expected inside XML element %s in configuration file",
2248 bus_config_parser_element_type_to_name (top_element_type (parser)));
2252 case ELEMENT_PIDFILE:
2256 e->had_content = TRUE;
2258 if (!_dbus_string_copy_data (content, &s))
2261 dbus_free (parser->pidfile);
2262 parser->pidfile = s;
2266 case ELEMENT_INCLUDE:
2268 DBusString full_path, selinux_policy_root;
2270 e->had_content = TRUE;
2272 if (e->d.include.if_selinux_enabled
2273 && !bus_selinux_enabled ())
2276 if (!_dbus_string_init (&full_path))
2279 if (e->d.include.selinux_root_relative)
2281 if (!bus_selinux_get_policy_root ())
2283 dbus_set_error (error, DBUS_ERROR_FAILED,
2284 "Could not determine SELinux policy root for relative inclusion");
2285 _dbus_string_free (&full_path);
2288 _dbus_string_init_const (&selinux_policy_root,
2289 bus_selinux_get_policy_root ());
2290 if (!make_full_path (&selinux_policy_root, content, &full_path))
2292 _dbus_string_free (&full_path);
2296 else if (!make_full_path (&parser->basedir, content, &full_path))
2298 _dbus_string_free (&full_path);
2302 if (!include_file (parser, &full_path,
2303 e->d.include.ignore_missing, error))
2305 _dbus_string_free (&full_path);
2309 _dbus_string_free (&full_path);
2313 case ELEMENT_SERVICEHELPER:
2315 DBusString full_path;
2317 e->had_content = TRUE;
2319 if (!_dbus_string_init (&full_path))
2322 if (!make_full_path (&parser->basedir, content, &full_path))
2324 _dbus_string_free (&full_path);
2328 if (!servicehelper_path (parser, &full_path, error))
2330 _dbus_string_free (&full_path);
2334 _dbus_string_free (&full_path);
2338 case ELEMENT_INCLUDEDIR:
2340 DBusString full_path;
2342 e->had_content = TRUE;
2344 if (!_dbus_string_init (&full_path))
2347 if (!make_full_path (&parser->basedir, content, &full_path))
2349 _dbus_string_free (&full_path);
2353 if (!include_dir (parser, &full_path, error))
2355 _dbus_string_free (&full_path);
2359 _dbus_string_free (&full_path);
2367 e->had_content = TRUE;
2369 if (!_dbus_string_copy_data (content, &s))
2372 dbus_free (parser->user);
2381 e->had_content = TRUE;
2383 if (!_dbus_string_copy_data (content, &s))
2386 dbus_free (parser->bus_type);
2387 parser->bus_type = s;
2391 case ELEMENT_LISTEN:
2395 e->had_content = TRUE;
2397 if (!_dbus_string_copy_data (content, &s))
2400 if (!_dbus_list_append (&parser->listen_on,
2413 e->had_content = TRUE;
2415 if (!_dbus_string_copy_data (content, &s))
2418 if (!_dbus_list_append (&parser->mechanisms,
2427 case ELEMENT_SERVICEDIR:
2430 DBusString full_path;
2432 e->had_content = TRUE;
2434 if (!_dbus_string_init (&full_path))
2437 if (!make_full_path (&parser->basedir, content, &full_path))
2439 _dbus_string_free (&full_path);
2443 if (!_dbus_string_copy_data (&full_path, &s))
2445 _dbus_string_free (&full_path);
2449 /* _only_ extra session directories can be specified */
2450 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2452 _dbus_string_free (&full_path);
2457 _dbus_string_free (&full_path);
2465 e->had_content = TRUE;
2468 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2470 dbus_set_error (error, DBUS_ERROR_FAILED,
2471 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2476 e->d.limit.value = val;
2478 _dbus_verbose ("Loaded value %ld for limit %s\n",
2485 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2489 BUS_SET_OOM (error);
2494 bus_config_parser_finished (BusConfigParser *parser,
2497 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2499 if (parser->stack != NULL)
2501 dbus_set_error (error, DBUS_ERROR_FAILED,
2502 "Element <%s> was not closed in configuration file",
2503 bus_config_parser_element_type_to_name (top_element_type (parser)));
2508 if (parser->is_toplevel && parser->listen_on == NULL)
2510 dbus_set_error (error, DBUS_ERROR_FAILED,
2511 "Configuration file needs one or more <listen> elements giving addresses");
2519 bus_config_parser_get_user (BusConfigParser *parser)
2521 return parser->user;
2525 bus_config_parser_get_type (BusConfigParser *parser)
2527 return parser->bus_type;
2531 bus_config_parser_get_addresses (BusConfigParser *parser)
2533 return &parser->listen_on;
2537 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2539 return &parser->mechanisms;
2543 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2545 return &parser->service_dirs;
2549 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2551 return &parser->conf_dirs;
2555 bus_config_parser_get_fork (BusConfigParser *parser)
2557 return parser->fork;
2561 bus_config_parser_get_pidfile (BusConfigParser *parser)
2563 return parser->pidfile;
2567 bus_config_parser_get_servicehelper (BusConfigParser *parser)
2569 return parser->servicehelper;
2573 bus_config_parser_steal_policy (BusConfigParser *parser)
2577 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2579 policy = parser->policy;
2581 parser->policy = NULL;
2586 /* Overwrite any limits that were set in the configuration file */
2588 bus_config_parser_get_limits (BusConfigParser *parser,
2591 *limits = parser->limits;
2595 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2597 DBusHashTable *table;
2599 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2601 table = parser->service_context_table;
2603 parser->service_context_table = NULL;
2608 #ifdef DBUS_BUILD_TESTS
2619 do_load (const DBusString *full_path,
2621 dbus_bool_t oom_possible)
2623 BusConfigParser *parser;
2626 dbus_error_init (&error);
2628 parser = bus_config_load (full_path, TRUE, NULL, &error);
2631 _DBUS_ASSERT_ERROR_IS_SET (&error);
2634 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2636 _dbus_verbose ("Failed to load valid file due to OOM\n");
2637 dbus_error_free (&error);
2640 else if (validity == VALID)
2642 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2645 dbus_error_free (&error);
2650 dbus_error_free (&error);
2656 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2658 bus_config_parser_unref (parser);
2660 if (validity == INVALID)
2662 _dbus_warn ("Accepted invalid file\n");
2672 const DBusString *full_path;
2677 check_loader_oom_func (void *data)
2679 LoaderOomData *d = data;
2681 return do_load (d->full_path, d->validity, TRUE);
2685 process_test_valid_subdir (const DBusString *test_base_dir,
2689 DBusString test_directory;
2690 DBusString filename;
2698 if (!_dbus_string_init (&test_directory))
2699 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2701 _dbus_string_init_const (&filename, subdir);
2703 if (!_dbus_string_copy (test_base_dir, 0,
2704 &test_directory, 0))
2705 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2707 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2708 _dbus_assert_not_reached ("couldn't allocate full path");
2710 _dbus_string_free (&filename);
2711 if (!_dbus_string_init (&filename))
2712 _dbus_assert_not_reached ("didn't allocate filename string\n");
2714 dbus_error_init (&error);
2715 dir = _dbus_directory_open (&test_directory, &error);
2718 _dbus_warn ("Could not open %s: %s\n",
2719 _dbus_string_get_const_data (&test_directory),
2721 dbus_error_free (&error);
2725 if (validity == VALID)
2726 printf ("Testing valid files:\n");
2727 else if (validity == INVALID)
2728 printf ("Testing invalid files:\n");
2730 printf ("Testing unknown files:\n");
2733 while (_dbus_directory_get_next_file (dir, &filename, &error))
2735 DBusString full_path;
2738 if (!_dbus_string_init (&full_path))
2739 _dbus_assert_not_reached ("couldn't init string");
2741 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2742 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2744 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2745 _dbus_assert_not_reached ("couldn't concat file to dir");
2747 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2749 _dbus_verbose ("Skipping non-.conf file %s\n",
2750 _dbus_string_get_const_data (&filename));
2751 _dbus_string_free (&full_path);
2755 printf (" %s\n", _dbus_string_get_const_data (&filename));
2757 _dbus_verbose (" expecting %s\n",
2758 validity == VALID ? "valid" :
2759 (validity == INVALID ? "invalid" :
2760 (validity == UNKNOWN ? "unknown" : "???")));
2762 d.full_path = &full_path;
2763 d.validity = validity;
2765 /* FIXME hackaround for an expat problem, see
2766 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2767 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2769 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2770 if (!check_loader_oom_func (&d))
2771 _dbus_assert_not_reached ("test failed");
2773 _dbus_string_free (&full_path);
2776 if (dbus_error_is_set (&error))
2778 _dbus_warn ("Could not get next file in %s: %s\n",
2779 _dbus_string_get_const_data (&test_directory),
2781 dbus_error_free (&error);
2790 _dbus_directory_close (dir);
2791 _dbus_string_free (&test_directory);
2792 _dbus_string_free (&filename);
2798 bools_equal (dbus_bool_t a,
2805 strings_equal_or_both_null (const char *a,
2808 if (a == NULL || b == NULL)
2811 return !strcmp (a, b);
2815 elements_equal (const Element *a,
2818 if (a->type != b->type)
2821 if (!bools_equal (a->had_content, b->had_content))
2827 case ELEMENT_INCLUDE:
2828 if (!bools_equal (a->d.include.ignore_missing,
2829 b->d.include.ignore_missing))
2833 case ELEMENT_POLICY:
2834 if (a->d.policy.type != b->d.policy.type)
2836 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
2841 if (strcmp (a->d.limit.name, b->d.limit.name))
2843 if (a->d.limit.value != b->d.limit.value)
2857 lists_of_elements_equal (DBusList *a,
2866 while (ia != NULL && ib != NULL)
2868 if (elements_equal (ia->data, ib->data))
2870 ia = _dbus_list_get_next_link (&a, ia);
2871 ib = _dbus_list_get_next_link (&b, ib);
2874 return ia == NULL && ib == NULL;
2878 lists_of_c_strings_equal (DBusList *a,
2887 while (ia != NULL && ib != NULL)
2889 if (strcmp (ia->data, ib->data))
2891 ia = _dbus_list_get_next_link (&a, ia);
2892 ib = _dbus_list_get_next_link (&b, ib);
2895 return ia == NULL && ib == NULL;
2899 limits_equal (const BusLimits *a,
2903 (a->max_incoming_bytes == b->max_incoming_bytes
2904 || a->max_outgoing_bytes == b->max_outgoing_bytes
2905 || a->max_message_size == b->max_message_size
2906 || a->activation_timeout == b->activation_timeout
2907 || a->auth_timeout == b->auth_timeout
2908 || a->max_completed_connections == b->max_completed_connections
2909 || a->max_incomplete_connections == b->max_incomplete_connections
2910 || a->max_connections_per_user == b->max_connections_per_user
2911 || a->max_pending_activations == b->max_pending_activations
2912 || a->max_services_per_connection == b->max_services_per_connection
2913 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2914 || a->max_replies_per_connection == b->max_replies_per_connection
2915 || a->reply_timeout == b->reply_timeout);
2919 config_parsers_equal (const BusConfigParser *a,
2920 const BusConfigParser *b)
2922 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2925 if (!lists_of_elements_equal (a->stack, b->stack))
2928 if (!strings_equal_or_both_null (a->user, b->user))
2931 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2934 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2937 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2940 /* FIXME: compare policy */
2942 /* FIXME: compare service selinux ID table */
2944 if (! limits_equal (&a->limits, &b->limits))
2947 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2950 if (! bools_equal (a->fork, b->fork))
2953 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2960 all_are_equiv (const DBusString *target_directory)
2962 DBusString filename;
2964 BusConfigParser *first_parser;
2965 BusConfigParser *parser;
2971 first_parser = NULL;
2975 if (!_dbus_string_init (&filename))
2976 _dbus_assert_not_reached ("didn't allocate filename string");
2978 dbus_error_init (&error);
2979 dir = _dbus_directory_open (target_directory, &error);
2982 _dbus_warn ("Could not open %s: %s\n",
2983 _dbus_string_get_const_data (target_directory),
2985 dbus_error_free (&error);
2989 printf ("Comparing equivalent files:\n");
2992 while (_dbus_directory_get_next_file (dir, &filename, &error))
2994 DBusString full_path;
2996 if (!_dbus_string_init (&full_path))
2997 _dbus_assert_not_reached ("couldn't init string");
2999 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
3000 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3002 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3003 _dbus_assert_not_reached ("couldn't concat file to dir");
3005 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3007 _dbus_verbose ("Skipping non-.conf file %s\n",
3008 _dbus_string_get_const_data (&filename));
3009 _dbus_string_free (&full_path);
3013 printf (" %s\n", _dbus_string_get_const_data (&filename));
3015 parser = bus_config_load (&full_path, TRUE, NULL, &error);
3019 _dbus_warn ("Could not load file %s: %s\n",
3020 _dbus_string_get_const_data (&full_path),
3022 _dbus_string_free (&full_path);
3023 dbus_error_free (&error);
3026 else if (first_parser == NULL)
3028 _dbus_string_free (&full_path);
3029 first_parser = parser;
3033 _dbus_string_free (&full_path);
3034 equal = config_parsers_equal (first_parser, parser);
3035 bus_config_parser_unref (parser);
3044 _dbus_string_free (&filename);
3046 bus_config_parser_unref (first_parser);
3048 _dbus_directory_close (dir);
3055 process_test_equiv_subdir (const DBusString *test_base_dir,
3058 DBusString test_directory;
3059 DBusString filename;
3068 if (!_dbus_string_init (&test_directory))
3069 _dbus_assert_not_reached ("didn't allocate test_directory");
3071 _dbus_string_init_const (&filename, subdir);
3073 if (!_dbus_string_copy (test_base_dir, 0,
3074 &test_directory, 0))
3075 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3077 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3078 _dbus_assert_not_reached ("couldn't allocate full path");
3080 _dbus_string_free (&filename);
3081 if (!_dbus_string_init (&filename))
3082 _dbus_assert_not_reached ("didn't allocate filename string");
3084 dbus_error_init (&error);
3085 dir = _dbus_directory_open (&test_directory, &error);
3088 _dbus_warn ("Could not open %s: %s\n",
3089 _dbus_string_get_const_data (&test_directory),
3091 dbus_error_free (&error);
3095 while (_dbus_directory_get_next_file (dir, &filename, &error))
3097 DBusString full_path;
3099 /* Skip CVS's magic directories! */
3100 if (_dbus_string_equal_c_str (&filename, "CVS"))
3103 if (!_dbus_string_init (&full_path))
3104 _dbus_assert_not_reached ("couldn't init string");
3106 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3107 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3109 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3110 _dbus_assert_not_reached ("couldn't concat file to dir");
3112 equal = all_are_equiv (&full_path);
3113 _dbus_string_free (&full_path);
3122 _dbus_string_free (&test_directory);
3123 _dbus_string_free (&filename);
3125 _dbus_directory_close (dir);
3131 static const char *test_session_service_dir_matches[] =
3134 "/testusr/testlocal/testshare/dbus-1/services",
3135 "/testusr/testshare/dbus-1/services",
3137 DBUS_DATADIR"/dbus-1/services",
3139 "/testhome/foo/.testlocal/testshare/dbus-1/services",
3145 test_default_session_servicedirs (void)
3150 const char *common_progs;
3153 /* On Unix we don't actually use this variable, but it's easier to handle the
3154 * deallocation if we always allocate it, whether needed or not */
3155 if (!_dbus_string_init (&progs))
3156 _dbus_assert_not_reached ("OOM allocating progs");
3158 common_progs = _dbus_getenv ("CommonProgramFiles");
3162 if (!_dbus_string_append (&progs, common_progs))
3164 _dbus_string_free (&progs);
3168 if (!_dbus_string_append (&progs, "/dbus-1/services"))
3170 _dbus_string_free (&progs);
3173 test_session_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3178 printf ("Testing retrieving the default session service directories\n");
3179 if (!_dbus_get_standard_session_servicedirs (&dirs))
3180 _dbus_assert_not_reached ("couldn't get stardard dirs");
3182 /* make sure our defaults end with share/dbus-1/service */
3183 while ((link = _dbus_list_pop_first_link (&dirs)))
3187 printf (" default service dir: %s\n", (char *)link->data);
3188 _dbus_string_init_const (&path, (char *)link->data);
3189 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/services"))
3191 printf ("error with default session service directories\n");
3192 dbus_free (link->data);
3193 _dbus_list_free_link (link);
3194 _dbus_string_free (&progs);
3198 dbus_free (link->data);
3199 _dbus_list_free_link (link);
3203 if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3204 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3206 if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3207 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3209 if (!_dbus_get_standard_session_servicedirs (&dirs))
3210 _dbus_assert_not_reached ("couldn't get stardard dirs");
3212 /* make sure we read and parse the env variable correctly */
3214 while ((link = _dbus_list_pop_first_link (&dirs)))
3216 printf (" test service dir: %s\n", (char *)link->data);
3217 if (test_session_service_dir_matches[i] == NULL)
3219 printf ("more directories parsed than in match set\n");
3220 dbus_free (link->data);
3221 _dbus_list_free_link (link);
3222 _dbus_string_free (&progs);
3226 if (strcmp (test_session_service_dir_matches[i],
3227 (char *)link->data) != 0)
3229 printf ("%s directory does not match %s in the match set\n",
3231 test_session_service_dir_matches[i]);
3232 dbus_free (link->data);
3233 _dbus_list_free_link (link);
3234 _dbus_string_free (&progs);
3240 dbus_free (link->data);
3241 _dbus_list_free_link (link);
3244 if (test_session_service_dir_matches[i] != NULL)
3246 printf ("extra data %s in the match set was not matched\n",
3247 test_session_service_dir_matches[i]);
3249 _dbus_string_free (&progs);
3253 _dbus_string_free (&progs);
3257 static const char *test_system_service_dir_matches[] =
3260 "/testusr/testlocal/testshare/dbus-1/system-services",
3261 "/testusr/testshare/dbus-1/system-services",
3263 DBUS_DATADIR"/dbus-1/system-services",
3268 test_default_system_servicedirs (void)
3273 const char *common_progs;
3276 /* On Unix we don't actually use this variable, but it's easier to handle the
3277 * deallocation if we always allocate it, whether needed or not */
3278 if (!_dbus_string_init (&progs))
3279 _dbus_assert_not_reached ("OOM allocating progs");
3281 common_progs = _dbus_getenv ("CommonProgramFiles");
3285 if (!_dbus_string_append (&progs, common_progs))
3287 _dbus_string_free (&progs);
3291 if (!_dbus_string_append (&progs, "/dbus-1/system-services"))
3293 _dbus_string_free (&progs);
3296 test_system_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3301 printf ("Testing retrieving the default system service directories\n");
3302 if (!_dbus_get_standard_system_servicedirs (&dirs))
3303 _dbus_assert_not_reached ("couldn't get stardard dirs");
3305 /* make sure our defaults end with share/dbus-1/system-service */
3306 while ((link = _dbus_list_pop_first_link (&dirs)))
3310 printf (" default service dir: %s\n", (char *)link->data);
3311 _dbus_string_init_const (&path, (char *)link->data);
3312 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/system-services"))
3314 printf ("error with default system service directories\n");
3315 dbus_free (link->data);
3316 _dbus_list_free_link (link);
3317 _dbus_string_free (&progs);
3321 dbus_free (link->data);
3322 _dbus_list_free_link (link);
3326 if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3327 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3329 if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3330 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3332 if (!_dbus_get_standard_system_servicedirs (&dirs))
3333 _dbus_assert_not_reached ("couldn't get stardard dirs");
3335 /* make sure we read and parse the env variable correctly */
3337 while ((link = _dbus_list_pop_first_link (&dirs)))
3339 printf (" test service dir: %s\n", (char *)link->data);
3340 if (test_system_service_dir_matches[i] == NULL)
3342 printf ("more directories parsed than in match set\n");
3343 dbus_free (link->data);
3344 _dbus_list_free_link (link);
3345 _dbus_string_free (&progs);
3349 if (strcmp (test_system_service_dir_matches[i],
3350 (char *)link->data) != 0)
3352 printf ("%s directory does not match %s in the match set\n",
3354 test_system_service_dir_matches[i]);
3355 dbus_free (link->data);
3356 _dbus_list_free_link (link);
3357 _dbus_string_free (&progs);
3363 dbus_free (link->data);
3364 _dbus_list_free_link (link);
3367 if (test_system_service_dir_matches[i] != NULL)
3369 printf ("extra data %s in the match set was not matched\n",
3370 test_system_service_dir_matches[i]);
3372 _dbus_string_free (&progs);
3376 _dbus_string_free (&progs);
3381 bus_config_parser_test (const DBusString *test_data_dir)
3383 if (test_data_dir == NULL ||
3384 _dbus_string_get_length (test_data_dir) == 0)
3386 printf ("No test data\n");
3390 if (!test_default_session_servicedirs())
3393 if (!test_default_system_servicedirs())
3396 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3399 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3402 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3408 #endif /* DBUS_BUILD_TESTS */