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 && receive_requested_reply) ||
1185 (send_interface && own) ||
1186 (send_interface && user) ||
1187 (send_interface && group)) ||
1189 ((send_member && send_error) ||
1190 (send_member && receive_interface) ||
1191 (send_member && receive_member) ||
1192 (send_member && receive_error) ||
1193 (send_member && receive_sender) ||
1194 (send_member && receive_requested_reply) ||
1195 (send_member && own) ||
1196 (send_member && user) ||
1197 (send_member && group)) ||
1199 ((send_error && receive_interface) ||
1200 (send_error && receive_member) ||
1201 (send_error && receive_error) ||
1202 (send_error && receive_sender) ||
1203 (send_error && receive_requested_reply) ||
1204 (send_error && own) ||
1205 (send_error && user) ||
1206 (send_error && group)) ||
1208 ((send_destination && receive_interface) ||
1209 (send_destination && receive_member) ||
1210 (send_destination && receive_error) ||
1211 (send_destination && receive_sender) ||
1212 (send_destination && receive_requested_reply) ||
1213 (send_destination && own) ||
1214 (send_destination && user) ||
1215 (send_destination && group)) ||
1217 ((send_type && receive_interface) ||
1218 (send_type && receive_member) ||
1219 (send_type && receive_error) ||
1220 (send_type && receive_sender) ||
1221 (send_type && receive_requested_reply) ||
1222 (send_type && own) ||
1223 (send_type && user) ||
1224 (send_type && group)) ||
1226 ((send_path && receive_interface) ||
1227 (send_path && receive_member) ||
1228 (send_path && receive_error) ||
1229 (send_path && receive_sender) ||
1230 (send_path && receive_requested_reply) ||
1231 (send_path && own) ||
1232 (send_path && user) ||
1233 (send_path && group)) ||
1235 ((send_requested_reply && receive_interface) ||
1236 (send_requested_reply && receive_member) ||
1237 (send_requested_reply && receive_error) ||
1238 (send_requested_reply && receive_sender) ||
1239 (send_requested_reply && receive_requested_reply) ||
1240 (send_requested_reply && own) ||
1241 (send_requested_reply && user) ||
1242 (send_requested_reply && group)) ||
1244 ((receive_interface && receive_error) ||
1245 (receive_interface && own) ||
1246 (receive_interface && user) ||
1247 (receive_interface && group)) ||
1249 ((receive_member && receive_error) ||
1250 (receive_member && own) ||
1251 (receive_member && user) ||
1252 (receive_member && group)) ||
1254 ((receive_error && own) ||
1255 (receive_error && user) ||
1256 (receive_error && group)) ||
1258 ((eavesdrop && own) ||
1259 (eavesdrop && user) ||
1260 (eavesdrop && group)) ||
1262 ((receive_requested_reply && own) ||
1263 (receive_requested_reply && user) ||
1264 (receive_requested_reply && group)) ||
1271 dbus_set_error (error, DBUS_ERROR_FAILED,
1272 "Invalid combination of attributes on element <%s>",
1279 /* In BusPolicyRule, NULL represents wildcard.
1280 * In the config file, '*' represents it.
1282 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1284 if (send_interface || send_member || send_error || send_destination ||
1285 send_path || send_type || send_requested_reply)
1289 if (IS_WILDCARD (send_interface))
1290 send_interface = NULL;
1291 if (IS_WILDCARD (send_member))
1293 if (IS_WILDCARD (send_error))
1295 if (IS_WILDCARD (send_destination))
1296 send_destination = NULL;
1297 if (IS_WILDCARD (send_path))
1299 if (IS_WILDCARD (send_type))
1302 message_type = DBUS_MESSAGE_TYPE_INVALID;
1303 if (send_type != NULL)
1305 message_type = dbus_message_type_from_string (send_type);
1306 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1308 dbus_set_error (error, DBUS_ERROR_FAILED,
1309 "Bad message type \"%s\"",
1316 !(strcmp (eavesdrop, "true") == 0 ||
1317 strcmp (eavesdrop, "false") == 0))
1319 dbus_set_error (error, DBUS_ERROR_FAILED,
1320 "Bad value \"%s\" for %s attribute, must be true or false",
1321 "eavesdrop", eavesdrop);
1325 if (send_requested_reply &&
1326 !(strcmp (send_requested_reply, "true") == 0 ||
1327 strcmp (send_requested_reply, "false") == 0))
1329 dbus_set_error (error, DBUS_ERROR_FAILED,
1330 "Bad value \"%s\" for %s attribute, must be true or false",
1331 "send_requested_reply", send_requested_reply);
1335 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1340 rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1342 if (send_requested_reply)
1343 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1345 rule->d.send.message_type = message_type;
1346 rule->d.send.path = _dbus_strdup (send_path);
1347 rule->d.send.interface = _dbus_strdup (send_interface);
1348 rule->d.send.member = _dbus_strdup (send_member);
1349 rule->d.send.error = _dbus_strdup (send_error);
1350 rule->d.send.destination = _dbus_strdup (send_destination);
1351 if (send_path && rule->d.send.path == NULL)
1353 if (send_interface && rule->d.send.interface == NULL)
1355 if (send_member && rule->d.send.member == NULL)
1357 if (send_error && rule->d.send.error == NULL)
1359 if (send_destination && rule->d.send.destination == NULL)
1362 else if (receive_interface || receive_member || receive_error || receive_sender ||
1363 receive_path || receive_type || eavesdrop || receive_requested_reply)
1367 if (IS_WILDCARD (receive_interface))
1368 receive_interface = NULL;
1369 if (IS_WILDCARD (receive_member))
1370 receive_member = NULL;
1371 if (IS_WILDCARD (receive_error))
1372 receive_error = NULL;
1373 if (IS_WILDCARD (receive_sender))
1374 receive_sender = NULL;
1375 if (IS_WILDCARD (receive_path))
1376 receive_path = NULL;
1377 if (IS_WILDCARD (receive_type))
1378 receive_type = NULL;
1380 message_type = DBUS_MESSAGE_TYPE_INVALID;
1381 if (receive_type != NULL)
1383 message_type = dbus_message_type_from_string (receive_type);
1384 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1386 dbus_set_error (error, DBUS_ERROR_FAILED,
1387 "Bad message type \"%s\"",
1395 !(strcmp (eavesdrop, "true") == 0 ||
1396 strcmp (eavesdrop, "false") == 0))
1398 dbus_set_error (error, DBUS_ERROR_FAILED,
1399 "Bad value \"%s\" for %s attribute, must be true or false",
1400 "eavesdrop", eavesdrop);
1404 if (receive_requested_reply &&
1405 !(strcmp (receive_requested_reply, "true") == 0 ||
1406 strcmp (receive_requested_reply, "false") == 0))
1408 dbus_set_error (error, DBUS_ERROR_FAILED,
1409 "Bad value \"%s\" for %s attribute, must be true or false",
1410 "receive_requested_reply", receive_requested_reply);
1414 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1419 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1421 if (receive_requested_reply)
1422 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1424 rule->d.receive.message_type = message_type;
1425 rule->d.receive.path = _dbus_strdup (receive_path);
1426 rule->d.receive.interface = _dbus_strdup (receive_interface);
1427 rule->d.receive.member = _dbus_strdup (receive_member);
1428 rule->d.receive.error = _dbus_strdup (receive_error);
1429 rule->d.receive.origin = _dbus_strdup (receive_sender);
1431 if (receive_path && rule->d.receive.path == NULL)
1433 if (receive_interface && rule->d.receive.interface == NULL)
1435 if (receive_member && rule->d.receive.member == NULL)
1437 if (receive_error && rule->d.receive.error == NULL)
1439 if (receive_sender && rule->d.receive.origin == NULL)
1444 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1448 if (IS_WILDCARD (own))
1451 rule->d.own.service_name = _dbus_strdup (own);
1452 if (own && rule->d.own.service_name == NULL)
1457 if (IS_WILDCARD (user))
1459 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1463 rule->d.user.uid = DBUS_UID_UNSET;
1467 DBusString username;
1470 _dbus_string_init_const (&username, user);
1472 if (_dbus_get_user_id (&username, &uid))
1474 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1478 rule->d.user.uid = uid;
1482 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1483 user, element_name);
1489 if (IS_WILDCARD (group))
1491 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1495 rule->d.group.gid = DBUS_GID_UNSET;
1499 DBusString groupname;
1502 _dbus_string_init_const (&groupname, group);
1504 if (_dbus_get_user_id (&groupname, &gid))
1506 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1510 rule->d.group.gid = gid;
1514 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1515 group, element_name);
1520 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1526 pe = peek_element (parser);
1527 _dbus_assert (pe != NULL);
1528 _dbus_assert (pe->type == ELEMENT_POLICY);
1530 switch (pe->d.policy.type)
1532 case POLICY_IGNORED:
1533 /* drop the rule on the floor */
1536 case POLICY_DEFAULT:
1537 if (!bus_policy_append_default_rule (parser->policy, rule))
1540 case POLICY_MANDATORY:
1541 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1545 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1547 dbus_set_error (error, DBUS_ERROR_FAILED,
1548 "<%s> rule cannot be per-user because it has bus-global semantics",
1553 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1558 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1560 dbus_set_error (error, DBUS_ERROR_FAILED,
1561 "<%s> rule cannot be per-group because it has bus-global semantics",
1566 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1572 case POLICY_CONSOLE:
1573 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1579 bus_policy_rule_unref (rule);
1586 BUS_SET_OOM (error);
1589 bus_policy_rule_unref (rule);
1594 start_policy_child (BusConfigParser *parser,
1595 const char *element_name,
1596 const char **attribute_names,
1597 const char **attribute_values,
1600 if (strcmp (element_name, "allow") == 0)
1602 if (!append_rule_from_element (parser, element_name,
1603 attribute_names, attribute_values,
1607 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1609 BUS_SET_OOM (error);
1615 else if (strcmp (element_name, "deny") == 0)
1617 if (!append_rule_from_element (parser, element_name,
1618 attribute_names, attribute_values,
1622 if (push_element (parser, ELEMENT_DENY) == NULL)
1624 BUS_SET_OOM (error);
1632 dbus_set_error (error, DBUS_ERROR_FAILED,
1633 "Element <%s> not allowed inside <%s> in configuration file",
1634 element_name, "policy");
1640 start_selinux_child (BusConfigParser *parser,
1641 const char *element_name,
1642 const char **attribute_names,
1643 const char **attribute_values,
1650 context_copy = NULL;
1652 if (strcmp (element_name, "associate") == 0)
1655 const char *context;
1657 if (!locate_attributes (parser, "associate",
1662 "context", &context,
1666 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1668 BUS_SET_OOM (error);
1672 if (own == NULL || context == NULL)
1674 dbus_set_error (error, DBUS_ERROR_FAILED,
1675 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1679 own_copy = _dbus_strdup (own);
1680 if (own_copy == NULL)
1682 context_copy = _dbus_strdup (context);
1683 if (context_copy == NULL)
1686 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1687 own_copy, context_copy))
1694 dbus_set_error (error, DBUS_ERROR_FAILED,
1695 "Element <%s> not allowed inside <%s> in configuration file",
1696 element_name, "selinux");
1702 dbus_free (own_copy);
1705 dbus_free (context_copy);
1707 BUS_SET_OOM (error);
1712 bus_config_parser_start_element (BusConfigParser *parser,
1713 const char *element_name,
1714 const char **attribute_names,
1715 const char **attribute_values,
1720 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1722 /* printf ("START: %s\n", element_name); */
1724 t = top_element_type (parser);
1726 if (t == ELEMENT_NONE)
1728 if (strcmp (element_name, "busconfig") == 0)
1730 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1733 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1735 BUS_SET_OOM (error);
1743 dbus_set_error (error, DBUS_ERROR_FAILED,
1744 "Unknown element <%s> at root of configuration file",
1749 else if (t == ELEMENT_BUSCONFIG)
1751 return start_busconfig_child (parser, element_name,
1752 attribute_names, attribute_values,
1755 else if (t == ELEMENT_POLICY)
1757 return start_policy_child (parser, element_name,
1758 attribute_names, attribute_values,
1761 else if (t == ELEMENT_SELINUX)
1763 return start_selinux_child (parser, element_name,
1764 attribute_names, attribute_values,
1769 dbus_set_error (error, DBUS_ERROR_FAILED,
1770 "Element <%s> is not allowed in this context",
1777 set_limit (BusConfigParser *parser,
1782 dbus_bool_t must_be_positive;
1783 dbus_bool_t must_be_int;
1785 must_be_int = FALSE;
1786 must_be_positive = FALSE;
1788 if (strcmp (name, "max_incoming_bytes") == 0)
1790 must_be_positive = TRUE;
1791 parser->limits.max_incoming_bytes = value;
1793 else if (strcmp (name, "max_outgoing_bytes") == 0)
1795 must_be_positive = TRUE;
1796 parser->limits.max_outgoing_bytes = value;
1798 else if (strcmp (name, "max_message_size") == 0)
1800 must_be_positive = TRUE;
1801 parser->limits.max_message_size = value;
1803 else if (strcmp (name, "service_start_timeout") == 0)
1805 must_be_positive = TRUE;
1807 parser->limits.activation_timeout = value;
1809 else if (strcmp (name, "auth_timeout") == 0)
1811 must_be_positive = TRUE;
1813 parser->limits.auth_timeout = value;
1815 else if (strcmp (name, "reply_timeout") == 0)
1817 must_be_positive = TRUE;
1819 parser->limits.reply_timeout = value;
1821 else if (strcmp (name, "max_completed_connections") == 0)
1823 must_be_positive = TRUE;
1825 parser->limits.max_completed_connections = value;
1827 else if (strcmp (name, "max_incomplete_connections") == 0)
1829 must_be_positive = TRUE;
1831 parser->limits.max_incomplete_connections = value;
1833 else if (strcmp (name, "max_connections_per_user") == 0)
1835 must_be_positive = TRUE;
1837 parser->limits.max_connections_per_user = value;
1839 else if (strcmp (name, "max_pending_service_starts") == 0)
1841 must_be_positive = TRUE;
1843 parser->limits.max_pending_activations = value;
1845 else if (strcmp (name, "max_names_per_connection") == 0)
1847 must_be_positive = TRUE;
1849 parser->limits.max_services_per_connection = value;
1851 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1853 must_be_positive = TRUE;
1855 parser->limits.max_match_rules_per_connection = value;
1857 else if (strcmp (name, "max_replies_per_connection") == 0)
1859 must_be_positive = TRUE;
1861 parser->limits.max_replies_per_connection = value;
1865 dbus_set_error (error, DBUS_ERROR_FAILED,
1866 "There is no limit called \"%s\"\n",
1871 if (must_be_positive && value < 0)
1873 dbus_set_error (error, DBUS_ERROR_FAILED,
1874 "<limit name=\"%s\"> must be a positive number\n",
1880 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1882 dbus_set_error (error, DBUS_ERROR_FAILED,
1883 "<limit name=\"%s\"> value is too large\n",
1892 bus_config_parser_end_element (BusConfigParser *parser,
1893 const char *element_name,
1900 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1902 /* printf ("END: %s\n", element_name); */
1904 t = top_element_type (parser);
1906 if (t == ELEMENT_NONE)
1908 /* should probably be an assertion failure but
1909 * being paranoid about XML parsers
1911 dbus_set_error (error, DBUS_ERROR_FAILED,
1912 "XML parser ended element with no element on the stack");
1916 n = element_type_to_name (t);
1917 _dbus_assert (n != NULL);
1918 if (strcmp (n, element_name) != 0)
1920 /* should probably be an assertion failure but
1921 * being paranoid about XML parsers
1923 dbus_set_error (error, DBUS_ERROR_FAILED,
1924 "XML element <%s> ended but topmost element on the stack was <%s>",
1929 e = peek_element (parser);
1930 _dbus_assert (e != NULL);
1935 _dbus_assert_not_reached ("element in stack has no type");
1938 case ELEMENT_INCLUDE:
1941 case ELEMENT_LISTEN:
1942 case ELEMENT_PIDFILE:
1944 case ELEMENT_SERVICEDIR:
1945 case ELEMENT_INCLUDEDIR:
1947 if (!e->had_content)
1949 dbus_set_error (error, DBUS_ERROR_FAILED,
1950 "XML element <%s> was expected to have content inside it",
1951 element_type_to_name (e->type));
1955 if (e->type == ELEMENT_LIMIT)
1957 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1963 case ELEMENT_BUSCONFIG:
1964 case ELEMENT_POLICY:
1968 case ELEMENT_SELINUX:
1969 case ELEMENT_ASSOCIATE:
1970 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
1974 pop_element (parser);
1980 all_whitespace (const DBusString *str)
1984 _dbus_string_skip_white (str, 0, &i);
1986 return i == _dbus_string_get_length (str);
1990 make_full_path (const DBusString *basedir,
1991 const DBusString *filename,
1992 DBusString *full_path)
1994 if (_dbus_path_is_absolute (filename))
1996 return _dbus_string_copy (filename, 0, full_path, 0);
2000 if (!_dbus_string_copy (basedir, 0, full_path, 0))
2003 if (!_dbus_concat_dir_and_file (full_path, filename))
2011 include_file (BusConfigParser *parser,
2012 const DBusString *filename,
2013 dbus_bool_t ignore_missing,
2016 /* FIXME good test case for this would load each config file in the
2017 * test suite both alone, and as an include, and check
2018 * that the result is the same
2020 BusConfigParser *included;
2021 const char *filename_str;
2022 DBusError tmp_error;
2024 dbus_error_init (&tmp_error);
2026 filename_str = _dbus_string_get_const_data (filename);
2028 /* Check to make sure this file hasn't already been included. */
2029 if (seen_include (parser, filename))
2031 dbus_set_error (error, DBUS_ERROR_FAILED,
2032 "Circular inclusion of file '%s'",
2037 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2039 BUS_SET_OOM (error);
2043 /* Since parser is passed in as the parent, included
2044 inherits parser's limits. */
2045 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2047 _dbus_list_pop_last (&parser->included_files);
2049 if (included == NULL)
2051 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2053 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2056 dbus_error_free (&tmp_error);
2061 dbus_move_error (&tmp_error, error);
2067 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2069 if (!merge_included (parser, included, error))
2071 bus_config_parser_unref (included);
2075 /* Copy included's limits back to parser. */
2076 parser->limits = included->limits;
2078 bus_config_parser_unref (included);
2084 include_dir (BusConfigParser *parser,
2085 const DBusString *dirname,
2088 DBusString filename;
2090 DBusError tmp_error;
2094 if (!_dbus_string_init (&filename))
2096 BUS_SET_OOM (error);
2102 dir = _dbus_directory_open (dirname, error);
2107 dbus_error_init (&tmp_error);
2108 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2110 DBusString full_path;
2112 if (!_dbus_string_init (&full_path))
2114 BUS_SET_OOM (error);
2118 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2120 BUS_SET_OOM (error);
2121 _dbus_string_free (&full_path);
2125 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2127 BUS_SET_OOM (error);
2128 _dbus_string_free (&full_path);
2132 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2134 if (!include_file (parser, &full_path, TRUE, error))
2136 _dbus_string_free (&full_path);
2141 _dbus_string_free (&full_path);
2144 if (dbus_error_is_set (&tmp_error))
2146 dbus_move_error (&tmp_error, error);
2151 if (!_dbus_string_copy_data (dirname, &s))
2153 BUS_SET_OOM (error);
2157 if (!_dbus_list_append (&parser->conf_dirs, s))
2160 BUS_SET_OOM (error);
2167 _dbus_string_free (&filename);
2170 _dbus_directory_close (dir);
2176 bus_config_parser_content (BusConfigParser *parser,
2177 const DBusString *content,
2182 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2188 _dbus_string_get_const_data (content, &c_str);
2190 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2194 e = peek_element (parser);
2197 dbus_set_error (error, DBUS_ERROR_FAILED,
2198 "Text content outside of any XML element in configuration file");
2201 else if (e->had_content)
2203 _dbus_assert_not_reached ("Element had multiple content blocks");
2207 switch (top_element_type (parser))
2210 _dbus_assert_not_reached ("element at top of stack has no type");
2213 case ELEMENT_BUSCONFIG:
2214 case ELEMENT_POLICY:
2218 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2219 case ELEMENT_SELINUX:
2220 case ELEMENT_ASSOCIATE:
2221 if (all_whitespace (content))
2225 dbus_set_error (error, DBUS_ERROR_FAILED,
2226 "No text content expected inside XML element %s in configuration file",
2227 element_type_to_name (top_element_type (parser)));
2231 case ELEMENT_PIDFILE:
2235 e->had_content = TRUE;
2237 if (!_dbus_string_copy_data (content, &s))
2240 dbus_free (parser->pidfile);
2241 parser->pidfile = s;
2245 case ELEMENT_INCLUDE:
2247 DBusString full_path, selinux_policy_root;
2249 e->had_content = TRUE;
2251 if (e->d.include.if_selinux_enabled
2252 && !bus_selinux_enabled ())
2255 if (!_dbus_string_init (&full_path))
2258 if (e->d.include.selinux_root_relative)
2260 if (!bus_selinux_get_policy_root ())
2262 dbus_set_error (error, DBUS_ERROR_FAILED,
2263 "Could not determine SELinux policy root for relative inclusion");
2264 _dbus_string_free (&full_path);
2267 _dbus_string_init_const (&selinux_policy_root,
2268 bus_selinux_get_policy_root ());
2269 if (!make_full_path (&selinux_policy_root, content, &full_path))
2271 _dbus_string_free (&full_path);
2275 else if (!make_full_path (&parser->basedir, content, &full_path))
2277 _dbus_string_free (&full_path);
2281 if (!include_file (parser, &full_path,
2282 e->d.include.ignore_missing, error))
2284 _dbus_string_free (&full_path);
2288 _dbus_string_free (&full_path);
2292 case ELEMENT_INCLUDEDIR:
2294 DBusString full_path;
2296 e->had_content = TRUE;
2298 if (!_dbus_string_init (&full_path))
2301 if (!make_full_path (&parser->basedir, content, &full_path))
2303 _dbus_string_free (&full_path);
2307 if (!include_dir (parser, &full_path, error))
2309 _dbus_string_free (&full_path);
2313 _dbus_string_free (&full_path);
2321 e->had_content = TRUE;
2323 if (!_dbus_string_copy_data (content, &s))
2326 dbus_free (parser->user);
2335 e->had_content = TRUE;
2337 if (!_dbus_string_copy_data (content, &s))
2340 dbus_free (parser->bus_type);
2341 parser->bus_type = s;
2345 case ELEMENT_LISTEN:
2349 e->had_content = TRUE;
2351 if (!_dbus_string_copy_data (content, &s))
2354 if (!_dbus_list_append (&parser->listen_on,
2367 e->had_content = TRUE;
2369 if (!_dbus_string_copy_data (content, &s))
2372 if (!_dbus_list_append (&parser->mechanisms,
2381 case ELEMENT_SERVICEDIR:
2384 DBusString full_path;
2386 e->had_content = TRUE;
2388 if (!_dbus_string_init (&full_path))
2391 if (!make_full_path (&parser->basedir, content, &full_path))
2393 _dbus_string_free (&full_path);
2397 if (!_dbus_string_copy_data (&full_path, &s))
2399 _dbus_string_free (&full_path);
2403 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2405 _dbus_string_free (&full_path);
2410 _dbus_string_free (&full_path);
2418 e->had_content = TRUE;
2421 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2423 dbus_set_error (error, DBUS_ERROR_FAILED,
2424 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2429 e->d.limit.value = val;
2431 _dbus_verbose ("Loaded value %ld for limit %s\n",
2438 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2442 BUS_SET_OOM (error);
2447 bus_config_parser_finished (BusConfigParser *parser,
2450 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2452 if (parser->stack != NULL)
2454 dbus_set_error (error, DBUS_ERROR_FAILED,
2455 "Element <%s> was not closed in configuration file",
2456 element_type_to_name (top_element_type (parser)));
2461 if (parser->is_toplevel && parser->listen_on == NULL)
2463 dbus_set_error (error, DBUS_ERROR_FAILED,
2464 "Configuration file needs one or more <listen> elements giving addresses");
2472 bus_config_parser_get_user (BusConfigParser *parser)
2474 return parser->user;
2478 bus_config_parser_get_type (BusConfigParser *parser)
2480 return parser->bus_type;
2484 bus_config_parser_get_addresses (BusConfigParser *parser)
2486 return &parser->listen_on;
2490 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2492 return &parser->mechanisms;
2496 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2498 return &parser->service_dirs;
2502 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2504 return &parser->conf_dirs;
2508 bus_config_parser_get_fork (BusConfigParser *parser)
2510 return parser->fork;
2514 bus_config_parser_get_pidfile (BusConfigParser *parser)
2516 return parser->pidfile;
2520 bus_config_parser_steal_policy (BusConfigParser *parser)
2524 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2526 policy = parser->policy;
2528 parser->policy = NULL;
2533 /* Overwrite any limits that were set in the configuration file */
2535 bus_config_parser_get_limits (BusConfigParser *parser,
2538 *limits = parser->limits;
2542 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2544 DBusHashTable *table;
2546 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2548 table = parser->service_context_table;
2550 parser->service_context_table = NULL;
2555 #ifdef DBUS_BUILD_TESTS
2566 do_load (const DBusString *full_path,
2568 dbus_bool_t oom_possible)
2570 BusConfigParser *parser;
2573 dbus_error_init (&error);
2575 parser = bus_config_load (full_path, TRUE, NULL, &error);
2578 _DBUS_ASSERT_ERROR_IS_SET (&error);
2581 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2583 _dbus_verbose ("Failed to load valid file due to OOM\n");
2584 dbus_error_free (&error);
2587 else if (validity == VALID)
2589 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2592 dbus_error_free (&error);
2597 dbus_error_free (&error);
2603 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2605 bus_config_parser_unref (parser);
2607 if (validity == INVALID)
2609 _dbus_warn ("Accepted invalid file\n");
2619 const DBusString *full_path;
2624 check_loader_oom_func (void *data)
2626 LoaderOomData *d = data;
2628 return do_load (d->full_path, d->validity, TRUE);
2632 process_test_valid_subdir (const DBusString *test_base_dir,
2636 DBusString test_directory;
2637 DBusString filename;
2645 if (!_dbus_string_init (&test_directory))
2646 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2648 _dbus_string_init_const (&filename, subdir);
2650 if (!_dbus_string_copy (test_base_dir, 0,
2651 &test_directory, 0))
2652 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2654 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2655 _dbus_assert_not_reached ("couldn't allocate full path");
2657 _dbus_string_free (&filename);
2658 if (!_dbus_string_init (&filename))
2659 _dbus_assert_not_reached ("didn't allocate filename string\n");
2661 dbus_error_init (&error);
2662 dir = _dbus_directory_open (&test_directory, &error);
2665 _dbus_warn ("Could not open %s: %s\n",
2666 _dbus_string_get_const_data (&test_directory),
2668 dbus_error_free (&error);
2672 if (validity == VALID)
2673 printf ("Testing valid files:\n");
2674 else if (validity == INVALID)
2675 printf ("Testing invalid files:\n");
2677 printf ("Testing unknown files:\n");
2680 while (_dbus_directory_get_next_file (dir, &filename, &error))
2682 DBusString full_path;
2685 if (!_dbus_string_init (&full_path))
2686 _dbus_assert_not_reached ("couldn't init string");
2688 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2689 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2691 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2692 _dbus_assert_not_reached ("couldn't concat file to dir");
2694 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2696 _dbus_verbose ("Skipping non-.conf file %s\n",
2697 _dbus_string_get_const_data (&filename));
2698 _dbus_string_free (&full_path);
2702 printf (" %s\n", _dbus_string_get_const_data (&filename));
2704 _dbus_verbose (" expecting %s\n",
2705 validity == VALID ? "valid" :
2706 (validity == INVALID ? "invalid" :
2707 (validity == UNKNOWN ? "unknown" : "???")));
2709 d.full_path = &full_path;
2710 d.validity = validity;
2712 /* FIXME hackaround for an expat problem, see
2713 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2714 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2716 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2717 if (!check_loader_oom_func (&d))
2718 _dbus_assert_not_reached ("test failed");
2720 _dbus_string_free (&full_path);
2723 if (dbus_error_is_set (&error))
2725 _dbus_warn ("Could not get next file in %s: %s\n",
2726 _dbus_string_get_const_data (&test_directory),
2728 dbus_error_free (&error);
2737 _dbus_directory_close (dir);
2738 _dbus_string_free (&test_directory);
2739 _dbus_string_free (&filename);
2745 bools_equal (dbus_bool_t a,
2752 strings_equal_or_both_null (const char *a,
2755 if (a == NULL || b == NULL)
2758 return !strcmp (a, b);
2762 elements_equal (const Element *a,
2765 if (a->type != b->type)
2768 if (!bools_equal (a->had_content, b->had_content))
2774 case ELEMENT_INCLUDE:
2775 if (!bools_equal (a->d.include.ignore_missing,
2776 b->d.include.ignore_missing))
2780 case ELEMENT_POLICY:
2781 if (a->d.policy.type != b->d.policy.type)
2783 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
2788 if (strcmp (a->d.limit.name, b->d.limit.name))
2790 if (a->d.limit.value != b->d.limit.value)
2804 lists_of_elements_equal (DBusList *a,
2813 while (ia != NULL && ib != NULL)
2815 if (elements_equal (ia->data, ib->data))
2817 ia = _dbus_list_get_next_link (&a, ia);
2818 ib = _dbus_list_get_next_link (&b, ib);
2821 return ia == NULL && ib == NULL;
2825 lists_of_c_strings_equal (DBusList *a,
2834 while (ia != NULL && ib != NULL)
2836 if (strcmp (ia->data, ib->data))
2838 ia = _dbus_list_get_next_link (&a, ia);
2839 ib = _dbus_list_get_next_link (&b, ib);
2842 return ia == NULL && ib == NULL;
2846 limits_equal (const BusLimits *a,
2850 (a->max_incoming_bytes == b->max_incoming_bytes
2851 || a->max_outgoing_bytes == b->max_outgoing_bytes
2852 || a->max_message_size == b->max_message_size
2853 || a->activation_timeout == b->activation_timeout
2854 || a->auth_timeout == b->auth_timeout
2855 || a->max_completed_connections == b->max_completed_connections
2856 || a->max_incomplete_connections == b->max_incomplete_connections
2857 || a->max_connections_per_user == b->max_connections_per_user
2858 || a->max_pending_activations == b->max_pending_activations
2859 || a->max_services_per_connection == b->max_services_per_connection
2860 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2861 || a->max_replies_per_connection == b->max_replies_per_connection
2862 || a->reply_timeout == b->reply_timeout);
2866 config_parsers_equal (const BusConfigParser *a,
2867 const BusConfigParser *b)
2869 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2872 if (!lists_of_elements_equal (a->stack, b->stack))
2875 if (!strings_equal_or_both_null (a->user, b->user))
2878 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2881 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2884 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2887 /* FIXME: compare policy */
2889 /* FIXME: compare service selinux ID table */
2891 if (! limits_equal (&a->limits, &b->limits))
2894 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2897 if (! bools_equal (a->fork, b->fork))
2900 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2907 all_are_equiv (const DBusString *target_directory)
2909 DBusString filename;
2911 BusConfigParser *first_parser;
2912 BusConfigParser *parser;
2918 first_parser = NULL;
2922 if (!_dbus_string_init (&filename))
2923 _dbus_assert_not_reached ("didn't allocate filename string");
2925 dbus_error_init (&error);
2926 dir = _dbus_directory_open (target_directory, &error);
2929 _dbus_warn ("Could not open %s: %s\n",
2930 _dbus_string_get_const_data (target_directory),
2932 dbus_error_free (&error);
2936 printf ("Comparing equivalent files:\n");
2939 while (_dbus_directory_get_next_file (dir, &filename, &error))
2941 DBusString full_path;
2943 if (!_dbus_string_init (&full_path))
2944 _dbus_assert_not_reached ("couldn't init string");
2946 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2947 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2949 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2950 _dbus_assert_not_reached ("couldn't concat file to dir");
2952 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2954 _dbus_verbose ("Skipping non-.conf file %s\n",
2955 _dbus_string_get_const_data (&filename));
2956 _dbus_string_free (&full_path);
2960 printf (" %s\n", _dbus_string_get_const_data (&filename));
2962 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2966 _dbus_warn ("Could not load file %s: %s\n",
2967 _dbus_string_get_const_data (&full_path),
2969 _dbus_string_free (&full_path);
2970 dbus_error_free (&error);
2973 else if (first_parser == NULL)
2975 _dbus_string_free (&full_path);
2976 first_parser = parser;
2980 _dbus_string_free (&full_path);
2981 equal = config_parsers_equal (first_parser, parser);
2982 bus_config_parser_unref (parser);
2991 _dbus_string_free (&filename);
2993 bus_config_parser_unref (first_parser);
2995 _dbus_directory_close (dir);
3002 process_test_equiv_subdir (const DBusString *test_base_dir,
3005 DBusString test_directory;
3006 DBusString filename;
3015 if (!_dbus_string_init (&test_directory))
3016 _dbus_assert_not_reached ("didn't allocate test_directory");
3018 _dbus_string_init_const (&filename, subdir);
3020 if (!_dbus_string_copy (test_base_dir, 0,
3021 &test_directory, 0))
3022 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3024 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3025 _dbus_assert_not_reached ("couldn't allocate full path");
3027 _dbus_string_free (&filename);
3028 if (!_dbus_string_init (&filename))
3029 _dbus_assert_not_reached ("didn't allocate filename string");
3031 dbus_error_init (&error);
3032 dir = _dbus_directory_open (&test_directory, &error);
3035 _dbus_warn ("Could not open %s: %s\n",
3036 _dbus_string_get_const_data (&test_directory),
3038 dbus_error_free (&error);
3042 while (_dbus_directory_get_next_file (dir, &filename, &error))
3044 DBusString full_path;
3046 /* Skip CVS's magic directories! */
3047 if (_dbus_string_equal_c_str (&filename, "CVS"))
3050 if (!_dbus_string_init (&full_path))
3051 _dbus_assert_not_reached ("couldn't init string");
3053 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3054 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3056 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3057 _dbus_assert_not_reached ("couldn't concat file to dir");
3059 equal = all_are_equiv (&full_path);
3060 _dbus_string_free (&full_path);
3069 _dbus_string_free (&test_directory);
3070 _dbus_string_free (&filename);
3072 _dbus_directory_close (dir);
3078 static const char *test_service_dir_matches[] =
3080 DBUS_DATADIR"/dbus-1/services",
3082 "/testusr/testlocal/testshare/dbus-1/services",
3083 "/testusr/testshare/dbus-1/services",
3084 "/testhome/foo/.testlocal/testshare/dbus-1/services",
3090 test_default_session_servicedirs (void)
3095 const char *common_progs;
3098 common_progs = _dbus_getenv ("CommonProgramFiles");
3101 if (!_dbus_string_init (&progs))
3104 if (!_dbus_string_append (&progs, common_progs))
3106 _dbus_string_free (&progs);
3110 if (!_dbus_string_append (&progs, "/dbus-1/services"))
3112 _dbus_string_free (&progs);
3115 test_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3119 printf ("Testing retrieving the default session service directories\n");
3120 if (!_dbus_get_standard_session_servicedirs (&dirs))
3121 _dbus_assert_not_reached ("couldn't get stardard dirs");
3123 /* make sure our defaults end with share/dbus-1/service */
3124 while ((link = _dbus_list_pop_first_link (&dirs)))
3128 printf (" default service dir: %s\n", (char *)link->data);
3129 _dbus_string_init_const (&path, (char *)link->data);
3130 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/services"))
3132 printf ("error with default session service directories\n");
3133 dbus_free (link->data);
3134 _dbus_list_free_link (link);
3135 _dbus_string_free (&progs);
3139 dbus_free (link->data);
3140 _dbus_list_free_link (link);
3144 if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3145 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3147 if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3148 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3150 if (!_dbus_get_standard_session_servicedirs (&dirs))
3151 _dbus_assert_not_reached ("couldn't get stardard dirs");
3153 /* make sure we read and parse the env variable correctly */
3155 while ((link = _dbus_list_pop_first_link (&dirs)))
3157 printf (" test service dir: %s\n", (char *)link->data);
3158 if (test_service_dir_matches[i] == NULL)
3160 printf ("more directories parsed than in match set\n");
3161 dbus_free (link->data);
3162 _dbus_list_free_link (link);
3163 _dbus_string_free (&progs);
3167 if (strcmp (test_service_dir_matches[i],
3168 (char *)link->data) != 0)
3170 printf ("%s directory does not match %s in the match set\n",
3172 test_service_dir_matches[i]);
3173 dbus_free (link->data);
3174 _dbus_list_free_link (link);
3175 _dbus_string_free (&progs);
3181 dbus_free (link->data);
3182 _dbus_list_free_link (link);
3185 if (test_service_dir_matches[i] != NULL)
3187 printf ("extra data %s in the match set was not matched\n",
3188 test_service_dir_matches[i]);
3190 _dbus_string_free (&progs);
3194 _dbus_string_free (&progs);
3199 bus_config_parser_test (const DBusString *test_data_dir)
3201 if (test_data_dir == NULL ||
3202 _dbus_string_get_length (test_data_dir) == 0)
3204 printf ("No test data\n");
3208 if (!test_default_session_servicedirs())
3211 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3214 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3217 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3223 #endif /* DBUS_BUILD_TESTS */