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 keep_umask : 1; /**< TRUE to keep original umask when forking */
116 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
120 push_element (BusConfigParser *parser,
125 _dbus_assert (type != ELEMENT_NONE);
127 e = dbus_new0 (Element, 1);
131 if (!_dbus_list_append (&parser->stack, e))
143 element_free (Element *e)
145 if (e->type == ELEMENT_LIMIT)
146 dbus_free (e->d.limit.name);
152 pop_element (BusConfigParser *parser)
156 e = _dbus_list_pop_last (&parser->stack);
162 peek_element (BusConfigParser *parser)
166 e = _dbus_list_get_last (&parser->stack);
172 top_element_type (BusConfigParser *parser)
176 e = _dbus_list_get_last (&parser->stack);
185 merge_service_context_hash (DBusHashTable *dest,
195 _dbus_hash_iter_init (from, &iter);
196 while (_dbus_hash_iter_next (&iter))
198 const char *service = _dbus_hash_iter_get_string_key (&iter);
199 const char *context = _dbus_hash_iter_get_value (&iter);
201 service_copy = _dbus_strdup (service);
202 if (service_copy == NULL)
204 context_copy = _dbus_strdup (context);
205 if (context_copy == NULL)
208 if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
219 dbus_free (service_copy);
222 dbus_free (context_copy);
228 service_dirs_find_dir (DBusList **service_dirs,
233 _dbus_assert (dir != NULL);
235 for (link = *service_dirs; link; link = _dbus_list_get_next_link(service_dirs, link))
237 const char *link_dir;
239 link_dir = (const char *)link->data;
240 if (strcmp (dir, link_dir) == 0)
248 service_dirs_append_unique_or_free (DBusList **service_dirs,
251 if (!service_dirs_find_dir (service_dirs, dir))
252 return _dbus_list_append (service_dirs, dir);
259 service_dirs_append_link_unique_or_free (DBusList **service_dirs,
262 if (!service_dirs_find_dir (service_dirs, dir_link->data))
264 _dbus_list_append_link (service_dirs, dir_link);
268 dbus_free (dir_link->data);
269 _dbus_list_free_link (dir_link);
274 merge_included (BusConfigParser *parser,
275 BusConfigParser *included,
280 if (!bus_policy_merge (parser->policy,
287 if (!merge_service_context_hash (parser->service_context_table,
288 included->service_context_table))
294 if (included->user != NULL)
296 dbus_free (parser->user);
297 parser->user = included->user;
298 included->user = NULL;
301 if (included->bus_type != NULL)
303 dbus_free (parser->bus_type);
304 parser->bus_type = included->bus_type;
305 included->bus_type = NULL;
311 if (included->keep_umask)
312 parser->keep_umask = TRUE;
314 if (included->pidfile != NULL)
316 dbus_free (parser->pidfile);
317 parser->pidfile = included->pidfile;
318 included->pidfile = NULL;
321 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
322 _dbus_list_append_link (&parser->listen_on, link);
324 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
325 _dbus_list_append_link (&parser->mechanisms, link);
327 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
328 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
330 while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
331 _dbus_list_append_link (&parser->conf_dirs, link);
337 seen_include (BusConfigParser *parser,
338 const DBusString *file)
342 iter = parser->included_files;
345 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
348 iter = _dbus_list_get_next_link (&parser->included_files, iter);
355 bus_config_parser_new (const DBusString *basedir,
356 dbus_bool_t is_toplevel,
357 const BusConfigParser *parent)
359 BusConfigParser *parser;
361 parser = dbus_new0 (BusConfigParser, 1);
365 parser->is_toplevel = !!is_toplevel;
367 if (!_dbus_string_init (&parser->basedir))
373 if (((parser->policy = bus_policy_new ()) == NULL) ||
374 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
375 ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
377 dbus_free)) == NULL))
380 bus_policy_unref (parser->policy);
382 _dbus_string_free (&parser->basedir);
390 /* Initialize the parser's limits from the parent. */
391 parser->limits = parent->limits;
393 /* Use the parent's list of included_files to avoid
394 circular inclusions. */
395 parser->included_files = parent->included_files;
400 /* Make up some numbers! woot! */
401 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 127;
402 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 127;
403 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
405 /* Making this long means the user has to wait longer for an error
406 * message if something screws up, but making it too short means
407 * they might see a false failure.
409 parser->limits.activation_timeout = 25000; /* 25 seconds */
411 /* Making this long risks making a DOS attack easier, but too short
412 * and legitimate auth will fail. If interactive auth (ask user for
413 * password) is allowed, then potentially it has to be quite long.
415 parser->limits.auth_timeout = 30000; /* 30 seconds */
417 parser->limits.max_incomplete_connections = 64;
418 parser->limits.max_connections_per_user = 256;
420 /* Note that max_completed_connections / max_connections_per_user
421 * is the number of users that would have to work together to
422 * DOS all the other users.
424 parser->limits.max_completed_connections = 2048;
426 parser->limits.max_pending_activations = 512;
427 parser->limits.max_services_per_connection = 512;
429 /* For this one, keep in mind that it isn't only the memory used
430 * by the match rules, but slowdown from linearly walking a big
431 * list of them. A client adding more than this is almost
432 * certainly a bad idea for that reason, and should change to a
433 * smaller number of wider-net match rules - getting every last
434 * message to the bus is probably better than having a thousand
437 parser->limits.max_match_rules_per_connection = 512;
439 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
441 /* this is effectively a limit on message queue size for messages
442 * that require a reply
444 parser->limits.max_replies_per_connection = 1024*8;
447 parser->refcount = 1;
453 bus_config_parser_ref (BusConfigParser *parser)
455 _dbus_assert (parser->refcount > 0);
457 parser->refcount += 1;
463 bus_config_parser_unref (BusConfigParser *parser)
465 _dbus_assert (parser->refcount > 0);
467 parser->refcount -= 1;
469 if (parser->refcount == 0)
471 while (parser->stack != NULL)
472 pop_element (parser);
474 dbus_free (parser->user);
475 dbus_free (parser->servicehelper);
476 dbus_free (parser->bus_type);
477 dbus_free (parser->pidfile);
479 _dbus_list_foreach (&parser->listen_on,
480 (DBusForeachFunction) dbus_free,
483 _dbus_list_clear (&parser->listen_on);
485 _dbus_list_foreach (&parser->service_dirs,
486 (DBusForeachFunction) dbus_free,
489 _dbus_list_clear (&parser->service_dirs);
491 _dbus_list_foreach (&parser->conf_dirs,
492 (DBusForeachFunction) dbus_free,
495 _dbus_list_clear (&parser->conf_dirs);
497 _dbus_list_foreach (&parser->mechanisms,
498 (DBusForeachFunction) dbus_free,
501 _dbus_list_clear (&parser->mechanisms);
503 _dbus_string_free (&parser->basedir);
506 bus_policy_unref (parser->policy);
508 if (parser->service_context_table)
509 _dbus_hash_table_unref (parser->service_context_table);
516 bus_config_parser_check_doctype (BusConfigParser *parser,
520 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
522 if (strcmp (doctype, "busconfig") != 0)
524 dbus_set_error (error,
526 "Configuration file has the wrong document type %s",
541 locate_attributes (BusConfigParser *parser,
542 const char *element_name,
543 const char **attribute_names,
544 const char **attribute_values,
546 const char *first_attribute_name,
547 const char **first_attribute_retloc,
555 LocateAttr attrs[MAX_ATTRS];
559 _dbus_assert (first_attribute_name != NULL);
560 _dbus_assert (first_attribute_retloc != NULL);
565 attrs[0].name = first_attribute_name;
566 attrs[0].retloc = first_attribute_retloc;
567 *first_attribute_retloc = NULL;
569 va_start (args, first_attribute_retloc);
571 name = va_arg (args, const char*);
572 retloc = va_arg (args, const char**);
576 _dbus_assert (retloc != NULL);
577 _dbus_assert (n_attrs < MAX_ATTRS);
579 attrs[n_attrs].name = name;
580 attrs[n_attrs].retloc = retloc;
584 name = va_arg (args, const char*);
585 retloc = va_arg (args, const char**);
591 while (attribute_names[i])
600 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
602 retloc = attrs[j].retloc;
606 dbus_set_error (error, DBUS_ERROR_FAILED,
607 "Attribute \"%s\" repeated twice on the same <%s> element",
608 attrs[j].name, element_name);
613 *retloc = attribute_values[i];
622 dbus_set_error (error, DBUS_ERROR_FAILED,
623 "Attribute \"%s\" is invalid on <%s> element in this context",
624 attribute_names[i], element_name);
637 check_no_attributes (BusConfigParser *parser,
638 const char *element_name,
639 const char **attribute_names,
640 const char **attribute_values,
643 if (attribute_names[0] != NULL)
645 dbus_set_error (error, DBUS_ERROR_FAILED,
646 "Attribute \"%s\" is invalid on <%s> element in this context",
647 attribute_names[0], element_name);
655 start_busconfig_child (BusConfigParser *parser,
656 const char *element_name,
657 const char **attribute_names,
658 const char **attribute_values,
661 ElementType element_type;
663 element_type = bus_config_parser_element_name_to_type (element_name);
665 if (element_type == ELEMENT_USER)
667 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
670 if (push_element (parser, ELEMENT_USER) == NULL)
678 else if (element_type == ELEMENT_TYPE)
680 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
683 if (push_element (parser, ELEMENT_TYPE) == NULL)
691 else if (element_type == ELEMENT_FORK)
693 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
696 if (push_element (parser, ELEMENT_FORK) == NULL)
706 else if (element_type == ELEMENT_KEEP_UMASK)
708 if (!check_no_attributes (parser, "keep_umask", attribute_names, attribute_values, error))
711 if (push_element (parser, ELEMENT_KEEP_UMASK) == NULL)
717 parser->keep_umask = TRUE;
721 else if (element_type == ELEMENT_PIDFILE)
723 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
726 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
734 else if (element_type == ELEMENT_LISTEN)
736 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
739 if (push_element (parser, ELEMENT_LISTEN) == NULL)
747 else if (element_type == ELEMENT_AUTH)
749 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
752 if (push_element (parser, ELEMENT_AUTH) == NULL)
760 else if (element_type == ELEMENT_SERVICEHELPER)
762 if (!check_no_attributes (parser, "servicehelper", attribute_names, attribute_values, error))
765 if (push_element (parser, ELEMENT_SERVICEHELPER) == NULL)
773 else if (element_type == ELEMENT_INCLUDEDIR)
775 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
778 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
786 else if (element_type == ELEMENT_STANDARD_SESSION_SERVICEDIRS)
792 if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
795 if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
801 if (!_dbus_get_standard_session_servicedirs (&dirs))
807 while ((link = _dbus_list_pop_first_link (&dirs)))
808 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
812 else if (element_type == ELEMENT_STANDARD_SYSTEM_SERVICEDIRS)
818 if (!check_no_attributes (parser, "standard_system_servicedirs", attribute_names, attribute_values, error))
821 if (push_element (parser, ELEMENT_STANDARD_SYSTEM_SERVICEDIRS) == NULL)
827 if (!_dbus_get_standard_system_servicedirs (&dirs))
833 while ((link = _dbus_list_pop_first_link (&dirs)))
834 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
838 else if (element_type == ELEMENT_SERVICEDIR)
840 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
843 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
851 else if (element_type == ELEMENT_INCLUDE)
854 const char *if_selinux_enabled;
855 const char *ignore_missing;
856 const char *selinux_root_relative;
858 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
864 e->d.include.ignore_missing = FALSE;
865 e->d.include.if_selinux_enabled = FALSE;
866 e->d.include.selinux_root_relative = FALSE;
868 if (!locate_attributes (parser, "include",
872 "ignore_missing", &ignore_missing,
873 "if_selinux_enabled", &if_selinux_enabled,
874 "selinux_root_relative", &selinux_root_relative,
878 if (ignore_missing != NULL)
880 if (strcmp (ignore_missing, "yes") == 0)
881 e->d.include.ignore_missing = TRUE;
882 else if (strcmp (ignore_missing, "no") == 0)
883 e->d.include.ignore_missing = FALSE;
886 dbus_set_error (error, DBUS_ERROR_FAILED,
887 "ignore_missing attribute must have value \"yes\" or \"no\"");
892 if (if_selinux_enabled != NULL)
894 if (strcmp (if_selinux_enabled, "yes") == 0)
895 e->d.include.if_selinux_enabled = TRUE;
896 else if (strcmp (if_selinux_enabled, "no") == 0)
897 e->d.include.if_selinux_enabled = FALSE;
900 dbus_set_error (error, DBUS_ERROR_FAILED,
901 "if_selinux_enabled attribute must have value"
902 " \"yes\" or \"no\"");
907 if (selinux_root_relative != NULL)
909 if (strcmp (selinux_root_relative, "yes") == 0)
910 e->d.include.selinux_root_relative = TRUE;
911 else if (strcmp (selinux_root_relative, "no") == 0)
912 e->d.include.selinux_root_relative = FALSE;
915 dbus_set_error (error, DBUS_ERROR_FAILED,
916 "selinux_root_relative attribute must have value"
917 " \"yes\" or \"no\"");
924 else if (element_type == ELEMENT_POLICY)
930 const char *at_console;
932 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
938 e->d.policy.type = POLICY_IGNORED;
940 if (!locate_attributes (parser, "policy",
947 "at_console", &at_console,
951 if (((context && user) ||
952 (context && group) ||
953 (context && at_console)) ||
955 (user && at_console)) ||
956 (group && at_console) ||
957 !(context || user || group || at_console))
959 dbus_set_error (error, DBUS_ERROR_FAILED,
960 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
966 if (strcmp (context, "default") == 0)
968 e->d.policy.type = POLICY_DEFAULT;
970 else if (strcmp (context, "mandatory") == 0)
972 e->d.policy.type = POLICY_MANDATORY;
976 dbus_set_error (error, DBUS_ERROR_FAILED,
977 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
982 else if (user != NULL)
985 _dbus_string_init_const (&username, user);
987 if (_dbus_parse_unix_user_from_config (&username,
988 &e->d.policy.gid_uid_or_at_console))
989 e->d.policy.type = POLICY_USER;
991 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
994 else if (group != NULL)
996 DBusString group_name;
997 _dbus_string_init_const (&group_name, group);
999 if (_dbus_parse_unix_group_from_config (&group_name,
1000 &e->d.policy.gid_uid_or_at_console))
1001 e->d.policy.type = POLICY_GROUP;
1003 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
1006 else if (at_console != NULL)
1009 t = (strcmp (at_console, "true") == 0);
1010 if (t || strcmp (at_console, "false") == 0)
1012 e->d.policy.gid_uid_or_at_console = t;
1013 e->d.policy.type = POLICY_CONSOLE;
1017 dbus_set_error (error, DBUS_ERROR_FAILED,
1018 "Unknown value \"%s\" for at_console in message bus configuration file",
1026 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
1031 else if (element_type == ELEMENT_LIMIT)
1036 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1038 BUS_SET_OOM (error);
1042 if (!locate_attributes (parser, "limit",
1052 dbus_set_error (error, DBUS_ERROR_FAILED,
1053 "<limit> element must have a \"name\" attribute");
1057 e->d.limit.name = _dbus_strdup (name);
1058 if (e->d.limit.name == NULL)
1060 BUS_SET_OOM (error);
1066 else if (element_type == ELEMENT_SELINUX)
1068 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1071 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1073 BUS_SET_OOM (error);
1081 dbus_set_error (error, DBUS_ERROR_FAILED,
1082 "Element <%s> not allowed inside <%s> in configuration file",
1083 element_name, "busconfig");
1089 append_rule_from_element (BusConfigParser *parser,
1090 const char *element_name,
1091 const char **attribute_names,
1092 const char **attribute_values,
1096 const char *send_interface;
1097 const char *send_member;
1098 const char *send_error;
1099 const char *send_destination;
1100 const char *send_path;
1101 const char *send_type;
1102 const char *receive_interface;
1103 const char *receive_member;
1104 const char *receive_error;
1105 const char *receive_sender;
1106 const char *receive_path;
1107 const char *receive_type;
1108 const char *eavesdrop;
1109 const char *send_requested_reply;
1110 const char *receive_requested_reply;
1115 BusPolicyRule *rule;
1117 if (!locate_attributes (parser, element_name,
1121 "send_interface", &send_interface,
1122 "send_member", &send_member,
1123 "send_error", &send_error,
1124 "send_destination", &send_destination,
1125 "send_path", &send_path,
1126 "send_type", &send_type,
1127 "receive_interface", &receive_interface,
1128 "receive_member", &receive_member,
1129 "receive_error", &receive_error,
1130 "receive_sender", &receive_sender,
1131 "receive_path", &receive_path,
1132 "receive_type", &receive_type,
1133 "eavesdrop", &eavesdrop,
1134 "send_requested_reply", &send_requested_reply,
1135 "receive_requested_reply", &receive_requested_reply,
1142 if (!(send_interface || send_member || send_error || send_destination ||
1143 send_type || send_path ||
1144 receive_interface || receive_member || receive_error || receive_sender ||
1145 receive_type || receive_path || eavesdrop ||
1146 send_requested_reply || receive_requested_reply ||
1147 own || user || group))
1149 dbus_set_error (error, DBUS_ERROR_FAILED,
1150 "Element <%s> must have one or more attributes",
1155 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1156 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1158 dbus_set_error (error, DBUS_ERROR_FAILED,
1159 "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.",
1164 /* Allowed combinations of elements are:
1166 * base, must be all send or all receive:
1169 * interface + member
1172 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1173 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1175 * user, group, own must occur alone
1177 * Pretty sure the below stuff is broken, FIXME think about it more.
1180 if (((send_interface && send_error) ||
1181 (send_interface && receive_interface) ||
1182 (send_interface && receive_member) ||
1183 (send_interface && receive_error) ||
1184 (send_interface && receive_sender) ||
1185 (send_interface && receive_requested_reply) ||
1186 (send_interface && own) ||
1187 (send_interface && user) ||
1188 (send_interface && group)) ||
1190 ((send_member && send_error) ||
1191 (send_member && receive_interface) ||
1192 (send_member && receive_member) ||
1193 (send_member && receive_error) ||
1194 (send_member && receive_sender) ||
1195 (send_member && receive_requested_reply) ||
1196 (send_member && own) ||
1197 (send_member && user) ||
1198 (send_member && group)) ||
1200 ((send_error && receive_interface) ||
1201 (send_error && receive_member) ||
1202 (send_error && receive_error) ||
1203 (send_error && receive_sender) ||
1204 (send_error && receive_requested_reply) ||
1205 (send_error && own) ||
1206 (send_error && user) ||
1207 (send_error && group)) ||
1209 ((send_destination && receive_interface) ||
1210 (send_destination && receive_member) ||
1211 (send_destination && receive_error) ||
1212 (send_destination && receive_sender) ||
1213 (send_destination && receive_requested_reply) ||
1214 (send_destination && own) ||
1215 (send_destination && user) ||
1216 (send_destination && group)) ||
1218 ((send_type && receive_interface) ||
1219 (send_type && receive_member) ||
1220 (send_type && receive_error) ||
1221 (send_type && receive_sender) ||
1222 (send_type && receive_requested_reply) ||
1223 (send_type && own) ||
1224 (send_type && user) ||
1225 (send_type && group)) ||
1227 ((send_path && receive_interface) ||
1228 (send_path && receive_member) ||
1229 (send_path && receive_error) ||
1230 (send_path && receive_sender) ||
1231 (send_path && receive_requested_reply) ||
1232 (send_path && own) ||
1233 (send_path && user) ||
1234 (send_path && group)) ||
1236 ((send_requested_reply && receive_interface) ||
1237 (send_requested_reply && receive_member) ||
1238 (send_requested_reply && receive_error) ||
1239 (send_requested_reply && receive_sender) ||
1240 (send_requested_reply && receive_requested_reply) ||
1241 (send_requested_reply && own) ||
1242 (send_requested_reply && user) ||
1243 (send_requested_reply && group)) ||
1245 ((receive_interface && receive_error) ||
1246 (receive_interface && own) ||
1247 (receive_interface && user) ||
1248 (receive_interface && group)) ||
1250 ((receive_member && receive_error) ||
1251 (receive_member && own) ||
1252 (receive_member && user) ||
1253 (receive_member && group)) ||
1255 ((receive_error && own) ||
1256 (receive_error && user) ||
1257 (receive_error && group)) ||
1259 ((eavesdrop && own) ||
1260 (eavesdrop && user) ||
1261 (eavesdrop && group)) ||
1263 ((receive_requested_reply && own) ||
1264 (receive_requested_reply && user) ||
1265 (receive_requested_reply && group)) ||
1272 dbus_set_error (error, DBUS_ERROR_FAILED,
1273 "Invalid combination of attributes on element <%s>",
1280 /* In BusPolicyRule, NULL represents wildcard.
1281 * In the config file, '*' represents it.
1283 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1285 if (send_interface || send_member || send_error || send_destination ||
1286 send_path || send_type || send_requested_reply)
1290 if (IS_WILDCARD (send_interface))
1291 send_interface = NULL;
1292 if (IS_WILDCARD (send_member))
1294 if (IS_WILDCARD (send_error))
1296 if (IS_WILDCARD (send_destination))
1297 send_destination = NULL;
1298 if (IS_WILDCARD (send_path))
1300 if (IS_WILDCARD (send_type))
1303 message_type = DBUS_MESSAGE_TYPE_INVALID;
1304 if (send_type != NULL)
1306 message_type = dbus_message_type_from_string (send_type);
1307 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1309 dbus_set_error (error, DBUS_ERROR_FAILED,
1310 "Bad message type \"%s\"",
1317 !(strcmp (eavesdrop, "true") == 0 ||
1318 strcmp (eavesdrop, "false") == 0))
1320 dbus_set_error (error, DBUS_ERROR_FAILED,
1321 "Bad value \"%s\" for %s attribute, must be true or false",
1322 "eavesdrop", eavesdrop);
1326 if (send_requested_reply &&
1327 !(strcmp (send_requested_reply, "true") == 0 ||
1328 strcmp (send_requested_reply, "false") == 0))
1330 dbus_set_error (error, DBUS_ERROR_FAILED,
1331 "Bad value \"%s\" for %s attribute, must be true or false",
1332 "send_requested_reply", send_requested_reply);
1336 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1341 rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1343 if (send_requested_reply)
1344 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1346 rule->d.send.message_type = message_type;
1347 rule->d.send.path = _dbus_strdup (send_path);
1348 rule->d.send.interface = _dbus_strdup (send_interface);
1349 rule->d.send.member = _dbus_strdup (send_member);
1350 rule->d.send.error = _dbus_strdup (send_error);
1351 rule->d.send.destination = _dbus_strdup (send_destination);
1352 if (send_path && rule->d.send.path == NULL)
1354 if (send_interface && rule->d.send.interface == NULL)
1356 if (send_member && rule->d.send.member == NULL)
1358 if (send_error && rule->d.send.error == NULL)
1360 if (send_destination && rule->d.send.destination == NULL)
1363 else if (receive_interface || receive_member || receive_error || receive_sender ||
1364 receive_path || receive_type || eavesdrop || receive_requested_reply)
1368 if (IS_WILDCARD (receive_interface))
1369 receive_interface = NULL;
1370 if (IS_WILDCARD (receive_member))
1371 receive_member = NULL;
1372 if (IS_WILDCARD (receive_error))
1373 receive_error = NULL;
1374 if (IS_WILDCARD (receive_sender))
1375 receive_sender = NULL;
1376 if (IS_WILDCARD (receive_path))
1377 receive_path = NULL;
1378 if (IS_WILDCARD (receive_type))
1379 receive_type = NULL;
1381 message_type = DBUS_MESSAGE_TYPE_INVALID;
1382 if (receive_type != NULL)
1384 message_type = dbus_message_type_from_string (receive_type);
1385 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1387 dbus_set_error (error, DBUS_ERROR_FAILED,
1388 "Bad message type \"%s\"",
1396 !(strcmp (eavesdrop, "true") == 0 ||
1397 strcmp (eavesdrop, "false") == 0))
1399 dbus_set_error (error, DBUS_ERROR_FAILED,
1400 "Bad value \"%s\" for %s attribute, must be true or false",
1401 "eavesdrop", eavesdrop);
1405 if (receive_requested_reply &&
1406 !(strcmp (receive_requested_reply, "true") == 0 ||
1407 strcmp (receive_requested_reply, "false") == 0))
1409 dbus_set_error (error, DBUS_ERROR_FAILED,
1410 "Bad value \"%s\" for %s attribute, must be true or false",
1411 "receive_requested_reply", receive_requested_reply);
1415 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1420 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1422 if (receive_requested_reply)
1423 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1425 rule->d.receive.message_type = message_type;
1426 rule->d.receive.path = _dbus_strdup (receive_path);
1427 rule->d.receive.interface = _dbus_strdup (receive_interface);
1428 rule->d.receive.member = _dbus_strdup (receive_member);
1429 rule->d.receive.error = _dbus_strdup (receive_error);
1430 rule->d.receive.origin = _dbus_strdup (receive_sender);
1432 if (receive_path && rule->d.receive.path == NULL)
1434 if (receive_interface && rule->d.receive.interface == NULL)
1436 if (receive_member && rule->d.receive.member == NULL)
1438 if (receive_error && rule->d.receive.error == NULL)
1440 if (receive_sender && rule->d.receive.origin == NULL)
1445 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1449 if (IS_WILDCARD (own))
1452 rule->d.own.service_name = _dbus_strdup (own);
1453 if (own && rule->d.own.service_name == NULL)
1458 if (IS_WILDCARD (user))
1460 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1464 rule->d.user.uid = DBUS_UID_UNSET;
1468 DBusString username;
1471 _dbus_string_init_const (&username, user);
1473 if (_dbus_parse_unix_user_from_config (&username, &uid))
1475 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1479 rule->d.user.uid = uid;
1483 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1484 user, element_name);
1490 if (IS_WILDCARD (group))
1492 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1496 rule->d.group.gid = DBUS_GID_UNSET;
1500 DBusString groupname;
1503 _dbus_string_init_const (&groupname, group);
1505 if (_dbus_parse_unix_group_from_config (&groupname, &gid))
1507 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1511 rule->d.group.gid = gid;
1515 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1516 group, element_name);
1521 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1527 pe = peek_element (parser);
1528 _dbus_assert (pe != NULL);
1529 _dbus_assert (pe->type == ELEMENT_POLICY);
1531 switch (pe->d.policy.type)
1533 case POLICY_IGNORED:
1534 /* drop the rule on the floor */
1537 case POLICY_DEFAULT:
1538 if (!bus_policy_append_default_rule (parser->policy, rule))
1541 case POLICY_MANDATORY:
1542 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1546 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1548 dbus_set_error (error, DBUS_ERROR_FAILED,
1549 "<%s> rule cannot be per-user because it has bus-global semantics",
1554 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1559 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1561 dbus_set_error (error, DBUS_ERROR_FAILED,
1562 "<%s> rule cannot be per-group because it has bus-global semantics",
1567 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1573 case POLICY_CONSOLE:
1574 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1580 bus_policy_rule_unref (rule);
1587 BUS_SET_OOM (error);
1590 bus_policy_rule_unref (rule);
1595 start_policy_child (BusConfigParser *parser,
1596 const char *element_name,
1597 const char **attribute_names,
1598 const char **attribute_values,
1601 if (strcmp (element_name, "allow") == 0)
1603 if (!append_rule_from_element (parser, element_name,
1604 attribute_names, attribute_values,
1608 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1610 BUS_SET_OOM (error);
1616 else if (strcmp (element_name, "deny") == 0)
1618 if (!append_rule_from_element (parser, element_name,
1619 attribute_names, attribute_values,
1623 if (push_element (parser, ELEMENT_DENY) == NULL)
1625 BUS_SET_OOM (error);
1633 dbus_set_error (error, DBUS_ERROR_FAILED,
1634 "Element <%s> not allowed inside <%s> in configuration file",
1635 element_name, "policy");
1641 start_selinux_child (BusConfigParser *parser,
1642 const char *element_name,
1643 const char **attribute_names,
1644 const char **attribute_values,
1651 context_copy = NULL;
1653 if (strcmp (element_name, "associate") == 0)
1656 const char *context;
1658 if (!locate_attributes (parser, "associate",
1663 "context", &context,
1667 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1669 BUS_SET_OOM (error);
1673 if (own == NULL || context == NULL)
1675 dbus_set_error (error, DBUS_ERROR_FAILED,
1676 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1680 own_copy = _dbus_strdup (own);
1681 if (own_copy == NULL)
1683 context_copy = _dbus_strdup (context);
1684 if (context_copy == NULL)
1687 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1688 own_copy, context_copy))
1695 dbus_set_error (error, DBUS_ERROR_FAILED,
1696 "Element <%s> not allowed inside <%s> in configuration file",
1697 element_name, "selinux");
1703 dbus_free (own_copy);
1706 dbus_free (context_copy);
1708 BUS_SET_OOM (error);
1713 bus_config_parser_start_element (BusConfigParser *parser,
1714 const char *element_name,
1715 const char **attribute_names,
1716 const char **attribute_values,
1721 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1723 /* printf ("START: %s\n", element_name); */
1725 t = top_element_type (parser);
1727 if (t == ELEMENT_NONE)
1729 if (strcmp (element_name, "busconfig") == 0)
1731 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1734 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1736 BUS_SET_OOM (error);
1744 dbus_set_error (error, DBUS_ERROR_FAILED,
1745 "Unknown element <%s> at root of configuration file",
1750 else if (t == ELEMENT_BUSCONFIG)
1752 return start_busconfig_child (parser, element_name,
1753 attribute_names, attribute_values,
1756 else if (t == ELEMENT_POLICY)
1758 return start_policy_child (parser, element_name,
1759 attribute_names, attribute_values,
1762 else if (t == ELEMENT_SELINUX)
1764 return start_selinux_child (parser, element_name,
1765 attribute_names, attribute_values,
1770 dbus_set_error (error, DBUS_ERROR_FAILED,
1771 "Element <%s> is not allowed in this context",
1778 set_limit (BusConfigParser *parser,
1783 dbus_bool_t must_be_positive;
1784 dbus_bool_t must_be_int;
1786 must_be_int = FALSE;
1787 must_be_positive = FALSE;
1789 if (strcmp (name, "max_incoming_bytes") == 0)
1791 must_be_positive = TRUE;
1792 parser->limits.max_incoming_bytes = value;
1794 else if (strcmp (name, "max_outgoing_bytes") == 0)
1796 must_be_positive = TRUE;
1797 parser->limits.max_outgoing_bytes = value;
1799 else if (strcmp (name, "max_message_size") == 0)
1801 must_be_positive = TRUE;
1802 parser->limits.max_message_size = value;
1804 else if (strcmp (name, "service_start_timeout") == 0)
1806 must_be_positive = TRUE;
1808 parser->limits.activation_timeout = value;
1810 else if (strcmp (name, "auth_timeout") == 0)
1812 must_be_positive = TRUE;
1814 parser->limits.auth_timeout = value;
1816 else if (strcmp (name, "reply_timeout") == 0)
1818 must_be_positive = TRUE;
1820 parser->limits.reply_timeout = value;
1822 else if (strcmp (name, "max_completed_connections") == 0)
1824 must_be_positive = TRUE;
1826 parser->limits.max_completed_connections = value;
1828 else if (strcmp (name, "max_incomplete_connections") == 0)
1830 must_be_positive = TRUE;
1832 parser->limits.max_incomplete_connections = value;
1834 else if (strcmp (name, "max_connections_per_user") == 0)
1836 must_be_positive = TRUE;
1838 parser->limits.max_connections_per_user = value;
1840 else if (strcmp (name, "max_pending_service_starts") == 0)
1842 must_be_positive = TRUE;
1844 parser->limits.max_pending_activations = value;
1846 else if (strcmp (name, "max_names_per_connection") == 0)
1848 must_be_positive = TRUE;
1850 parser->limits.max_services_per_connection = value;
1852 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1854 must_be_positive = TRUE;
1856 parser->limits.max_match_rules_per_connection = value;
1858 else if (strcmp (name, "max_replies_per_connection") == 0)
1860 must_be_positive = TRUE;
1862 parser->limits.max_replies_per_connection = value;
1866 dbus_set_error (error, DBUS_ERROR_FAILED,
1867 "There is no limit called \"%s\"\n",
1872 if (must_be_positive && value < 0)
1874 dbus_set_error (error, DBUS_ERROR_FAILED,
1875 "<limit name=\"%s\"> must be a positive number\n",
1881 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1883 dbus_set_error (error, DBUS_ERROR_FAILED,
1884 "<limit name=\"%s\"> value is too large\n",
1893 bus_config_parser_end_element (BusConfigParser *parser,
1894 const char *element_name,
1901 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1903 /* printf ("END: %s\n", element_name); */
1905 t = top_element_type (parser);
1907 if (t == ELEMENT_NONE)
1909 /* should probably be an assertion failure but
1910 * being paranoid about XML parsers
1912 dbus_set_error (error, DBUS_ERROR_FAILED,
1913 "XML parser ended element with no element on the stack");
1917 n = bus_config_parser_element_type_to_name (t);
1918 _dbus_assert (n != NULL);
1919 if (strcmp (n, element_name) != 0)
1921 /* should probably be an assertion failure but
1922 * being paranoid about XML parsers
1924 dbus_set_error (error, DBUS_ERROR_FAILED,
1925 "XML element <%s> ended but topmost element on the stack was <%s>",
1930 e = peek_element (parser);
1931 _dbus_assert (e != NULL);
1936 _dbus_assert_not_reached ("element in stack has no type");
1939 case ELEMENT_INCLUDE:
1942 case ELEMENT_LISTEN:
1943 case ELEMENT_PIDFILE:
1945 case ELEMENT_SERVICEDIR:
1946 case ELEMENT_SERVICEHELPER:
1947 case ELEMENT_INCLUDEDIR:
1949 if (!e->had_content)
1951 dbus_set_error (error, DBUS_ERROR_FAILED,
1952 "XML element <%s> was expected to have content inside it",
1953 bus_config_parser_element_type_to_name (e->type));
1957 if (e->type == ELEMENT_LIMIT)
1959 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1965 case ELEMENT_BUSCONFIG:
1966 case ELEMENT_POLICY:
1970 case ELEMENT_KEEP_UMASK:
1971 case ELEMENT_SELINUX:
1972 case ELEMENT_ASSOCIATE:
1973 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
1974 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
1978 pop_element (parser);
1984 all_whitespace (const DBusString *str)
1988 _dbus_string_skip_white (str, 0, &i);
1990 return i == _dbus_string_get_length (str);
1994 make_full_path (const DBusString *basedir,
1995 const DBusString *filename,
1996 DBusString *full_path)
1998 if (_dbus_path_is_absolute (filename))
2000 return _dbus_string_copy (filename, 0, full_path, 0);
2004 if (!_dbus_string_copy (basedir, 0, full_path, 0))
2007 if (!_dbus_concat_dir_and_file (full_path, filename))
2015 include_file (BusConfigParser *parser,
2016 const DBusString *filename,
2017 dbus_bool_t ignore_missing,
2020 /* FIXME good test case for this would load each config file in the
2021 * test suite both alone, and as an include, and check
2022 * that the result is the same
2024 BusConfigParser *included;
2025 const char *filename_str;
2026 DBusError tmp_error;
2028 dbus_error_init (&tmp_error);
2030 filename_str = _dbus_string_get_const_data (filename);
2032 /* Check to make sure this file hasn't already been included. */
2033 if (seen_include (parser, filename))
2035 dbus_set_error (error, DBUS_ERROR_FAILED,
2036 "Circular inclusion of file '%s'",
2041 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2043 BUS_SET_OOM (error);
2047 /* Since parser is passed in as the parent, included
2048 inherits parser's limits. */
2049 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2051 _dbus_list_pop_last (&parser->included_files);
2053 if (included == NULL)
2055 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2057 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2060 dbus_error_free (&tmp_error);
2065 dbus_move_error (&tmp_error, error);
2071 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2073 if (!merge_included (parser, included, error))
2075 bus_config_parser_unref (included);
2079 /* Copy included's limits back to parser. */
2080 parser->limits = included->limits;
2082 bus_config_parser_unref (included);
2088 servicehelper_path (BusConfigParser *parser,
2089 const DBusString *filename,
2092 const char *filename_str;
2093 char *servicehelper;
2095 filename_str = _dbus_string_get_const_data (filename);
2097 /* copy to avoid overwriting with NULL on OOM */
2098 servicehelper = _dbus_strdup (filename_str);
2101 if (servicehelper == NULL)
2103 BUS_SET_OOM (error);
2107 /* save the latest servicehelper only if not OOM */
2108 dbus_free (parser->servicehelper);
2109 parser->servicehelper = servicehelper;
2111 /* We don't check whether the helper exists; instead we
2112 * would just fail to ever activate anything if it doesn't.
2113 * This allows an admin to fix the problem if it doesn't exist.
2114 * It also allows the parser test suite to successfully parse
2115 * test cases without installing the helper. ;-)
2122 include_dir (BusConfigParser *parser,
2123 const DBusString *dirname,
2126 DBusString filename;
2128 DBusError tmp_error;
2132 if (!_dbus_string_init (&filename))
2134 BUS_SET_OOM (error);
2140 dir = _dbus_directory_open (dirname, error);
2145 dbus_error_init (&tmp_error);
2146 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2148 DBusString full_path;
2150 if (!_dbus_string_init (&full_path))
2152 BUS_SET_OOM (error);
2156 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2158 BUS_SET_OOM (error);
2159 _dbus_string_free (&full_path);
2163 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2165 BUS_SET_OOM (error);
2166 _dbus_string_free (&full_path);
2170 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2172 if (!include_file (parser, &full_path, TRUE, error))
2174 _dbus_string_free (&full_path);
2179 _dbus_string_free (&full_path);
2182 if (dbus_error_is_set (&tmp_error))
2184 dbus_move_error (&tmp_error, error);
2189 if (!_dbus_string_copy_data (dirname, &s))
2191 BUS_SET_OOM (error);
2195 if (!_dbus_list_append (&parser->conf_dirs, s))
2198 BUS_SET_OOM (error);
2205 _dbus_string_free (&filename);
2208 _dbus_directory_close (dir);
2214 bus_config_parser_content (BusConfigParser *parser,
2215 const DBusString *content,
2220 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2226 _dbus_string_get_const_data (content, &c_str);
2228 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2232 e = peek_element (parser);
2235 dbus_set_error (error, DBUS_ERROR_FAILED,
2236 "Text content outside of any XML element in configuration file");
2239 else if (e->had_content)
2241 _dbus_assert_not_reached ("Element had multiple content blocks");
2245 switch (top_element_type (parser))
2248 _dbus_assert_not_reached ("element at top of stack has no type");
2251 case ELEMENT_BUSCONFIG:
2252 case ELEMENT_POLICY:
2256 case ELEMENT_KEEP_UMASK:
2257 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2258 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
2259 case ELEMENT_SELINUX:
2260 case ELEMENT_ASSOCIATE:
2261 if (all_whitespace (content))
2265 dbus_set_error (error, DBUS_ERROR_FAILED,
2266 "No text content expected inside XML element %s in configuration file",
2267 bus_config_parser_element_type_to_name (top_element_type (parser)));
2271 case ELEMENT_PIDFILE:
2275 e->had_content = TRUE;
2277 if (!_dbus_string_copy_data (content, &s))
2280 dbus_free (parser->pidfile);
2281 parser->pidfile = s;
2285 case ELEMENT_INCLUDE:
2287 DBusString full_path, selinux_policy_root;
2289 e->had_content = TRUE;
2291 if (e->d.include.if_selinux_enabled
2292 && !bus_selinux_enabled ())
2295 if (!_dbus_string_init (&full_path))
2298 if (e->d.include.selinux_root_relative)
2300 if (!bus_selinux_get_policy_root ())
2302 dbus_set_error (error, DBUS_ERROR_FAILED,
2303 "Could not determine SELinux policy root for relative inclusion");
2304 _dbus_string_free (&full_path);
2307 _dbus_string_init_const (&selinux_policy_root,
2308 bus_selinux_get_policy_root ());
2309 if (!make_full_path (&selinux_policy_root, content, &full_path))
2311 _dbus_string_free (&full_path);
2315 else if (!make_full_path (&parser->basedir, content, &full_path))
2317 _dbus_string_free (&full_path);
2321 if (!include_file (parser, &full_path,
2322 e->d.include.ignore_missing, error))
2324 _dbus_string_free (&full_path);
2328 _dbus_string_free (&full_path);
2332 case ELEMENT_SERVICEHELPER:
2334 DBusString full_path;
2336 e->had_content = TRUE;
2338 if (!_dbus_string_init (&full_path))
2341 if (!make_full_path (&parser->basedir, content, &full_path))
2343 _dbus_string_free (&full_path);
2347 if (!servicehelper_path (parser, &full_path, error))
2349 _dbus_string_free (&full_path);
2353 _dbus_string_free (&full_path);
2357 case ELEMENT_INCLUDEDIR:
2359 DBusString full_path;
2361 e->had_content = TRUE;
2363 if (!_dbus_string_init (&full_path))
2366 if (!make_full_path (&parser->basedir, content, &full_path))
2368 _dbus_string_free (&full_path);
2372 if (!include_dir (parser, &full_path, error))
2374 _dbus_string_free (&full_path);
2378 _dbus_string_free (&full_path);
2386 e->had_content = TRUE;
2388 if (!_dbus_string_copy_data (content, &s))
2391 dbus_free (parser->user);
2400 e->had_content = TRUE;
2402 if (!_dbus_string_copy_data (content, &s))
2405 dbus_free (parser->bus_type);
2406 parser->bus_type = s;
2410 case ELEMENT_LISTEN:
2414 e->had_content = TRUE;
2416 if (!_dbus_string_copy_data (content, &s))
2419 if (!_dbus_list_append (&parser->listen_on,
2432 e->had_content = TRUE;
2434 if (!_dbus_string_copy_data (content, &s))
2437 if (!_dbus_list_append (&parser->mechanisms,
2446 case ELEMENT_SERVICEDIR:
2449 DBusString full_path;
2451 e->had_content = TRUE;
2453 if (!_dbus_string_init (&full_path))
2456 if (!make_full_path (&parser->basedir, content, &full_path))
2458 _dbus_string_free (&full_path);
2462 if (!_dbus_string_copy_data (&full_path, &s))
2464 _dbus_string_free (&full_path);
2468 /* _only_ extra session directories can be specified */
2469 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2471 _dbus_string_free (&full_path);
2476 _dbus_string_free (&full_path);
2484 e->had_content = TRUE;
2487 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2489 dbus_set_error (error, DBUS_ERROR_FAILED,
2490 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2495 e->d.limit.value = val;
2497 _dbus_verbose ("Loaded value %ld for limit %s\n",
2504 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2508 BUS_SET_OOM (error);
2513 bus_config_parser_finished (BusConfigParser *parser,
2516 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2518 if (parser->stack != NULL)
2520 dbus_set_error (error, DBUS_ERROR_FAILED,
2521 "Element <%s> was not closed in configuration file",
2522 bus_config_parser_element_type_to_name (top_element_type (parser)));
2527 if (parser->is_toplevel && parser->listen_on == NULL)
2529 dbus_set_error (error, DBUS_ERROR_FAILED,
2530 "Configuration file needs one or more <listen> elements giving addresses");
2538 bus_config_parser_get_user (BusConfigParser *parser)
2540 return parser->user;
2544 bus_config_parser_get_type (BusConfigParser *parser)
2546 return parser->bus_type;
2550 bus_config_parser_get_addresses (BusConfigParser *parser)
2552 return &parser->listen_on;
2556 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2558 return &parser->mechanisms;
2562 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2564 return &parser->service_dirs;
2568 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2570 return &parser->conf_dirs;
2574 bus_config_parser_get_fork (BusConfigParser *parser)
2576 return parser->fork;
2580 bus_config_parser_get_keep_umask (BusConfigParser *parser)
2582 return parser->keep_umask;
2586 bus_config_parser_get_pidfile (BusConfigParser *parser)
2588 return parser->pidfile;
2592 bus_config_parser_get_servicehelper (BusConfigParser *parser)
2594 return parser->servicehelper;
2598 bus_config_parser_steal_policy (BusConfigParser *parser)
2602 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2604 policy = parser->policy;
2606 parser->policy = NULL;
2611 /* Overwrite any limits that were set in the configuration file */
2613 bus_config_parser_get_limits (BusConfigParser *parser,
2616 *limits = parser->limits;
2620 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2622 DBusHashTable *table;
2624 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2626 table = parser->service_context_table;
2628 parser->service_context_table = NULL;
2633 #ifdef DBUS_BUILD_TESTS
2644 do_load (const DBusString *full_path,
2646 dbus_bool_t oom_possible)
2648 BusConfigParser *parser;
2651 dbus_error_init (&error);
2653 parser = bus_config_load (full_path, TRUE, NULL, &error);
2656 _DBUS_ASSERT_ERROR_IS_SET (&error);
2659 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2661 _dbus_verbose ("Failed to load valid file due to OOM\n");
2662 dbus_error_free (&error);
2665 else if (validity == VALID)
2667 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2670 dbus_error_free (&error);
2675 dbus_error_free (&error);
2681 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2683 bus_config_parser_unref (parser);
2685 if (validity == INVALID)
2687 _dbus_warn ("Accepted invalid file\n");
2697 const DBusString *full_path;
2702 check_loader_oom_func (void *data)
2704 LoaderOomData *d = data;
2706 return do_load (d->full_path, d->validity, TRUE);
2710 process_test_valid_subdir (const DBusString *test_base_dir,
2714 DBusString test_directory;
2715 DBusString filename;
2723 if (!_dbus_string_init (&test_directory))
2724 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2726 _dbus_string_init_const (&filename, subdir);
2728 if (!_dbus_string_copy (test_base_dir, 0,
2729 &test_directory, 0))
2730 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2732 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2733 _dbus_assert_not_reached ("couldn't allocate full path");
2735 _dbus_string_free (&filename);
2736 if (!_dbus_string_init (&filename))
2737 _dbus_assert_not_reached ("didn't allocate filename string\n");
2739 dbus_error_init (&error);
2740 dir = _dbus_directory_open (&test_directory, &error);
2743 _dbus_warn ("Could not open %s: %s\n",
2744 _dbus_string_get_const_data (&test_directory),
2746 dbus_error_free (&error);
2750 if (validity == VALID)
2751 printf ("Testing valid files:\n");
2752 else if (validity == INVALID)
2753 printf ("Testing invalid files:\n");
2755 printf ("Testing unknown files:\n");
2758 while (_dbus_directory_get_next_file (dir, &filename, &error))
2760 DBusString full_path;
2763 if (!_dbus_string_init (&full_path))
2764 _dbus_assert_not_reached ("couldn't init string");
2766 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2767 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2769 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2770 _dbus_assert_not_reached ("couldn't concat file to dir");
2772 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2774 _dbus_verbose ("Skipping non-.conf file %s\n",
2775 _dbus_string_get_const_data (&filename));
2776 _dbus_string_free (&full_path);
2780 printf (" %s\n", _dbus_string_get_const_data (&filename));
2782 _dbus_verbose (" expecting %s\n",
2783 validity == VALID ? "valid" :
2784 (validity == INVALID ? "invalid" :
2785 (validity == UNKNOWN ? "unknown" : "???")));
2787 d.full_path = &full_path;
2788 d.validity = validity;
2790 /* FIXME hackaround for an expat problem, see
2791 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2792 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2794 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2795 if (!check_loader_oom_func (&d))
2796 _dbus_assert_not_reached ("test failed");
2798 _dbus_string_free (&full_path);
2801 if (dbus_error_is_set (&error))
2803 _dbus_warn ("Could not get next file in %s: %s\n",
2804 _dbus_string_get_const_data (&test_directory),
2806 dbus_error_free (&error);
2815 _dbus_directory_close (dir);
2816 _dbus_string_free (&test_directory);
2817 _dbus_string_free (&filename);
2823 bools_equal (dbus_bool_t a,
2830 strings_equal_or_both_null (const char *a,
2833 if (a == NULL || b == NULL)
2836 return !strcmp (a, b);
2840 elements_equal (const Element *a,
2843 if (a->type != b->type)
2846 if (!bools_equal (a->had_content, b->had_content))
2852 case ELEMENT_INCLUDE:
2853 if (!bools_equal (a->d.include.ignore_missing,
2854 b->d.include.ignore_missing))
2858 case ELEMENT_POLICY:
2859 if (a->d.policy.type != b->d.policy.type)
2861 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
2866 if (strcmp (a->d.limit.name, b->d.limit.name))
2868 if (a->d.limit.value != b->d.limit.value)
2882 lists_of_elements_equal (DBusList *a,
2891 while (ia != NULL && ib != NULL)
2893 if (elements_equal (ia->data, ib->data))
2895 ia = _dbus_list_get_next_link (&a, ia);
2896 ib = _dbus_list_get_next_link (&b, ib);
2899 return ia == NULL && ib == NULL;
2903 lists_of_c_strings_equal (DBusList *a,
2912 while (ia != NULL && ib != NULL)
2914 if (strcmp (ia->data, ib->data))
2916 ia = _dbus_list_get_next_link (&a, ia);
2917 ib = _dbus_list_get_next_link (&b, ib);
2920 return ia == NULL && ib == NULL;
2924 limits_equal (const BusLimits *a,
2928 (a->max_incoming_bytes == b->max_incoming_bytes
2929 || a->max_outgoing_bytes == b->max_outgoing_bytes
2930 || a->max_message_size == b->max_message_size
2931 || a->activation_timeout == b->activation_timeout
2932 || a->auth_timeout == b->auth_timeout
2933 || a->max_completed_connections == b->max_completed_connections
2934 || a->max_incomplete_connections == b->max_incomplete_connections
2935 || a->max_connections_per_user == b->max_connections_per_user
2936 || a->max_pending_activations == b->max_pending_activations
2937 || a->max_services_per_connection == b->max_services_per_connection
2938 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2939 || a->max_replies_per_connection == b->max_replies_per_connection
2940 || a->reply_timeout == b->reply_timeout);
2944 config_parsers_equal (const BusConfigParser *a,
2945 const BusConfigParser *b)
2947 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2950 if (!lists_of_elements_equal (a->stack, b->stack))
2953 if (!strings_equal_or_both_null (a->user, b->user))
2956 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2959 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2962 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2965 /* FIXME: compare policy */
2967 /* FIXME: compare service selinux ID table */
2969 if (! limits_equal (&a->limits, &b->limits))
2972 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2975 if (! bools_equal (a->fork, b->fork))
2978 if (! bools_equal (a->keep_umask, b->keep_umask))
2981 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2988 all_are_equiv (const DBusString *target_directory)
2990 DBusString filename;
2992 BusConfigParser *first_parser;
2993 BusConfigParser *parser;
2999 first_parser = NULL;
3003 if (!_dbus_string_init (&filename))
3004 _dbus_assert_not_reached ("didn't allocate filename string");
3006 dbus_error_init (&error);
3007 dir = _dbus_directory_open (target_directory, &error);
3010 _dbus_warn ("Could not open %s: %s\n",
3011 _dbus_string_get_const_data (target_directory),
3013 dbus_error_free (&error);
3017 printf ("Comparing equivalent files:\n");
3020 while (_dbus_directory_get_next_file (dir, &filename, &error))
3022 DBusString full_path;
3024 if (!_dbus_string_init (&full_path))
3025 _dbus_assert_not_reached ("couldn't init string");
3027 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
3028 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3030 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3031 _dbus_assert_not_reached ("couldn't concat file to dir");
3033 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
3035 _dbus_verbose ("Skipping non-.conf file %s\n",
3036 _dbus_string_get_const_data (&filename));
3037 _dbus_string_free (&full_path);
3041 printf (" %s\n", _dbus_string_get_const_data (&filename));
3043 parser = bus_config_load (&full_path, TRUE, NULL, &error);
3047 _dbus_warn ("Could not load file %s: %s\n",
3048 _dbus_string_get_const_data (&full_path),
3050 _dbus_string_free (&full_path);
3051 dbus_error_free (&error);
3054 else if (first_parser == NULL)
3056 _dbus_string_free (&full_path);
3057 first_parser = parser;
3061 _dbus_string_free (&full_path);
3062 equal = config_parsers_equal (first_parser, parser);
3063 bus_config_parser_unref (parser);
3072 _dbus_string_free (&filename);
3074 bus_config_parser_unref (first_parser);
3076 _dbus_directory_close (dir);
3083 process_test_equiv_subdir (const DBusString *test_base_dir,
3086 DBusString test_directory;
3087 DBusString filename;
3096 if (!_dbus_string_init (&test_directory))
3097 _dbus_assert_not_reached ("didn't allocate test_directory");
3099 _dbus_string_init_const (&filename, subdir);
3101 if (!_dbus_string_copy (test_base_dir, 0,
3102 &test_directory, 0))
3103 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3105 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3106 _dbus_assert_not_reached ("couldn't allocate full path");
3108 _dbus_string_free (&filename);
3109 if (!_dbus_string_init (&filename))
3110 _dbus_assert_not_reached ("didn't allocate filename string");
3112 dbus_error_init (&error);
3113 dir = _dbus_directory_open (&test_directory, &error);
3116 _dbus_warn ("Could not open %s: %s\n",
3117 _dbus_string_get_const_data (&test_directory),
3119 dbus_error_free (&error);
3123 while (_dbus_directory_get_next_file (dir, &filename, &error))
3125 DBusString full_path;
3127 /* Skip CVS's magic directories! */
3128 if (_dbus_string_equal_c_str (&filename, "CVS"))
3131 if (!_dbus_string_init (&full_path))
3132 _dbus_assert_not_reached ("couldn't init string");
3134 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3135 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3137 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3138 _dbus_assert_not_reached ("couldn't concat file to dir");
3140 equal = all_are_equiv (&full_path);
3141 _dbus_string_free (&full_path);
3150 _dbus_string_free (&test_directory);
3151 _dbus_string_free (&filename);
3153 _dbus_directory_close (dir);
3159 static const char *test_session_service_dir_matches[] =
3162 "/testusr/testlocal/testshare/dbus-1/services",
3163 "/testusr/testshare/dbus-1/services",
3165 DBUS_DATADIR"/dbus-1/services",
3167 "/testhome/foo/.testlocal/testshare/dbus-1/services",
3173 test_default_session_servicedirs (void)
3178 const char *common_progs;
3181 /* On Unix we don't actually use this variable, but it's easier to handle the
3182 * deallocation if we always allocate it, whether needed or not */
3183 if (!_dbus_string_init (&progs))
3184 _dbus_assert_not_reached ("OOM allocating progs");
3186 common_progs = _dbus_getenv ("CommonProgramFiles");
3190 if (!_dbus_string_append (&progs, common_progs))
3192 _dbus_string_free (&progs);
3196 if (!_dbus_string_append (&progs, "/dbus-1/services"))
3198 _dbus_string_free (&progs);
3201 test_session_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3206 printf ("Testing retrieving the default session service directories\n");
3207 if (!_dbus_get_standard_session_servicedirs (&dirs))
3208 _dbus_assert_not_reached ("couldn't get stardard dirs");
3210 /* make sure our defaults end with share/dbus-1/service */
3211 while ((link = _dbus_list_pop_first_link (&dirs)))
3215 printf (" default service dir: %s\n", (char *)link->data);
3216 _dbus_string_init_const (&path, (char *)link->data);
3217 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/services"))
3219 printf ("error with default session service directories\n");
3220 dbus_free (link->data);
3221 _dbus_list_free_link (link);
3222 _dbus_string_free (&progs);
3226 dbus_free (link->data);
3227 _dbus_list_free_link (link);
3231 if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3232 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3234 if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3235 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3237 if (!_dbus_get_standard_session_servicedirs (&dirs))
3238 _dbus_assert_not_reached ("couldn't get stardard dirs");
3240 /* make sure we read and parse the env variable correctly */
3242 while ((link = _dbus_list_pop_first_link (&dirs)))
3244 printf (" test service dir: %s\n", (char *)link->data);
3245 if (test_session_service_dir_matches[i] == NULL)
3247 printf ("more directories parsed than in match set\n");
3248 dbus_free (link->data);
3249 _dbus_list_free_link (link);
3250 _dbus_string_free (&progs);
3254 if (strcmp (test_session_service_dir_matches[i],
3255 (char *)link->data) != 0)
3257 printf ("%s directory does not match %s in the match set\n",
3259 test_session_service_dir_matches[i]);
3260 dbus_free (link->data);
3261 _dbus_list_free_link (link);
3262 _dbus_string_free (&progs);
3268 dbus_free (link->data);
3269 _dbus_list_free_link (link);
3272 if (test_session_service_dir_matches[i] != NULL)
3274 printf ("extra data %s in the match set was not matched\n",
3275 test_session_service_dir_matches[i]);
3277 _dbus_string_free (&progs);
3281 _dbus_string_free (&progs);
3285 static const char *test_system_service_dir_matches[] =
3288 "/testusr/testlocal/testshare/dbus-1/system-services",
3289 "/testusr/testshare/dbus-1/system-services",
3291 DBUS_DATADIR"/dbus-1/system-services",
3296 test_default_system_servicedirs (void)
3301 const char *common_progs;
3304 /* On Unix we don't actually use this variable, but it's easier to handle the
3305 * deallocation if we always allocate it, whether needed or not */
3306 if (!_dbus_string_init (&progs))
3307 _dbus_assert_not_reached ("OOM allocating progs");
3309 common_progs = _dbus_getenv ("CommonProgramFiles");
3313 if (!_dbus_string_append (&progs, common_progs))
3315 _dbus_string_free (&progs);
3319 if (!_dbus_string_append (&progs, "/dbus-1/system-services"))
3321 _dbus_string_free (&progs);
3324 test_system_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3329 printf ("Testing retrieving the default system service directories\n");
3330 if (!_dbus_get_standard_system_servicedirs (&dirs))
3331 _dbus_assert_not_reached ("couldn't get stardard dirs");
3333 /* make sure our defaults end with share/dbus-1/system-service */
3334 while ((link = _dbus_list_pop_first_link (&dirs)))
3338 printf (" default service dir: %s\n", (char *)link->data);
3339 _dbus_string_init_const (&path, (char *)link->data);
3340 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/system-services"))
3342 printf ("error with default system service directories\n");
3343 dbus_free (link->data);
3344 _dbus_list_free_link (link);
3345 _dbus_string_free (&progs);
3349 dbus_free (link->data);
3350 _dbus_list_free_link (link);
3354 if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3355 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3357 if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3358 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3360 if (!_dbus_get_standard_system_servicedirs (&dirs))
3361 _dbus_assert_not_reached ("couldn't get stardard dirs");
3363 /* make sure we read and parse the env variable correctly */
3365 while ((link = _dbus_list_pop_first_link (&dirs)))
3367 printf (" test service dir: %s\n", (char *)link->data);
3368 if (test_system_service_dir_matches[i] == NULL)
3370 printf ("more directories parsed than in match set\n");
3371 dbus_free (link->data);
3372 _dbus_list_free_link (link);
3373 _dbus_string_free (&progs);
3377 if (strcmp (test_system_service_dir_matches[i],
3378 (char *)link->data) != 0)
3380 printf ("%s directory does not match %s in the match set\n",
3382 test_system_service_dir_matches[i]);
3383 dbus_free (link->data);
3384 _dbus_list_free_link (link);
3385 _dbus_string_free (&progs);
3391 dbus_free (link->data);
3392 _dbus_list_free_link (link);
3395 if (test_system_service_dir_matches[i] != NULL)
3397 printf ("extra data %s in the match set was not matched\n",
3398 test_system_service_dir_matches[i]);
3400 _dbus_string_free (&progs);
3404 _dbus_string_free (&progs);
3409 bus_config_parser_test (const DBusString *test_data_dir)
3411 if (test_data_dir == NULL ||
3412 _dbus_string_get_length (test_data_dir) == 0)
3414 printf ("No test data\n");
3418 if (!test_default_session_servicedirs())
3421 if (!test_default_system_servicedirs())
3424 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3427 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3430 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3436 #endif /* DBUS_BUILD_TESTS */