1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* config-parser.c XML-library-agnostic configuration file parser
4 * Copyright (C) 2003, 2004 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config-parser.h"
28 #include <dbus/dbus-list.h>
29 #include <dbus/dbus-internals.h>
30 #include <dbus/dbus-userdb.h>
52 ELEMENT_STANDARD_SESSION_SERVICEDIRS
57 /* we ignore policies for unknown groups/users */
72 unsigned int had_content : 1;
78 unsigned int ignore_missing : 1;
79 unsigned int if_selinux_enabled : 1;
80 unsigned int selinux_root_relative : 1;
86 unsigned long gid_uid_or_at_console;
100 * Parser for bus configuration file.
102 struct BusConfigParser
104 int refcount; /**< Reference count */
106 DBusString basedir; /**< Directory we resolve paths relative to */
108 DBusList *stack; /**< stack of Element */
110 char *user; /**< user to run as */
112 char *bus_type; /**< Message bus type */
114 DBusList *listen_on; /**< List of addresses to listen to */
116 DBusList *mechanisms; /**< Auth mechanisms */
118 DBusList *service_dirs; /**< Directories to look for services in */
120 DBusList *conf_dirs; /**< Directories to look for policy configuration in */
122 BusPolicy *policy; /**< Security policy */
124 BusLimits limits; /**< Limits */
126 char *pidfile; /**< PID file */
128 DBusList *included_files; /**< Included files stack */
130 DBusHashTable *service_context_table; /**< Map service names to SELinux contexts */
132 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
134 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
138 element_type_to_name (ElementType type)
144 case ELEMENT_BUSCONFIG:
146 case ELEMENT_INCLUDE:
164 case ELEMENT_PIDFILE:
166 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
167 return "standard_session_servicedirs";
168 case ELEMENT_SERVICEDIR:
170 case ELEMENT_INCLUDEDIR:
174 case ELEMENT_SELINUX:
176 case ELEMENT_ASSOCIATE:
180 _dbus_assert_not_reached ("bad element type");
186 push_element (BusConfigParser *parser,
191 _dbus_assert (type != ELEMENT_NONE);
193 e = dbus_new0 (Element, 1);
197 if (!_dbus_list_append (&parser->stack, e))
209 element_free (Element *e)
211 if (e->type == ELEMENT_LIMIT)
212 dbus_free (e->d.limit.name);
218 pop_element (BusConfigParser *parser)
222 e = _dbus_list_pop_last (&parser->stack);
228 peek_element (BusConfigParser *parser)
232 e = _dbus_list_get_last (&parser->stack);
238 top_element_type (BusConfigParser *parser)
242 e = _dbus_list_get_last (&parser->stack);
251 merge_service_context_hash (DBusHashTable *dest,
261 _dbus_hash_iter_init (from, &iter);
262 while (_dbus_hash_iter_next (&iter))
264 const char *service = _dbus_hash_iter_get_string_key (&iter);
265 const char *context = _dbus_hash_iter_get_value (&iter);
267 service_copy = _dbus_strdup (service);
268 if (service_copy == NULL)
270 context_copy = _dbus_strdup (context);
271 if (context_copy == NULL)
274 if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
285 dbus_free (service_copy);
288 dbus_free (context_copy);
294 service_dirs_find_dir (DBusList **service_dirs,
299 _dbus_assert (dir != NULL);
301 for (link = *service_dirs; link; link = _dbus_list_get_next_link(service_dirs, link))
303 const char *link_dir;
305 link_dir = (const char *)link->data;
306 if (strcmp (dir, link_dir) == 0)
314 service_dirs_append_unique_or_free (DBusList **service_dirs,
317 if (!service_dirs_find_dir (service_dirs, dir))
318 return _dbus_list_append (service_dirs, dir);
325 service_dirs_append_link_unique_or_free (DBusList **service_dirs,
328 if (!service_dirs_find_dir (service_dirs, dir_link->data))
330 _dbus_list_append_link (service_dirs, dir_link);
334 dbus_free (dir_link->data);
335 _dbus_list_free_link (dir_link);
340 merge_included (BusConfigParser *parser,
341 BusConfigParser *included,
346 if (!bus_policy_merge (parser->policy,
353 if (!merge_service_context_hash (parser->service_context_table,
354 included->service_context_table))
360 if (included->user != NULL)
362 dbus_free (parser->user);
363 parser->user = included->user;
364 included->user = NULL;
367 if (included->bus_type != NULL)
369 dbus_free (parser->bus_type);
370 parser->bus_type = included->bus_type;
371 included->bus_type = NULL;
377 if (included->pidfile != NULL)
379 dbus_free (parser->pidfile);
380 parser->pidfile = included->pidfile;
381 included->pidfile = NULL;
384 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
385 _dbus_list_append_link (&parser->listen_on, link);
387 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
388 _dbus_list_append_link (&parser->mechanisms, link);
390 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
391 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
393 while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
394 _dbus_list_append_link (&parser->conf_dirs, link);
400 seen_include (BusConfigParser *parser,
401 const DBusString *file)
405 iter = parser->included_files;
408 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
411 iter = _dbus_list_get_next_link (&parser->included_files, iter);
418 bus_config_parser_new (const DBusString *basedir,
419 dbus_bool_t is_toplevel,
420 const BusConfigParser *parent)
422 BusConfigParser *parser;
424 parser = dbus_new0 (BusConfigParser, 1);
428 parser->is_toplevel = !!is_toplevel;
430 if (!_dbus_string_init (&parser->basedir))
436 if (((parser->policy = bus_policy_new ()) == NULL) ||
437 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
438 ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
440 dbus_free)) == NULL))
443 bus_policy_unref (parser->policy);
445 _dbus_string_free (&parser->basedir);
453 /* Initialize the parser's limits from the parent. */
454 parser->limits = parent->limits;
456 /* Use the parent's list of included_files to avoid
457 circular inclusions. */
458 parser->included_files = parent->included_files;
463 /* Make up some numbers! woot! */
464 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 127;
465 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 127;
466 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
468 /* Making this long means the user has to wait longer for an error
469 * message if something screws up, but making it too short means
470 * they might see a false failure.
472 parser->limits.activation_timeout = 25000; /* 25 seconds */
474 /* Making this long risks making a DOS attack easier, but too short
475 * and legitimate auth will fail. If interactive auth (ask user for
476 * password) is allowed, then potentially it has to be quite long.
478 parser->limits.auth_timeout = 30000; /* 30 seconds */
480 parser->limits.max_incomplete_connections = 64;
481 parser->limits.max_connections_per_user = 256;
483 /* Note that max_completed_connections / max_connections_per_user
484 * is the number of users that would have to work together to
485 * DOS all the other users.
487 parser->limits.max_completed_connections = 2048;
489 parser->limits.max_pending_activations = 512;
490 parser->limits.max_services_per_connection = 512;
492 parser->limits.max_match_rules_per_connection = 512;
494 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
496 /* this is effectively a limit on message queue size for messages
497 * that require a reply
499 parser->limits.max_replies_per_connection = 1024*8;
502 parser->refcount = 1;
508 bus_config_parser_ref (BusConfigParser *parser)
510 _dbus_assert (parser->refcount > 0);
512 parser->refcount += 1;
518 bus_config_parser_unref (BusConfigParser *parser)
520 _dbus_assert (parser->refcount > 0);
522 parser->refcount -= 1;
524 if (parser->refcount == 0)
526 while (parser->stack != NULL)
527 pop_element (parser);
529 dbus_free (parser->user);
530 dbus_free (parser->bus_type);
531 dbus_free (parser->pidfile);
533 _dbus_list_foreach (&parser->listen_on,
534 (DBusForeachFunction) dbus_free,
537 _dbus_list_clear (&parser->listen_on);
539 _dbus_list_foreach (&parser->service_dirs,
540 (DBusForeachFunction) dbus_free,
543 _dbus_list_clear (&parser->service_dirs);
545 _dbus_list_foreach (&parser->conf_dirs,
546 (DBusForeachFunction) dbus_free,
549 _dbus_list_clear (&parser->conf_dirs);
551 _dbus_list_foreach (&parser->mechanisms,
552 (DBusForeachFunction) dbus_free,
555 _dbus_list_clear (&parser->mechanisms);
557 _dbus_string_free (&parser->basedir);
560 bus_policy_unref (parser->policy);
562 if (parser->service_context_table)
563 _dbus_hash_table_unref (parser->service_context_table);
570 bus_config_parser_check_doctype (BusConfigParser *parser,
574 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
576 if (strcmp (doctype, "busconfig") != 0)
578 dbus_set_error (error,
580 "Configuration file has the wrong document type %s",
595 locate_attributes (BusConfigParser *parser,
596 const char *element_name,
597 const char **attribute_names,
598 const char **attribute_values,
600 const char *first_attribute_name,
601 const char **first_attribute_retloc,
609 LocateAttr attrs[MAX_ATTRS];
613 _dbus_assert (first_attribute_name != NULL);
614 _dbus_assert (first_attribute_retloc != NULL);
619 attrs[0].name = first_attribute_name;
620 attrs[0].retloc = first_attribute_retloc;
621 *first_attribute_retloc = NULL;
623 va_start (args, first_attribute_retloc);
625 name = va_arg (args, const char*);
626 retloc = va_arg (args, const char**);
630 _dbus_assert (retloc != NULL);
631 _dbus_assert (n_attrs < MAX_ATTRS);
633 attrs[n_attrs].name = name;
634 attrs[n_attrs].retloc = retloc;
638 name = va_arg (args, const char*);
639 retloc = va_arg (args, const char**);
648 while (attribute_names[i])
657 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
659 retloc = attrs[j].retloc;
663 dbus_set_error (error, DBUS_ERROR_FAILED,
664 "Attribute \"%s\" repeated twice on the same <%s> element",
665 attrs[j].name, element_name);
670 *retloc = attribute_values[i];
679 dbus_set_error (error, DBUS_ERROR_FAILED,
680 "Attribute \"%s\" is invalid on <%s> element in this context",
681 attribute_names[i], element_name);
694 check_no_attributes (BusConfigParser *parser,
695 const char *element_name,
696 const char **attribute_names,
697 const char **attribute_values,
700 if (attribute_names[0] != NULL)
702 dbus_set_error (error, DBUS_ERROR_FAILED,
703 "Attribute \"%s\" is invalid on <%s> element in this context",
704 attribute_names[0], element_name);
712 start_busconfig_child (BusConfigParser *parser,
713 const char *element_name,
714 const char **attribute_names,
715 const char **attribute_values,
718 if (strcmp (element_name, "user") == 0)
720 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
723 if (push_element (parser, ELEMENT_USER) == NULL)
731 else if (strcmp (element_name, "type") == 0)
733 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
736 if (push_element (parser, ELEMENT_TYPE) == NULL)
744 else if (strcmp (element_name, "fork") == 0)
746 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
749 if (push_element (parser, ELEMENT_FORK) == NULL)
759 else if (strcmp (element_name, "pidfile") == 0)
761 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
764 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
772 else if (strcmp (element_name, "listen") == 0)
774 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
777 if (push_element (parser, ELEMENT_LISTEN) == NULL)
785 else if (strcmp (element_name, "auth") == 0)
787 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
790 if (push_element (parser, ELEMENT_AUTH) == NULL)
798 else if (strcmp (element_name, "includedir") == 0)
800 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
803 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
811 else if (strcmp (element_name, "standard_session_servicedirs") == 0)
817 if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
820 if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
826 if (!_dbus_get_standard_session_servicedirs (&dirs))
832 while ((link = _dbus_list_pop_first_link (&dirs)))
833 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
837 else if (strcmp (element_name, "servicedir") == 0)
839 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
842 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
850 else if (strcmp (element_name, "include") == 0)
853 const char *if_selinux_enabled;
854 const char *ignore_missing;
855 const char *selinux_root_relative;
857 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
863 e->d.include.ignore_missing = FALSE;
864 e->d.include.if_selinux_enabled = FALSE;
865 e->d.include.selinux_root_relative = FALSE;
867 if (!locate_attributes (parser, "include",
871 "ignore_missing", &ignore_missing,
872 "if_selinux_enabled", &if_selinux_enabled,
873 "selinux_root_relative", &selinux_root_relative,
877 if (ignore_missing != NULL)
879 if (strcmp (ignore_missing, "yes") == 0)
880 e->d.include.ignore_missing = TRUE;
881 else if (strcmp (ignore_missing, "no") == 0)
882 e->d.include.ignore_missing = FALSE;
885 dbus_set_error (error, DBUS_ERROR_FAILED,
886 "ignore_missing attribute must have value \"yes\" or \"no\"");
891 if (if_selinux_enabled != NULL)
893 if (strcmp (if_selinux_enabled, "yes") == 0)
894 e->d.include.if_selinux_enabled = TRUE;
895 else if (strcmp (if_selinux_enabled, "no") == 0)
896 e->d.include.if_selinux_enabled = FALSE;
899 dbus_set_error (error, DBUS_ERROR_FAILED,
900 "if_selinux_enabled attribute must have value"
901 " \"yes\" or \"no\"");
906 if (selinux_root_relative != NULL)
908 if (strcmp (selinux_root_relative, "yes") == 0)
909 e->d.include.selinux_root_relative = TRUE;
910 else if (strcmp (selinux_root_relative, "no") == 0)
911 e->d.include.selinux_root_relative = FALSE;
914 dbus_set_error (error, DBUS_ERROR_FAILED,
915 "selinux_root_relative attribute must have value"
916 " \"yes\" or \"no\"");
923 else if (strcmp (element_name, "policy") == 0)
929 const char *at_console;
931 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
937 e->d.policy.type = POLICY_IGNORED;
939 if (!locate_attributes (parser, "policy",
946 "at_console", &at_console,
950 if (((context && user) ||
951 (context && group) ||
952 (context && at_console)) ||
954 (user && at_console)) ||
955 (group && at_console) ||
956 !(context || user || group || at_console))
958 dbus_set_error (error, DBUS_ERROR_FAILED,
959 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
965 if (strcmp (context, "default") == 0)
967 e->d.policy.type = POLICY_DEFAULT;
969 else if (strcmp (context, "mandatory") == 0)
971 e->d.policy.type = POLICY_MANDATORY;
975 dbus_set_error (error, DBUS_ERROR_FAILED,
976 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
981 else if (user != NULL)
984 _dbus_string_init_const (&username, user);
986 if (_dbus_get_user_id (&username,
987 &e->d.policy.gid_uid_or_at_console))
988 e->d.policy.type = POLICY_USER;
990 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
993 else if (group != NULL)
995 DBusString group_name;
996 _dbus_string_init_const (&group_name, group);
998 if (_dbus_get_group_id (&group_name,
999 &e->d.policy.gid_uid_or_at_console))
1000 e->d.policy.type = POLICY_GROUP;
1002 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
1005 else if (at_console != NULL)
1008 t = (strcmp (at_console, "true") == 0);
1009 if (t || strcmp (at_console, "false") == 0)
1011 e->d.policy.gid_uid_or_at_console = t;
1012 e->d.policy.type = POLICY_CONSOLE;
1016 dbus_set_error (error, DBUS_ERROR_FAILED,
1017 "Unknown value \"%s\" for at_console in message bus configuration file",
1025 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
1030 else if (strcmp (element_name, "limit") == 0)
1035 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1037 BUS_SET_OOM (error);
1041 if (!locate_attributes (parser, "limit",
1051 dbus_set_error (error, DBUS_ERROR_FAILED,
1052 "<limit> element must have a \"name\" attribute");
1056 e->d.limit.name = _dbus_strdup (name);
1057 if (e->d.limit.name == NULL)
1059 BUS_SET_OOM (error);
1065 else if (strcmp (element_name, "selinux") == 0)
1067 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1070 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1072 BUS_SET_OOM (error);
1080 dbus_set_error (error, DBUS_ERROR_FAILED,
1081 "Element <%s> not allowed inside <%s> in configuration file",
1082 element_name, "busconfig");
1088 append_rule_from_element (BusConfigParser *parser,
1089 const char *element_name,
1090 const char **attribute_names,
1091 const char **attribute_values,
1095 const char *send_interface;
1096 const char *send_member;
1097 const char *send_error;
1098 const char *send_destination;
1099 const char *send_path;
1100 const char *send_type;
1101 const char *receive_interface;
1102 const char *receive_member;
1103 const char *receive_error;
1104 const char *receive_sender;
1105 const char *receive_path;
1106 const char *receive_type;
1107 const char *eavesdrop;
1108 const char *send_requested_reply;
1109 const char *receive_requested_reply;
1114 BusPolicyRule *rule;
1116 if (!locate_attributes (parser, element_name,
1120 "send_interface", &send_interface,
1121 "send_member", &send_member,
1122 "send_error", &send_error,
1123 "send_destination", &send_destination,
1124 "send_path", &send_path,
1125 "send_type", &send_type,
1126 "receive_interface", &receive_interface,
1127 "receive_member", &receive_member,
1128 "receive_error", &receive_error,
1129 "receive_sender", &receive_sender,
1130 "receive_path", &receive_path,
1131 "receive_type", &receive_type,
1132 "eavesdrop", &eavesdrop,
1133 "send_requested_reply", &send_requested_reply,
1134 "receive_requested_reply", &receive_requested_reply,
1141 if (!(send_interface || send_member || send_error || send_destination ||
1142 send_type || send_path ||
1143 receive_interface || receive_member || receive_error || receive_sender ||
1144 receive_type || receive_path || eavesdrop ||
1145 send_requested_reply || receive_requested_reply ||
1146 own || user || group))
1148 dbus_set_error (error, DBUS_ERROR_FAILED,
1149 "Element <%s> must have one or more attributes",
1154 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1155 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1157 dbus_set_error (error, DBUS_ERROR_FAILED,
1158 "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.",
1163 /* Allowed combinations of elements are:
1165 * base, must be all send or all receive:
1168 * interface + member
1171 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1172 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1174 * user, group, own must occur alone
1176 * Pretty sure the below stuff is broken, FIXME think about it more.
1179 if (((send_interface && send_error) ||
1180 (send_interface && receive_interface) ||
1181 (send_interface && receive_member) ||
1182 (send_interface && receive_error) ||
1183 (send_interface && receive_sender) ||
1184 (send_interface && eavesdrop) ||
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 && eavesdrop) ||
1196 (send_member && receive_requested_reply) ||
1197 (send_member && own) ||
1198 (send_member && user) ||
1199 (send_member && group)) ||
1201 ((send_error && receive_interface) ||
1202 (send_error && receive_member) ||
1203 (send_error && receive_error) ||
1204 (send_error && receive_sender) ||
1205 (send_error && eavesdrop) ||
1206 (send_error && receive_requested_reply) ||
1207 (send_error && own) ||
1208 (send_error && user) ||
1209 (send_error && group)) ||
1211 ((send_destination && receive_interface) ||
1212 (send_destination && receive_member) ||
1213 (send_destination && receive_error) ||
1214 (send_destination && receive_sender) ||
1215 (send_destination && eavesdrop) ||
1216 (send_destination && receive_requested_reply) ||
1217 (send_destination && own) ||
1218 (send_destination && user) ||
1219 (send_destination && group)) ||
1221 ((send_type && receive_interface) ||
1222 (send_type && receive_member) ||
1223 (send_type && receive_error) ||
1224 (send_type && receive_sender) ||
1225 (send_type && eavesdrop) ||
1226 (send_type && receive_requested_reply) ||
1227 (send_type && own) ||
1228 (send_type && user) ||
1229 (send_type && group)) ||
1231 ((send_path && receive_interface) ||
1232 (send_path && receive_member) ||
1233 (send_path && receive_error) ||
1234 (send_path && receive_sender) ||
1235 (send_path && eavesdrop) ||
1236 (send_path && receive_requested_reply) ||
1237 (send_path && own) ||
1238 (send_path && user) ||
1239 (send_path && group)) ||
1241 ((send_requested_reply && receive_interface) ||
1242 (send_requested_reply && receive_member) ||
1243 (send_requested_reply && receive_error) ||
1244 (send_requested_reply && receive_sender) ||
1245 (send_requested_reply && eavesdrop) ||
1246 (send_requested_reply && receive_requested_reply) ||
1247 (send_requested_reply && own) ||
1248 (send_requested_reply && user) ||
1249 (send_requested_reply && group)) ||
1251 ((receive_interface && receive_error) ||
1252 (receive_interface && own) ||
1253 (receive_interface && user) ||
1254 (receive_interface && group)) ||
1256 ((receive_member && receive_error) ||
1257 (receive_member && own) ||
1258 (receive_member && user) ||
1259 (receive_member && group)) ||
1261 ((receive_error && own) ||
1262 (receive_error && user) ||
1263 (receive_error && group)) ||
1265 ((eavesdrop && own) ||
1266 (eavesdrop && user) ||
1267 (eavesdrop && group)) ||
1269 ((receive_requested_reply && own) ||
1270 (receive_requested_reply && user) ||
1271 (receive_requested_reply && group)) ||
1278 dbus_set_error (error, DBUS_ERROR_FAILED,
1279 "Invalid combination of attributes on element <%s>",
1286 /* In BusPolicyRule, NULL represents wildcard.
1287 * In the config file, '*' represents it.
1289 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1291 if (send_interface || send_member || send_error || send_destination ||
1292 send_path || send_type || send_requested_reply)
1296 if (IS_WILDCARD (send_interface))
1297 send_interface = NULL;
1298 if (IS_WILDCARD (send_member))
1300 if (IS_WILDCARD (send_error))
1302 if (IS_WILDCARD (send_destination))
1303 send_destination = NULL;
1304 if (IS_WILDCARD (send_path))
1306 if (IS_WILDCARD (send_type))
1309 message_type = DBUS_MESSAGE_TYPE_INVALID;
1310 if (send_type != NULL)
1312 message_type = dbus_message_type_from_string (send_type);
1313 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1315 dbus_set_error (error, DBUS_ERROR_FAILED,
1316 "Bad message type \"%s\"",
1322 if (send_requested_reply &&
1323 !(strcmp (send_requested_reply, "true") == 0 ||
1324 strcmp (send_requested_reply, "false") == 0))
1326 dbus_set_error (error, DBUS_ERROR_FAILED,
1327 "Bad value \"%s\" for %s attribute, must be true or false",
1328 "send_requested_reply", send_requested_reply);
1332 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1336 if (send_requested_reply)
1337 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1339 rule->d.send.message_type = message_type;
1340 rule->d.send.path = _dbus_strdup (send_path);
1341 rule->d.send.interface = _dbus_strdup (send_interface);
1342 rule->d.send.member = _dbus_strdup (send_member);
1343 rule->d.send.error = _dbus_strdup (send_error);
1344 rule->d.send.destination = _dbus_strdup (send_destination);
1345 if (send_path && rule->d.send.path == NULL)
1347 if (send_interface && rule->d.send.interface == NULL)
1349 if (send_member && rule->d.send.member == NULL)
1351 if (send_error && rule->d.send.error == NULL)
1353 if (send_destination && rule->d.send.destination == NULL)
1356 else if (receive_interface || receive_member || receive_error || receive_sender ||
1357 receive_path || receive_type || eavesdrop || receive_requested_reply)
1361 if (IS_WILDCARD (receive_interface))
1362 receive_interface = NULL;
1363 if (IS_WILDCARD (receive_member))
1364 receive_member = NULL;
1365 if (IS_WILDCARD (receive_error))
1366 receive_error = NULL;
1367 if (IS_WILDCARD (receive_sender))
1368 receive_sender = NULL;
1369 if (IS_WILDCARD (receive_path))
1370 receive_path = NULL;
1371 if (IS_WILDCARD (receive_type))
1372 receive_type = NULL;
1374 message_type = DBUS_MESSAGE_TYPE_INVALID;
1375 if (receive_type != NULL)
1377 message_type = dbus_message_type_from_string (receive_type);
1378 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1380 dbus_set_error (error, DBUS_ERROR_FAILED,
1381 "Bad message type \"%s\"",
1389 !(strcmp (eavesdrop, "true") == 0 ||
1390 strcmp (eavesdrop, "false") == 0))
1392 dbus_set_error (error, DBUS_ERROR_FAILED,
1393 "Bad value \"%s\" for %s attribute, must be true or false",
1394 "eavesdrop", eavesdrop);
1398 if (receive_requested_reply &&
1399 !(strcmp (receive_requested_reply, "true") == 0 ||
1400 strcmp (receive_requested_reply, "false") == 0))
1402 dbus_set_error (error, DBUS_ERROR_FAILED,
1403 "Bad value \"%s\" for %s attribute, must be true or false",
1404 "receive_requested_reply", receive_requested_reply);
1408 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1413 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1415 if (receive_requested_reply)
1416 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1418 rule->d.receive.message_type = message_type;
1419 rule->d.receive.path = _dbus_strdup (receive_path);
1420 rule->d.receive.interface = _dbus_strdup (receive_interface);
1421 rule->d.receive.member = _dbus_strdup (receive_member);
1422 rule->d.receive.error = _dbus_strdup (receive_error);
1423 rule->d.receive.origin = _dbus_strdup (receive_sender);
1425 if (receive_path && rule->d.receive.path == NULL)
1427 if (receive_interface && rule->d.receive.interface == NULL)
1429 if (receive_member && rule->d.receive.member == NULL)
1431 if (receive_error && rule->d.receive.error == NULL)
1433 if (receive_sender && rule->d.receive.origin == NULL)
1438 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1442 if (IS_WILDCARD (own))
1445 rule->d.own.service_name = _dbus_strdup (own);
1446 if (own && rule->d.own.service_name == NULL)
1451 if (IS_WILDCARD (user))
1453 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1457 rule->d.user.uid = DBUS_UID_UNSET;
1461 DBusString username;
1464 _dbus_string_init_const (&username, user);
1466 if (_dbus_get_user_id (&username, &uid))
1468 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1472 rule->d.user.uid = uid;
1476 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1477 user, element_name);
1483 if (IS_WILDCARD (group))
1485 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1489 rule->d.group.gid = DBUS_GID_UNSET;
1493 DBusString groupname;
1496 _dbus_string_init_const (&groupname, group);
1498 if (_dbus_get_user_id (&groupname, &gid))
1500 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1504 rule->d.group.gid = gid;
1508 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1509 group, element_name);
1514 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1520 pe = peek_element (parser);
1521 _dbus_assert (pe != NULL);
1522 _dbus_assert (pe->type == ELEMENT_POLICY);
1524 switch (pe->d.policy.type)
1526 case POLICY_IGNORED:
1527 /* drop the rule on the floor */
1530 case POLICY_DEFAULT:
1531 if (!bus_policy_append_default_rule (parser->policy, rule))
1534 case POLICY_MANDATORY:
1535 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1539 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1541 dbus_set_error (error, DBUS_ERROR_FAILED,
1542 "<%s> rule cannot be per-user because it has bus-global semantics",
1547 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1552 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1554 dbus_set_error (error, DBUS_ERROR_FAILED,
1555 "<%s> rule cannot be per-group because it has bus-global semantics",
1560 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1566 case POLICY_CONSOLE:
1567 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1573 bus_policy_rule_unref (rule);
1580 BUS_SET_OOM (error);
1583 bus_policy_rule_unref (rule);
1588 start_policy_child (BusConfigParser *parser,
1589 const char *element_name,
1590 const char **attribute_names,
1591 const char **attribute_values,
1594 if (strcmp (element_name, "allow") == 0)
1596 if (!append_rule_from_element (parser, element_name,
1597 attribute_names, attribute_values,
1601 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1603 BUS_SET_OOM (error);
1609 else if (strcmp (element_name, "deny") == 0)
1611 if (!append_rule_from_element (parser, element_name,
1612 attribute_names, attribute_values,
1616 if (push_element (parser, ELEMENT_DENY) == NULL)
1618 BUS_SET_OOM (error);
1626 dbus_set_error (error, DBUS_ERROR_FAILED,
1627 "Element <%s> not allowed inside <%s> in configuration file",
1628 element_name, "policy");
1634 start_selinux_child (BusConfigParser *parser,
1635 const char *element_name,
1636 const char **attribute_names,
1637 const char **attribute_values,
1644 context_copy = NULL;
1646 if (strcmp (element_name, "associate") == 0)
1649 const char *context;
1651 if (!locate_attributes (parser, "associate",
1656 "context", &context,
1660 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1662 BUS_SET_OOM (error);
1666 if (own == NULL || context == NULL)
1668 dbus_set_error (error, DBUS_ERROR_FAILED,
1669 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1673 own_copy = _dbus_strdup (own);
1674 if (own_copy == NULL)
1676 context_copy = _dbus_strdup (context);
1677 if (context_copy == NULL)
1680 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1681 own_copy, context_copy))
1688 dbus_set_error (error, DBUS_ERROR_FAILED,
1689 "Element <%s> not allowed inside <%s> in configuration file",
1690 element_name, "selinux");
1696 dbus_free (own_copy);
1699 dbus_free (context_copy);
1701 BUS_SET_OOM (error);
1706 bus_config_parser_start_element (BusConfigParser *parser,
1707 const char *element_name,
1708 const char **attribute_names,
1709 const char **attribute_values,
1714 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1716 /* printf ("START: %s\n", element_name); */
1718 t = top_element_type (parser);
1720 if (t == ELEMENT_NONE)
1722 if (strcmp (element_name, "busconfig") == 0)
1724 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1727 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1729 BUS_SET_OOM (error);
1737 dbus_set_error (error, DBUS_ERROR_FAILED,
1738 "Unknown element <%s> at root of configuration file",
1743 else if (t == ELEMENT_BUSCONFIG)
1745 return start_busconfig_child (parser, element_name,
1746 attribute_names, attribute_values,
1749 else if (t == ELEMENT_POLICY)
1751 return start_policy_child (parser, element_name,
1752 attribute_names, attribute_values,
1755 else if (t == ELEMENT_SELINUX)
1757 return start_selinux_child (parser, element_name,
1758 attribute_names, attribute_values,
1763 dbus_set_error (error, DBUS_ERROR_FAILED,
1764 "Element <%s> is not allowed in this context",
1771 set_limit (BusConfigParser *parser,
1776 dbus_bool_t must_be_positive;
1777 dbus_bool_t must_be_int;
1779 must_be_int = FALSE;
1780 must_be_positive = FALSE;
1782 if (strcmp (name, "max_incoming_bytes") == 0)
1784 must_be_positive = TRUE;
1785 parser->limits.max_incoming_bytes = value;
1787 else if (strcmp (name, "max_outgoing_bytes") == 0)
1789 must_be_positive = TRUE;
1790 parser->limits.max_outgoing_bytes = value;
1792 else if (strcmp (name, "max_message_size") == 0)
1794 must_be_positive = TRUE;
1795 parser->limits.max_message_size = value;
1797 else if (strcmp (name, "service_start_timeout") == 0)
1799 must_be_positive = TRUE;
1801 parser->limits.activation_timeout = value;
1803 else if (strcmp (name, "auth_timeout") == 0)
1805 must_be_positive = TRUE;
1807 parser->limits.auth_timeout = value;
1809 else if (strcmp (name, "reply_timeout") == 0)
1811 must_be_positive = TRUE;
1813 parser->limits.reply_timeout = value;
1815 else if (strcmp (name, "max_completed_connections") == 0)
1817 must_be_positive = TRUE;
1819 parser->limits.max_completed_connections = value;
1821 else if (strcmp (name, "max_incomplete_connections") == 0)
1823 must_be_positive = TRUE;
1825 parser->limits.max_incomplete_connections = value;
1827 else if (strcmp (name, "max_connections_per_user") == 0)
1829 must_be_positive = TRUE;
1831 parser->limits.max_connections_per_user = value;
1833 else if (strcmp (name, "max_pending_service_starts") == 0)
1835 must_be_positive = TRUE;
1837 parser->limits.max_pending_activations = value;
1839 else if (strcmp (name, "max_names_per_connection") == 0)
1841 must_be_positive = TRUE;
1843 parser->limits.max_services_per_connection = value;
1845 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1847 must_be_positive = TRUE;
1849 parser->limits.max_match_rules_per_connection = value;
1851 else if (strcmp (name, "max_replies_per_connection") == 0)
1853 must_be_positive = TRUE;
1855 parser->limits.max_replies_per_connection = value;
1859 dbus_set_error (error, DBUS_ERROR_FAILED,
1860 "There is no limit called \"%s\"\n",
1865 if (must_be_positive && value < 0)
1867 dbus_set_error (error, DBUS_ERROR_FAILED,
1868 "<limit name=\"%s\"> must be a positive number\n",
1874 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1876 dbus_set_error (error, DBUS_ERROR_FAILED,
1877 "<limit name=\"%s\"> value is too large\n",
1886 bus_config_parser_end_element (BusConfigParser *parser,
1887 const char *element_name,
1894 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1896 /* printf ("END: %s\n", element_name); */
1898 t = top_element_type (parser);
1900 if (t == ELEMENT_NONE)
1902 /* should probably be an assertion failure but
1903 * being paranoid about XML parsers
1905 dbus_set_error (error, DBUS_ERROR_FAILED,
1906 "XML parser ended element with no element on the stack");
1910 n = element_type_to_name (t);
1911 _dbus_assert (n != NULL);
1912 if (strcmp (n, element_name) != 0)
1914 /* should probably be an assertion failure but
1915 * being paranoid about XML parsers
1917 dbus_set_error (error, DBUS_ERROR_FAILED,
1918 "XML element <%s> ended but topmost element on the stack was <%s>",
1923 e = peek_element (parser);
1924 _dbus_assert (e != NULL);
1929 _dbus_assert_not_reached ("element in stack has no type");
1932 case ELEMENT_INCLUDE:
1935 case ELEMENT_LISTEN:
1936 case ELEMENT_PIDFILE:
1938 case ELEMENT_SERVICEDIR:
1939 case ELEMENT_INCLUDEDIR:
1941 if (!e->had_content)
1943 dbus_set_error (error, DBUS_ERROR_FAILED,
1944 "XML element <%s> was expected to have content inside it",
1945 element_type_to_name (e->type));
1949 if (e->type == ELEMENT_LIMIT)
1951 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1957 case ELEMENT_BUSCONFIG:
1958 case ELEMENT_POLICY:
1962 case ELEMENT_SELINUX:
1963 case ELEMENT_ASSOCIATE:
1964 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
1968 pop_element (parser);
1974 all_whitespace (const DBusString *str)
1978 _dbus_string_skip_white (str, 0, &i);
1980 return i == _dbus_string_get_length (str);
1984 make_full_path (const DBusString *basedir,
1985 const DBusString *filename,
1986 DBusString *full_path)
1988 if (_dbus_path_is_absolute (filename))
1990 return _dbus_string_copy (filename, 0, full_path, 0);
1994 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1997 if (!_dbus_concat_dir_and_file (full_path, filename))
2005 include_file (BusConfigParser *parser,
2006 const DBusString *filename,
2007 dbus_bool_t ignore_missing,
2010 /* FIXME good test case for this would load each config file in the
2011 * test suite both alone, and as an include, and check
2012 * that the result is the same
2014 BusConfigParser *included;
2015 const char *filename_str;
2016 DBusError tmp_error;
2018 dbus_error_init (&tmp_error);
2020 filename_str = _dbus_string_get_const_data (filename);
2022 /* Check to make sure this file hasn't already been included. */
2023 if (seen_include (parser, filename))
2025 dbus_set_error (error, DBUS_ERROR_FAILED,
2026 "Circular inclusion of file '%s'",
2031 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2033 BUS_SET_OOM (error);
2037 /* Since parser is passed in as the parent, included
2038 inherits parser's limits. */
2039 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2041 _dbus_list_pop_last (&parser->included_files);
2043 if (included == NULL)
2045 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2047 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2050 dbus_error_free (&tmp_error);
2055 dbus_move_error (&tmp_error, error);
2061 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2063 if (!merge_included (parser, included, error))
2065 bus_config_parser_unref (included);
2069 /* Copy included's limits back to parser. */
2070 parser->limits = included->limits;
2072 bus_config_parser_unref (included);
2078 include_dir (BusConfigParser *parser,
2079 const DBusString *dirname,
2082 DBusString filename;
2084 DBusError tmp_error;
2088 if (!_dbus_string_init (&filename))
2090 BUS_SET_OOM (error);
2096 dir = _dbus_directory_open (dirname, error);
2101 dbus_error_init (&tmp_error);
2102 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2104 DBusString full_path;
2106 if (!_dbus_string_init (&full_path))
2108 BUS_SET_OOM (error);
2112 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2114 BUS_SET_OOM (error);
2115 _dbus_string_free (&full_path);
2119 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2121 BUS_SET_OOM (error);
2122 _dbus_string_free (&full_path);
2126 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2128 if (!include_file (parser, &full_path, TRUE, error))
2130 _dbus_string_free (&full_path);
2135 _dbus_string_free (&full_path);
2138 if (dbus_error_is_set (&tmp_error))
2140 dbus_move_error (&tmp_error, error);
2145 if (!_dbus_string_copy_data (dirname, &s))
2147 BUS_SET_OOM (error);
2151 if (!_dbus_list_append (&parser->conf_dirs, s))
2154 BUS_SET_OOM (error);
2161 _dbus_string_free (&filename);
2164 _dbus_directory_close (dir);
2170 bus_config_parser_content (BusConfigParser *parser,
2171 const DBusString *content,
2176 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2182 _dbus_string_get_const_data (content, &c_str);
2184 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2188 e = peek_element (parser);
2191 dbus_set_error (error, DBUS_ERROR_FAILED,
2192 "Text content outside of any XML element in configuration file");
2195 else if (e->had_content)
2197 _dbus_assert_not_reached ("Element had multiple content blocks");
2201 switch (top_element_type (parser))
2204 _dbus_assert_not_reached ("element at top of stack has no type");
2207 case ELEMENT_BUSCONFIG:
2208 case ELEMENT_POLICY:
2212 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2213 case ELEMENT_SELINUX:
2214 case ELEMENT_ASSOCIATE:
2215 if (all_whitespace (content))
2219 dbus_set_error (error, DBUS_ERROR_FAILED,
2220 "No text content expected inside XML element %s in configuration file",
2221 element_type_to_name (top_element_type (parser)));
2225 case ELEMENT_PIDFILE:
2229 e->had_content = TRUE;
2231 if (!_dbus_string_copy_data (content, &s))
2234 dbus_free (parser->pidfile);
2235 parser->pidfile = s;
2239 case ELEMENT_INCLUDE:
2241 DBusString full_path, selinux_policy_root;
2243 e->had_content = TRUE;
2245 if (e->d.include.if_selinux_enabled
2246 && !bus_selinux_enabled ())
2249 if (!_dbus_string_init (&full_path))
2252 if (e->d.include.selinux_root_relative)
2254 if (!bus_selinux_get_policy_root ())
2256 dbus_set_error (error, DBUS_ERROR_FAILED,
2257 "Could not determine SELinux policy root for relative inclusion");
2258 _dbus_string_free (&full_path);
2261 _dbus_string_init_const (&selinux_policy_root,
2262 bus_selinux_get_policy_root ());
2263 if (!make_full_path (&selinux_policy_root, content, &full_path))
2265 _dbus_string_free (&full_path);
2269 else if (!make_full_path (&parser->basedir, content, &full_path))
2271 _dbus_string_free (&full_path);
2275 if (!include_file (parser, &full_path,
2276 e->d.include.ignore_missing, error))
2278 _dbus_string_free (&full_path);
2282 _dbus_string_free (&full_path);
2286 case ELEMENT_INCLUDEDIR:
2288 DBusString full_path;
2290 e->had_content = TRUE;
2292 if (!_dbus_string_init (&full_path))
2295 if (!make_full_path (&parser->basedir, content, &full_path))
2297 _dbus_string_free (&full_path);
2301 if (!include_dir (parser, &full_path, error))
2303 _dbus_string_free (&full_path);
2307 _dbus_string_free (&full_path);
2315 e->had_content = TRUE;
2317 if (!_dbus_string_copy_data (content, &s))
2320 dbus_free (parser->user);
2329 e->had_content = TRUE;
2331 if (!_dbus_string_copy_data (content, &s))
2334 dbus_free (parser->bus_type);
2335 parser->bus_type = s;
2339 case ELEMENT_LISTEN:
2343 e->had_content = TRUE;
2345 if (!_dbus_string_copy_data (content, &s))
2348 if (!_dbus_list_append (&parser->listen_on,
2361 e->had_content = TRUE;
2363 if (!_dbus_string_copy_data (content, &s))
2366 if (!_dbus_list_append (&parser->mechanisms,
2375 case ELEMENT_SERVICEDIR:
2378 DBusString full_path;
2380 e->had_content = TRUE;
2382 if (!_dbus_string_init (&full_path))
2385 if (!make_full_path (&parser->basedir, content, &full_path))
2387 _dbus_string_free (&full_path);
2391 if (!_dbus_string_copy_data (&full_path, &s))
2393 _dbus_string_free (&full_path);
2397 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2399 _dbus_string_free (&full_path);
2404 _dbus_string_free (&full_path);
2412 e->had_content = TRUE;
2415 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2417 dbus_set_error (error, DBUS_ERROR_FAILED,
2418 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2423 e->d.limit.value = val;
2425 _dbus_verbose ("Loaded value %ld for limit %s\n",
2432 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2436 BUS_SET_OOM (error);
2441 bus_config_parser_finished (BusConfigParser *parser,
2444 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2446 if (parser->stack != NULL)
2448 dbus_set_error (error, DBUS_ERROR_FAILED,
2449 "Element <%s> was not closed in configuration file",
2450 element_type_to_name (top_element_type (parser)));
2455 if (parser->is_toplevel && parser->listen_on == NULL)
2457 dbus_set_error (error, DBUS_ERROR_FAILED,
2458 "Configuration file needs one or more <listen> elements giving addresses");
2466 bus_config_parser_get_user (BusConfigParser *parser)
2468 return parser->user;
2472 bus_config_parser_get_type (BusConfigParser *parser)
2474 return parser->bus_type;
2478 bus_config_parser_get_addresses (BusConfigParser *parser)
2480 return &parser->listen_on;
2484 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2486 return &parser->mechanisms;
2490 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2492 return &parser->service_dirs;
2496 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2498 return &parser->conf_dirs;
2502 bus_config_parser_get_fork (BusConfigParser *parser)
2504 return parser->fork;
2508 bus_config_parser_get_pidfile (BusConfigParser *parser)
2510 return parser->pidfile;
2514 bus_config_parser_steal_policy (BusConfigParser *parser)
2518 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2520 policy = parser->policy;
2522 parser->policy = NULL;
2527 /* Overwrite any limits that were set in the configuration file */
2529 bus_config_parser_get_limits (BusConfigParser *parser,
2532 *limits = parser->limits;
2536 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2538 DBusHashTable *table;
2540 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2542 table = parser->service_context_table;
2544 parser->service_context_table = NULL;
2549 #ifdef DBUS_BUILD_TESTS
2560 do_load (const DBusString *full_path,
2562 dbus_bool_t oom_possible)
2564 BusConfigParser *parser;
2567 dbus_error_init (&error);
2569 parser = bus_config_load (full_path, TRUE, NULL, &error);
2572 _DBUS_ASSERT_ERROR_IS_SET (&error);
2575 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2577 _dbus_verbose ("Failed to load valid file due to OOM\n");
2578 dbus_error_free (&error);
2581 else if (validity == VALID)
2583 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2586 dbus_error_free (&error);
2591 dbus_error_free (&error);
2597 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2599 bus_config_parser_unref (parser);
2601 if (validity == INVALID)
2603 _dbus_warn ("Accepted invalid file\n");
2613 const DBusString *full_path;
2618 check_loader_oom_func (void *data)
2620 LoaderOomData *d = data;
2622 return do_load (d->full_path, d->validity, TRUE);
2626 process_test_valid_subdir (const DBusString *test_base_dir,
2630 DBusString test_directory;
2631 DBusString filename;
2639 if (!_dbus_string_init (&test_directory))
2640 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2642 _dbus_string_init_const (&filename, subdir);
2644 if (!_dbus_string_copy (test_base_dir, 0,
2645 &test_directory, 0))
2646 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2648 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2649 _dbus_assert_not_reached ("couldn't allocate full path");
2651 _dbus_string_free (&filename);
2652 if (!_dbus_string_init (&filename))
2653 _dbus_assert_not_reached ("didn't allocate filename string\n");
2655 dbus_error_init (&error);
2656 dir = _dbus_directory_open (&test_directory, &error);
2659 _dbus_warn ("Could not open %s: %s\n",
2660 _dbus_string_get_const_data (&test_directory),
2662 dbus_error_free (&error);
2666 if (validity == VALID)
2667 printf ("Testing valid files:\n");
2668 else if (validity == INVALID)
2669 printf ("Testing invalid files:\n");
2671 printf ("Testing unknown files:\n");
2674 while (_dbus_directory_get_next_file (dir, &filename, &error))
2676 DBusString full_path;
2679 if (!_dbus_string_init (&full_path))
2680 _dbus_assert_not_reached ("couldn't init string");
2682 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2683 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2685 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2686 _dbus_assert_not_reached ("couldn't concat file to dir");
2688 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2690 _dbus_verbose ("Skipping non-.conf file %s\n",
2691 _dbus_string_get_const_data (&filename));
2692 _dbus_string_free (&full_path);
2696 printf (" %s\n", _dbus_string_get_const_data (&filename));
2698 _dbus_verbose (" expecting %s\n",
2699 validity == VALID ? "valid" :
2700 (validity == INVALID ? "invalid" :
2701 (validity == UNKNOWN ? "unknown" : "???")));
2703 d.full_path = &full_path;
2704 d.validity = validity;
2706 /* FIXME hackaround for an expat problem, see
2707 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2708 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2710 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2711 if (!check_loader_oom_func (&d))
2712 _dbus_assert_not_reached ("test failed");
2714 _dbus_string_free (&full_path);
2717 if (dbus_error_is_set (&error))
2719 _dbus_warn ("Could not get next file in %s: %s\n",
2720 _dbus_string_get_const_data (&test_directory),
2722 dbus_error_free (&error);
2731 _dbus_directory_close (dir);
2732 _dbus_string_free (&test_directory);
2733 _dbus_string_free (&filename);
2739 bools_equal (dbus_bool_t a,
2746 strings_equal_or_both_null (const char *a,
2749 if (a == NULL || b == NULL)
2752 return !strcmp (a, b);
2756 elements_equal (const Element *a,
2759 if (a->type != b->type)
2762 if (!bools_equal (a->had_content, b->had_content))
2768 case ELEMENT_INCLUDE:
2769 if (!bools_equal (a->d.include.ignore_missing,
2770 b->d.include.ignore_missing))
2774 case ELEMENT_POLICY:
2775 if (a->d.policy.type != b->d.policy.type)
2777 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
2782 if (strcmp (a->d.limit.name, b->d.limit.name))
2784 if (a->d.limit.value != b->d.limit.value)
2798 lists_of_elements_equal (DBusList *a,
2807 while (ia != NULL && ib != NULL)
2809 if (elements_equal (ia->data, ib->data))
2811 ia = _dbus_list_get_next_link (&a, ia);
2812 ib = _dbus_list_get_next_link (&b, ib);
2815 return ia == NULL && ib == NULL;
2819 lists_of_c_strings_equal (DBusList *a,
2828 while (ia != NULL && ib != NULL)
2830 if (strcmp (ia->data, ib->data))
2832 ia = _dbus_list_get_next_link (&a, ia);
2833 ib = _dbus_list_get_next_link (&b, ib);
2836 return ia == NULL && ib == NULL;
2840 limits_equal (const BusLimits *a,
2844 (a->max_incoming_bytes == b->max_incoming_bytes
2845 || a->max_outgoing_bytes == b->max_outgoing_bytes
2846 || a->max_message_size == b->max_message_size
2847 || a->activation_timeout == b->activation_timeout
2848 || a->auth_timeout == b->auth_timeout
2849 || a->max_completed_connections == b->max_completed_connections
2850 || a->max_incomplete_connections == b->max_incomplete_connections
2851 || a->max_connections_per_user == b->max_connections_per_user
2852 || a->max_pending_activations == b->max_pending_activations
2853 || a->max_services_per_connection == b->max_services_per_connection
2854 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2855 || a->max_replies_per_connection == b->max_replies_per_connection
2856 || a->reply_timeout == b->reply_timeout);
2860 config_parsers_equal (const BusConfigParser *a,
2861 const BusConfigParser *b)
2863 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2866 if (!lists_of_elements_equal (a->stack, b->stack))
2869 if (!strings_equal_or_both_null (a->user, b->user))
2872 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2875 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2878 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2881 /* FIXME: compare policy */
2883 /* FIXME: compare service selinux ID table */
2885 if (! limits_equal (&a->limits, &b->limits))
2888 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2891 if (! bools_equal (a->fork, b->fork))
2894 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2901 all_are_equiv (const DBusString *target_directory)
2903 DBusString filename;
2905 BusConfigParser *first_parser;
2906 BusConfigParser *parser;
2912 first_parser = NULL;
2916 if (!_dbus_string_init (&filename))
2917 _dbus_assert_not_reached ("didn't allocate filename string");
2919 dbus_error_init (&error);
2920 dir = _dbus_directory_open (target_directory, &error);
2923 _dbus_warn ("Could not open %s: %s\n",
2924 _dbus_string_get_const_data (target_directory),
2926 dbus_error_free (&error);
2930 printf ("Comparing equivalent files:\n");
2933 while (_dbus_directory_get_next_file (dir, &filename, &error))
2935 DBusString full_path;
2937 if (!_dbus_string_init (&full_path))
2938 _dbus_assert_not_reached ("couldn't init string");
2940 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2941 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2943 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2944 _dbus_assert_not_reached ("couldn't concat file to dir");
2946 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2948 _dbus_verbose ("Skipping non-.conf file %s\n",
2949 _dbus_string_get_const_data (&filename));
2950 _dbus_string_free (&full_path);
2954 printf (" %s\n", _dbus_string_get_const_data (&filename));
2956 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2960 _dbus_warn ("Could not load file %s: %s\n",
2961 _dbus_string_get_const_data (&full_path),
2963 _dbus_string_free (&full_path);
2964 dbus_error_free (&error);
2967 else if (first_parser == NULL)
2969 _dbus_string_free (&full_path);
2970 first_parser = parser;
2974 _dbus_string_free (&full_path);
2975 equal = config_parsers_equal (first_parser, parser);
2976 bus_config_parser_unref (parser);
2985 _dbus_string_free (&filename);
2987 bus_config_parser_unref (first_parser);
2989 _dbus_directory_close (dir);
2996 process_test_equiv_subdir (const DBusString *test_base_dir,
2999 DBusString test_directory;
3000 DBusString filename;
3009 if (!_dbus_string_init (&test_directory))
3010 _dbus_assert_not_reached ("didn't allocate test_directory");
3012 _dbus_string_init_const (&filename, subdir);
3014 if (!_dbus_string_copy (test_base_dir, 0,
3015 &test_directory, 0))
3016 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3018 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3019 _dbus_assert_not_reached ("couldn't allocate full path");
3021 _dbus_string_free (&filename);
3022 if (!_dbus_string_init (&filename))
3023 _dbus_assert_not_reached ("didn't allocate filename string");
3025 dbus_error_init (&error);
3026 dir = _dbus_directory_open (&test_directory, &error);
3029 _dbus_warn ("Could not open %s: %s\n",
3030 _dbus_string_get_const_data (&test_directory),
3032 dbus_error_free (&error);
3036 while (_dbus_directory_get_next_file (dir, &filename, &error))
3038 DBusString full_path;
3040 /* Skip CVS's magic directories! */
3041 if (_dbus_string_equal_c_str (&filename, "CVS"))
3044 if (!_dbus_string_init (&full_path))
3045 _dbus_assert_not_reached ("couldn't init string");
3047 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3048 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3050 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3051 _dbus_assert_not_reached ("couldn't concat file to dir");
3053 equal = all_are_equiv (&full_path);
3054 _dbus_string_free (&full_path);
3063 _dbus_string_free (&test_directory);
3064 _dbus_string_free (&filename);
3066 _dbus_directory_close (dir);
3072 static const char *test_service_dir_matches[] =
3074 "/testusr/testlocal/testshare/dbus-1/services",
3075 "/testusr/testshare/dbus-1/services",
3076 DBUS_DATADIR"/dbus-1/services",
3077 "/testhome/foo/.testlocal/testshare/dbus-1/services",
3082 test_default_session_servicedirs (void)
3090 printf ("Testing retriving the default session service directories\n");
3091 if (!_dbus_get_standard_session_servicedirs (&dirs))
3092 _dbus_assert_not_reached ("couldn't get stardard dirs");
3094 /* make sure our defaults end with share/dbus-1/service */
3095 while ((link = _dbus_list_pop_first_link (&dirs)))
3099 printf (" default service dir: %s\n", (char *)link->data);
3100 _dbus_string_init_const (&path, (char *)link->data);
3101 if (!_dbus_string_ends_with_c_str (&path, "share/dbus-1/services"))
3103 printf ("error with default session service directories\n");
3107 dbus_free (link->data);
3108 _dbus_list_free_link (link);
3111 if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3112 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3114 if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3115 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3117 if (!_dbus_get_standard_session_servicedirs (&dirs))
3118 _dbus_assert_not_reached ("couldn't get stardard dirs");
3120 /* make sure we read and parse the env variable correctly */
3122 while ((link = _dbus_list_pop_first_link (&dirs)))
3124 printf (" test service dir: %s\n", (char *)link->data);
3125 if (test_service_dir_matches[i] == NULL)
3127 printf ("more directories parsed than in match set\n");
3131 if (strcmp (test_service_dir_matches[i],
3132 (char *)link->data) != 0)
3134 printf ("%s directory does not match %s in the match set\n",
3136 test_service_dir_matches[i]);
3142 dbus_free (link->data);
3143 _dbus_list_free_link (link);
3146 if (test_service_dir_matches[i] != NULL)
3148 printf ("extra data %s in the match set was not matched\n",
3149 test_service_dir_matches[i]);
3158 bus_config_parser_test (const DBusString *test_data_dir)
3160 if (test_data_dir == NULL ||
3161 _dbus_string_get_length (test_data_dir) == 0)
3163 printf ("No test data\n");
3167 if (!test_default_session_servicedirs())
3170 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3173 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3176 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3182 #endif /* DBUS_BUILD_TESTS */