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>
51 ELEMENT_STANDARD_SESSION_SERVICEDIRS
56 /* we ignore policies for unknown groups/users */
71 unsigned int had_content : 1;
77 unsigned int ignore_missing : 1;
78 unsigned int if_selinux_enabled : 1;
79 unsigned int selinux_root_relative : 1;
85 unsigned long gid_uid_or_at_console;
99 * Parser for bus configuration file.
101 struct BusConfigParser
103 int refcount; /**< Reference count */
105 DBusString basedir; /**< Directory we resolve paths relative to */
107 DBusList *stack; /**< stack of Element */
109 char *user; /**< user to run as */
111 char *bus_type; /**< Message bus type */
113 DBusList *listen_on; /**< List of addresses to listen to */
115 DBusList *mechanisms; /**< Auth mechanisms */
117 DBusList *service_dirs; /**< Directories to look for services in */
119 DBusList *conf_dirs; /**< Directories to look for policy configuration in */
121 BusPolicy *policy; /**< Security policy */
123 BusLimits limits; /**< Limits */
125 char *pidfile; /**< PID file */
127 DBusList *included_files; /**< Included files stack */
129 DBusHashTable *service_context_table; /**< Map service names to SELinux contexts */
131 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
133 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
137 element_type_to_name (ElementType type)
143 case ELEMENT_BUSCONFIG:
145 case ELEMENT_INCLUDE:
163 case ELEMENT_PIDFILE:
165 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
166 return "standard_session_servicedirs";
167 case ELEMENT_SERVICEDIR:
169 case ELEMENT_INCLUDEDIR:
173 case ELEMENT_SELINUX:
175 case ELEMENT_ASSOCIATE:
179 _dbus_assert_not_reached ("bad element type");
185 push_element (BusConfigParser *parser,
190 _dbus_assert (type != ELEMENT_NONE);
192 e = dbus_new0 (Element, 1);
196 if (!_dbus_list_append (&parser->stack, e))
208 element_free (Element *e)
210 if (e->type == ELEMENT_LIMIT)
211 dbus_free (e->d.limit.name);
217 pop_element (BusConfigParser *parser)
221 e = _dbus_list_pop_last (&parser->stack);
227 peek_element (BusConfigParser *parser)
231 e = _dbus_list_get_last (&parser->stack);
237 top_element_type (BusConfigParser *parser)
241 e = _dbus_list_get_last (&parser->stack);
250 merge_service_context_hash (DBusHashTable *dest,
260 _dbus_hash_iter_init (from, &iter);
261 while (_dbus_hash_iter_next (&iter))
263 const char *service = _dbus_hash_iter_get_string_key (&iter);
264 const char *context = _dbus_hash_iter_get_value (&iter);
266 service_copy = _dbus_strdup (service);
267 if (service_copy == NULL)
269 context_copy = _dbus_strdup (context);
270 if (context_copy == NULL)
273 if (!_dbus_hash_table_insert_string (dest, service_copy, context_copy))
284 dbus_free (service_copy);
287 dbus_free (context_copy);
293 service_dirs_find_dir (DBusList **service_dirs,
298 _dbus_assert (dir != NULL);
300 for (link = *service_dirs; link; link = _dbus_list_get_next_link(service_dirs, link))
302 const char *link_dir;
304 link_dir = (const char *)link->data;
305 if (strcmp (dir, link_dir) == 0)
313 service_dirs_append_unique_or_free (DBusList **service_dirs,
316 if (!service_dirs_find_dir (service_dirs, dir))
317 return _dbus_list_append (service_dirs, dir);
324 service_dirs_append_link_unique_or_free (DBusList **service_dirs,
327 if (!service_dirs_find_dir (service_dirs, dir_link->data))
329 _dbus_list_append_link (service_dirs, dir_link);
333 dbus_free (dir_link->data);
334 _dbus_list_free_link (dir_link);
339 merge_included (BusConfigParser *parser,
340 BusConfigParser *included,
345 if (!bus_policy_merge (parser->policy,
352 if (!merge_service_context_hash (parser->service_context_table,
353 included->service_context_table))
359 if (included->user != NULL)
361 dbus_free (parser->user);
362 parser->user = included->user;
363 included->user = NULL;
366 if (included->bus_type != NULL)
368 dbus_free (parser->bus_type);
369 parser->bus_type = included->bus_type;
370 included->bus_type = NULL;
376 if (included->pidfile != NULL)
378 dbus_free (parser->pidfile);
379 parser->pidfile = included->pidfile;
380 included->pidfile = NULL;
383 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
384 _dbus_list_append_link (&parser->listen_on, link);
386 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
387 _dbus_list_append_link (&parser->mechanisms, link);
389 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
390 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
392 while ((link = _dbus_list_pop_first_link (&included->conf_dirs)))
393 _dbus_list_append_link (&parser->conf_dirs, link);
399 seen_include (BusConfigParser *parser,
400 const DBusString *file)
404 iter = parser->included_files;
407 if (! strcmp (_dbus_string_get_const_data (file), iter->data))
410 iter = _dbus_list_get_next_link (&parser->included_files, iter);
417 bus_config_parser_new (const DBusString *basedir,
418 dbus_bool_t is_toplevel,
419 const BusConfigParser *parent)
421 BusConfigParser *parser;
423 parser = dbus_new0 (BusConfigParser, 1);
427 parser->is_toplevel = !!is_toplevel;
429 if (!_dbus_string_init (&parser->basedir))
435 if (((parser->policy = bus_policy_new ()) == NULL) ||
436 !_dbus_string_copy (basedir, 0, &parser->basedir, 0) ||
437 ((parser->service_context_table = _dbus_hash_table_new (DBUS_HASH_STRING,
439 dbus_free)) == NULL))
442 bus_policy_unref (parser->policy);
444 _dbus_string_free (&parser->basedir);
452 /* Initialize the parser's limits from the parent. */
453 parser->limits = parent->limits;
455 /* Use the parent's list of included_files to avoid
456 circular inclusions. */
457 parser->included_files = parent->included_files;
462 /* Make up some numbers! woot! */
463 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 127;
464 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 127;
465 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
467 /* Making this long means the user has to wait longer for an error
468 * message if something screws up, but making it too short means
469 * they might see a false failure.
471 parser->limits.activation_timeout = 25000; /* 25 seconds */
473 /* Making this long risks making a DOS attack easier, but too short
474 * and legitimate auth will fail. If interactive auth (ask user for
475 * password) is allowed, then potentially it has to be quite long.
477 parser->limits.auth_timeout = 30000; /* 30 seconds */
479 parser->limits.max_incomplete_connections = 64;
480 parser->limits.max_connections_per_user = 256;
482 /* Note that max_completed_connections / max_connections_per_user
483 * is the number of users that would have to work together to
484 * DOS all the other users.
486 parser->limits.max_completed_connections = 2048;
488 parser->limits.max_pending_activations = 512;
489 parser->limits.max_services_per_connection = 512;
491 /* For this one, keep in mind that it isn't only the memory used
492 * by the match rules, but slowdown from linearly walking a big
493 * list of them. A client adding more than this is almost
494 * certainly a bad idea for that reason, and should change to a
495 * smaller number of wider-net match rules - getting every last
496 * message to the bus is probably better than having a thousand
499 parser->limits.max_match_rules_per_connection = 512;
501 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
503 /* this is effectively a limit on message queue size for messages
504 * that require a reply
506 parser->limits.max_replies_per_connection = 1024*8;
509 parser->refcount = 1;
515 bus_config_parser_ref (BusConfigParser *parser)
517 _dbus_assert (parser->refcount > 0);
519 parser->refcount += 1;
525 bus_config_parser_unref (BusConfigParser *parser)
527 _dbus_assert (parser->refcount > 0);
529 parser->refcount -= 1;
531 if (parser->refcount == 0)
533 while (parser->stack != NULL)
534 pop_element (parser);
536 dbus_free (parser->user);
537 dbus_free (parser->bus_type);
538 dbus_free (parser->pidfile);
540 _dbus_list_foreach (&parser->listen_on,
541 (DBusForeachFunction) dbus_free,
544 _dbus_list_clear (&parser->listen_on);
546 _dbus_list_foreach (&parser->service_dirs,
547 (DBusForeachFunction) dbus_free,
550 _dbus_list_clear (&parser->service_dirs);
552 _dbus_list_foreach (&parser->conf_dirs,
553 (DBusForeachFunction) dbus_free,
556 _dbus_list_clear (&parser->conf_dirs);
558 _dbus_list_foreach (&parser->mechanisms,
559 (DBusForeachFunction) dbus_free,
562 _dbus_list_clear (&parser->mechanisms);
564 _dbus_string_free (&parser->basedir);
567 bus_policy_unref (parser->policy);
569 if (parser->service_context_table)
570 _dbus_hash_table_unref (parser->service_context_table);
577 bus_config_parser_check_doctype (BusConfigParser *parser,
581 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
583 if (strcmp (doctype, "busconfig") != 0)
585 dbus_set_error (error,
587 "Configuration file has the wrong document type %s",
602 locate_attributes (BusConfigParser *parser,
603 const char *element_name,
604 const char **attribute_names,
605 const char **attribute_values,
607 const char *first_attribute_name,
608 const char **first_attribute_retloc,
616 LocateAttr attrs[MAX_ATTRS];
620 _dbus_assert (first_attribute_name != NULL);
621 _dbus_assert (first_attribute_retloc != NULL);
626 attrs[0].name = first_attribute_name;
627 attrs[0].retloc = first_attribute_retloc;
628 *first_attribute_retloc = NULL;
630 va_start (args, first_attribute_retloc);
632 name = va_arg (args, const char*);
633 retloc = va_arg (args, const char**);
637 _dbus_assert (retloc != NULL);
638 _dbus_assert (n_attrs < MAX_ATTRS);
640 attrs[n_attrs].name = name;
641 attrs[n_attrs].retloc = retloc;
645 name = va_arg (args, const char*);
646 retloc = va_arg (args, const char**);
655 while (attribute_names[i])
664 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
666 retloc = attrs[j].retloc;
670 dbus_set_error (error, DBUS_ERROR_FAILED,
671 "Attribute \"%s\" repeated twice on the same <%s> element",
672 attrs[j].name, element_name);
677 *retloc = attribute_values[i];
686 dbus_set_error (error, DBUS_ERROR_FAILED,
687 "Attribute \"%s\" is invalid on <%s> element in this context",
688 attribute_names[i], element_name);
701 check_no_attributes (BusConfigParser *parser,
702 const char *element_name,
703 const char **attribute_names,
704 const char **attribute_values,
707 if (attribute_names[0] != NULL)
709 dbus_set_error (error, DBUS_ERROR_FAILED,
710 "Attribute \"%s\" is invalid on <%s> element in this context",
711 attribute_names[0], element_name);
719 start_busconfig_child (BusConfigParser *parser,
720 const char *element_name,
721 const char **attribute_names,
722 const char **attribute_values,
725 if (strcmp (element_name, "user") == 0)
727 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
730 if (push_element (parser, ELEMENT_USER) == NULL)
738 else if (strcmp (element_name, "type") == 0)
740 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
743 if (push_element (parser, ELEMENT_TYPE) == NULL)
751 else if (strcmp (element_name, "fork") == 0)
753 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
756 if (push_element (parser, ELEMENT_FORK) == NULL)
766 else if (strcmp (element_name, "pidfile") == 0)
768 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
771 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
779 else if (strcmp (element_name, "listen") == 0)
781 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
784 if (push_element (parser, ELEMENT_LISTEN) == NULL)
792 else if (strcmp (element_name, "auth") == 0)
794 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
797 if (push_element (parser, ELEMENT_AUTH) == NULL)
805 else if (strcmp (element_name, "includedir") == 0)
807 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
810 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
818 else if (strcmp (element_name, "standard_session_servicedirs") == 0)
824 if (!check_no_attributes (parser, "standard_session_servicedirs", attribute_names, attribute_values, error))
827 if (push_element (parser, ELEMENT_STANDARD_SESSION_SERVICEDIRS) == NULL)
833 if (!_dbus_get_standard_session_servicedirs (&dirs))
839 while ((link = _dbus_list_pop_first_link (&dirs)))
840 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
844 else if (strcmp (element_name, "servicedir") == 0)
846 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
849 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
857 else if (strcmp (element_name, "include") == 0)
860 const char *if_selinux_enabled;
861 const char *ignore_missing;
862 const char *selinux_root_relative;
864 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
870 e->d.include.ignore_missing = FALSE;
871 e->d.include.if_selinux_enabled = FALSE;
872 e->d.include.selinux_root_relative = FALSE;
874 if (!locate_attributes (parser, "include",
878 "ignore_missing", &ignore_missing,
879 "if_selinux_enabled", &if_selinux_enabled,
880 "selinux_root_relative", &selinux_root_relative,
884 if (ignore_missing != NULL)
886 if (strcmp (ignore_missing, "yes") == 0)
887 e->d.include.ignore_missing = TRUE;
888 else if (strcmp (ignore_missing, "no") == 0)
889 e->d.include.ignore_missing = FALSE;
892 dbus_set_error (error, DBUS_ERROR_FAILED,
893 "ignore_missing attribute must have value \"yes\" or \"no\"");
898 if (if_selinux_enabled != NULL)
900 if (strcmp (if_selinux_enabled, "yes") == 0)
901 e->d.include.if_selinux_enabled = TRUE;
902 else if (strcmp (if_selinux_enabled, "no") == 0)
903 e->d.include.if_selinux_enabled = FALSE;
906 dbus_set_error (error, DBUS_ERROR_FAILED,
907 "if_selinux_enabled attribute must have value"
908 " \"yes\" or \"no\"");
913 if (selinux_root_relative != NULL)
915 if (strcmp (selinux_root_relative, "yes") == 0)
916 e->d.include.selinux_root_relative = TRUE;
917 else if (strcmp (selinux_root_relative, "no") == 0)
918 e->d.include.selinux_root_relative = FALSE;
921 dbus_set_error (error, DBUS_ERROR_FAILED,
922 "selinux_root_relative attribute must have value"
923 " \"yes\" or \"no\"");
930 else if (strcmp (element_name, "policy") == 0)
936 const char *at_console;
938 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
944 e->d.policy.type = POLICY_IGNORED;
946 if (!locate_attributes (parser, "policy",
953 "at_console", &at_console,
957 if (((context && user) ||
958 (context && group) ||
959 (context && at_console)) ||
961 (user && at_console)) ||
962 (group && at_console) ||
963 !(context || user || group || at_console))
965 dbus_set_error (error, DBUS_ERROR_FAILED,
966 "<policy> element must have exactly one of (context|user|group|at_console) attributes");
972 if (strcmp (context, "default") == 0)
974 e->d.policy.type = POLICY_DEFAULT;
976 else if (strcmp (context, "mandatory") == 0)
978 e->d.policy.type = POLICY_MANDATORY;
982 dbus_set_error (error, DBUS_ERROR_FAILED,
983 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
988 else if (user != NULL)
991 _dbus_string_init_const (&username, user);
993 if (_dbus_parse_unix_user_from_config (&username,
994 &e->d.policy.gid_uid_or_at_console))
995 e->d.policy.type = POLICY_USER;
997 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
1000 else if (group != NULL)
1002 DBusString group_name;
1003 _dbus_string_init_const (&group_name, group);
1005 if (_dbus_parse_unix_group_from_config (&group_name,
1006 &e->d.policy.gid_uid_or_at_console))
1007 e->d.policy.type = POLICY_GROUP;
1009 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
1012 else if (at_console != NULL)
1015 t = (strcmp (at_console, "true") == 0);
1016 if (t || strcmp (at_console, "false") == 0)
1018 e->d.policy.gid_uid_or_at_console = t;
1019 e->d.policy.type = POLICY_CONSOLE;
1023 dbus_set_error (error, DBUS_ERROR_FAILED,
1024 "Unknown value \"%s\" for at_console in message bus configuration file",
1032 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
1037 else if (strcmp (element_name, "limit") == 0)
1042 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
1044 BUS_SET_OOM (error);
1048 if (!locate_attributes (parser, "limit",
1058 dbus_set_error (error, DBUS_ERROR_FAILED,
1059 "<limit> element must have a \"name\" attribute");
1063 e->d.limit.name = _dbus_strdup (name);
1064 if (e->d.limit.name == NULL)
1066 BUS_SET_OOM (error);
1072 else if (strcmp (element_name, "selinux") == 0)
1074 if (!check_no_attributes (parser, "selinux", attribute_names, attribute_values, error))
1077 if (push_element (parser, ELEMENT_SELINUX) == NULL)
1079 BUS_SET_OOM (error);
1087 dbus_set_error (error, DBUS_ERROR_FAILED,
1088 "Element <%s> not allowed inside <%s> in configuration file",
1089 element_name, "busconfig");
1095 append_rule_from_element (BusConfigParser *parser,
1096 const char *element_name,
1097 const char **attribute_names,
1098 const char **attribute_values,
1102 const char *send_interface;
1103 const char *send_member;
1104 const char *send_error;
1105 const char *send_destination;
1106 const char *send_path;
1107 const char *send_type;
1108 const char *receive_interface;
1109 const char *receive_member;
1110 const char *receive_error;
1111 const char *receive_sender;
1112 const char *receive_path;
1113 const char *receive_type;
1114 const char *eavesdrop;
1115 const char *send_requested_reply;
1116 const char *receive_requested_reply;
1121 BusPolicyRule *rule;
1123 if (!locate_attributes (parser, element_name,
1127 "send_interface", &send_interface,
1128 "send_member", &send_member,
1129 "send_error", &send_error,
1130 "send_destination", &send_destination,
1131 "send_path", &send_path,
1132 "send_type", &send_type,
1133 "receive_interface", &receive_interface,
1134 "receive_member", &receive_member,
1135 "receive_error", &receive_error,
1136 "receive_sender", &receive_sender,
1137 "receive_path", &receive_path,
1138 "receive_type", &receive_type,
1139 "eavesdrop", &eavesdrop,
1140 "send_requested_reply", &send_requested_reply,
1141 "receive_requested_reply", &receive_requested_reply,
1148 if (!(send_interface || send_member || send_error || send_destination ||
1149 send_type || send_path ||
1150 receive_interface || receive_member || receive_error || receive_sender ||
1151 receive_type || receive_path || eavesdrop ||
1152 send_requested_reply || receive_requested_reply ||
1153 own || user || group))
1155 dbus_set_error (error, DBUS_ERROR_FAILED,
1156 "Element <%s> must have one or more attributes",
1161 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
1162 (receive_member && (receive_interface == NULL && receive_path == NULL)))
1164 dbus_set_error (error, DBUS_ERROR_FAILED,
1165 "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.",
1170 /* Allowed combinations of elements are:
1172 * base, must be all send or all receive:
1175 * interface + member
1178 * base send_ can combine with send_destination, send_path, send_type, send_requested_reply
1179 * base receive_ with receive_sender, receive_path, receive_type, receive_requested_reply, eavesdrop
1181 * user, group, own must occur alone
1183 * Pretty sure the below stuff is broken, FIXME think about it more.
1186 if (((send_interface && send_error) ||
1187 (send_interface && receive_interface) ||
1188 (send_interface && receive_member) ||
1189 (send_interface && receive_error) ||
1190 (send_interface && receive_sender) ||
1191 (send_interface && receive_requested_reply) ||
1192 (send_interface && own) ||
1193 (send_interface && user) ||
1194 (send_interface && group)) ||
1196 ((send_member && send_error) ||
1197 (send_member && receive_interface) ||
1198 (send_member && receive_member) ||
1199 (send_member && receive_error) ||
1200 (send_member && receive_sender) ||
1201 (send_member && receive_requested_reply) ||
1202 (send_member && own) ||
1203 (send_member && user) ||
1204 (send_member && group)) ||
1206 ((send_error && receive_interface) ||
1207 (send_error && receive_member) ||
1208 (send_error && receive_error) ||
1209 (send_error && receive_sender) ||
1210 (send_error && receive_requested_reply) ||
1211 (send_error && own) ||
1212 (send_error && user) ||
1213 (send_error && group)) ||
1215 ((send_destination && receive_interface) ||
1216 (send_destination && receive_member) ||
1217 (send_destination && receive_error) ||
1218 (send_destination && receive_sender) ||
1219 (send_destination && receive_requested_reply) ||
1220 (send_destination && own) ||
1221 (send_destination && user) ||
1222 (send_destination && group)) ||
1224 ((send_type && receive_interface) ||
1225 (send_type && receive_member) ||
1226 (send_type && receive_error) ||
1227 (send_type && receive_sender) ||
1228 (send_type && receive_requested_reply) ||
1229 (send_type && own) ||
1230 (send_type && user) ||
1231 (send_type && group)) ||
1233 ((send_path && receive_interface) ||
1234 (send_path && receive_member) ||
1235 (send_path && receive_error) ||
1236 (send_path && receive_sender) ||
1237 (send_path && receive_requested_reply) ||
1238 (send_path && own) ||
1239 (send_path && user) ||
1240 (send_path && group)) ||
1242 ((send_requested_reply && receive_interface) ||
1243 (send_requested_reply && receive_member) ||
1244 (send_requested_reply && receive_error) ||
1245 (send_requested_reply && receive_sender) ||
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\"",
1323 !(strcmp (eavesdrop, "true") == 0 ||
1324 strcmp (eavesdrop, "false") == 0))
1326 dbus_set_error (error, DBUS_ERROR_FAILED,
1327 "Bad value \"%s\" for %s attribute, must be true or false",
1328 "eavesdrop", eavesdrop);
1332 if (send_requested_reply &&
1333 !(strcmp (send_requested_reply, "true") == 0 ||
1334 strcmp (send_requested_reply, "false") == 0))
1336 dbus_set_error (error, DBUS_ERROR_FAILED,
1337 "Bad value \"%s\" for %s attribute, must be true or false",
1338 "send_requested_reply", send_requested_reply);
1342 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1347 rule->d.send.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1349 if (send_requested_reply)
1350 rule->d.send.requested_reply = (strcmp (send_requested_reply, "true") == 0);
1352 rule->d.send.message_type = message_type;
1353 rule->d.send.path = _dbus_strdup (send_path);
1354 rule->d.send.interface = _dbus_strdup (send_interface);
1355 rule->d.send.member = _dbus_strdup (send_member);
1356 rule->d.send.error = _dbus_strdup (send_error);
1357 rule->d.send.destination = _dbus_strdup (send_destination);
1358 if (send_path && rule->d.send.path == NULL)
1360 if (send_interface && rule->d.send.interface == NULL)
1362 if (send_member && rule->d.send.member == NULL)
1364 if (send_error && rule->d.send.error == NULL)
1366 if (send_destination && rule->d.send.destination == NULL)
1369 else if (receive_interface || receive_member || receive_error || receive_sender ||
1370 receive_path || receive_type || eavesdrop || receive_requested_reply)
1374 if (IS_WILDCARD (receive_interface))
1375 receive_interface = NULL;
1376 if (IS_WILDCARD (receive_member))
1377 receive_member = NULL;
1378 if (IS_WILDCARD (receive_error))
1379 receive_error = NULL;
1380 if (IS_WILDCARD (receive_sender))
1381 receive_sender = NULL;
1382 if (IS_WILDCARD (receive_path))
1383 receive_path = NULL;
1384 if (IS_WILDCARD (receive_type))
1385 receive_type = NULL;
1387 message_type = DBUS_MESSAGE_TYPE_INVALID;
1388 if (receive_type != NULL)
1390 message_type = dbus_message_type_from_string (receive_type);
1391 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1393 dbus_set_error (error, DBUS_ERROR_FAILED,
1394 "Bad message type \"%s\"",
1402 !(strcmp (eavesdrop, "true") == 0 ||
1403 strcmp (eavesdrop, "false") == 0))
1405 dbus_set_error (error, DBUS_ERROR_FAILED,
1406 "Bad value \"%s\" for %s attribute, must be true or false",
1407 "eavesdrop", eavesdrop);
1411 if (receive_requested_reply &&
1412 !(strcmp (receive_requested_reply, "true") == 0 ||
1413 strcmp (receive_requested_reply, "false") == 0))
1415 dbus_set_error (error, DBUS_ERROR_FAILED,
1416 "Bad value \"%s\" for %s attribute, must be true or false",
1417 "receive_requested_reply", receive_requested_reply);
1421 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1426 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1428 if (receive_requested_reply)
1429 rule->d.receive.requested_reply = (strcmp (receive_requested_reply, "true") == 0);
1431 rule->d.receive.message_type = message_type;
1432 rule->d.receive.path = _dbus_strdup (receive_path);
1433 rule->d.receive.interface = _dbus_strdup (receive_interface);
1434 rule->d.receive.member = _dbus_strdup (receive_member);
1435 rule->d.receive.error = _dbus_strdup (receive_error);
1436 rule->d.receive.origin = _dbus_strdup (receive_sender);
1438 if (receive_path && rule->d.receive.path == NULL)
1440 if (receive_interface && rule->d.receive.interface == NULL)
1442 if (receive_member && rule->d.receive.member == NULL)
1444 if (receive_error && rule->d.receive.error == NULL)
1446 if (receive_sender && rule->d.receive.origin == NULL)
1451 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1455 if (IS_WILDCARD (own))
1458 rule->d.own.service_name = _dbus_strdup (own);
1459 if (own && rule->d.own.service_name == NULL)
1464 if (IS_WILDCARD (user))
1466 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1470 rule->d.user.uid = DBUS_UID_UNSET;
1474 DBusString username;
1477 _dbus_string_init_const (&username, user);
1479 if (_dbus_parse_unix_user_from_config (&username, &uid))
1481 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1485 rule->d.user.uid = uid;
1489 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1490 user, element_name);
1496 if (IS_WILDCARD (group))
1498 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1502 rule->d.group.gid = DBUS_GID_UNSET;
1506 DBusString groupname;
1509 _dbus_string_init_const (&groupname, group);
1511 if (_dbus_parse_unix_group_from_config (&groupname, &gid))
1513 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1517 rule->d.group.gid = gid;
1521 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1522 group, element_name);
1527 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1533 pe = peek_element (parser);
1534 _dbus_assert (pe != NULL);
1535 _dbus_assert (pe->type == ELEMENT_POLICY);
1537 switch (pe->d.policy.type)
1539 case POLICY_IGNORED:
1540 /* drop the rule on the floor */
1543 case POLICY_DEFAULT:
1544 if (!bus_policy_append_default_rule (parser->policy, rule))
1547 case POLICY_MANDATORY:
1548 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1552 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1554 dbus_set_error (error, DBUS_ERROR_FAILED,
1555 "<%s> rule cannot be per-user because it has bus-global semantics",
1560 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1565 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1567 dbus_set_error (error, DBUS_ERROR_FAILED,
1568 "<%s> rule cannot be per-group because it has bus-global semantics",
1573 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1579 case POLICY_CONSOLE:
1580 if (!bus_policy_append_console_rule (parser->policy, pe->d.policy.gid_uid_or_at_console,
1586 bus_policy_rule_unref (rule);
1593 BUS_SET_OOM (error);
1596 bus_policy_rule_unref (rule);
1601 start_policy_child (BusConfigParser *parser,
1602 const char *element_name,
1603 const char **attribute_names,
1604 const char **attribute_values,
1607 if (strcmp (element_name, "allow") == 0)
1609 if (!append_rule_from_element (parser, element_name,
1610 attribute_names, attribute_values,
1614 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1616 BUS_SET_OOM (error);
1622 else if (strcmp (element_name, "deny") == 0)
1624 if (!append_rule_from_element (parser, element_name,
1625 attribute_names, attribute_values,
1629 if (push_element (parser, ELEMENT_DENY) == NULL)
1631 BUS_SET_OOM (error);
1639 dbus_set_error (error, DBUS_ERROR_FAILED,
1640 "Element <%s> not allowed inside <%s> in configuration file",
1641 element_name, "policy");
1647 start_selinux_child (BusConfigParser *parser,
1648 const char *element_name,
1649 const char **attribute_names,
1650 const char **attribute_values,
1657 context_copy = NULL;
1659 if (strcmp (element_name, "associate") == 0)
1662 const char *context;
1664 if (!locate_attributes (parser, "associate",
1669 "context", &context,
1673 if (push_element (parser, ELEMENT_ASSOCIATE) == NULL)
1675 BUS_SET_OOM (error);
1679 if (own == NULL || context == NULL)
1681 dbus_set_error (error, DBUS_ERROR_FAILED,
1682 "Element <associate> must have attributes own=\"<servicename>\" and context=\"<selinux context>\"");
1686 own_copy = _dbus_strdup (own);
1687 if (own_copy == NULL)
1689 context_copy = _dbus_strdup (context);
1690 if (context_copy == NULL)
1693 if (!_dbus_hash_table_insert_string (parser->service_context_table,
1694 own_copy, context_copy))
1701 dbus_set_error (error, DBUS_ERROR_FAILED,
1702 "Element <%s> not allowed inside <%s> in configuration file",
1703 element_name, "selinux");
1709 dbus_free (own_copy);
1712 dbus_free (context_copy);
1714 BUS_SET_OOM (error);
1719 bus_config_parser_start_element (BusConfigParser *parser,
1720 const char *element_name,
1721 const char **attribute_names,
1722 const char **attribute_values,
1727 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1729 /* printf ("START: %s\n", element_name); */
1731 t = top_element_type (parser);
1733 if (t == ELEMENT_NONE)
1735 if (strcmp (element_name, "busconfig") == 0)
1737 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1740 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1742 BUS_SET_OOM (error);
1750 dbus_set_error (error, DBUS_ERROR_FAILED,
1751 "Unknown element <%s> at root of configuration file",
1756 else if (t == ELEMENT_BUSCONFIG)
1758 return start_busconfig_child (parser, element_name,
1759 attribute_names, attribute_values,
1762 else if (t == ELEMENT_POLICY)
1764 return start_policy_child (parser, element_name,
1765 attribute_names, attribute_values,
1768 else if (t == ELEMENT_SELINUX)
1770 return start_selinux_child (parser, element_name,
1771 attribute_names, attribute_values,
1776 dbus_set_error (error, DBUS_ERROR_FAILED,
1777 "Element <%s> is not allowed in this context",
1784 set_limit (BusConfigParser *parser,
1789 dbus_bool_t must_be_positive;
1790 dbus_bool_t must_be_int;
1792 must_be_int = FALSE;
1793 must_be_positive = FALSE;
1795 if (strcmp (name, "max_incoming_bytes") == 0)
1797 must_be_positive = TRUE;
1798 parser->limits.max_incoming_bytes = value;
1800 else if (strcmp (name, "max_outgoing_bytes") == 0)
1802 must_be_positive = TRUE;
1803 parser->limits.max_outgoing_bytes = value;
1805 else if (strcmp (name, "max_message_size") == 0)
1807 must_be_positive = TRUE;
1808 parser->limits.max_message_size = value;
1810 else if (strcmp (name, "service_start_timeout") == 0)
1812 must_be_positive = TRUE;
1814 parser->limits.activation_timeout = value;
1816 else if (strcmp (name, "auth_timeout") == 0)
1818 must_be_positive = TRUE;
1820 parser->limits.auth_timeout = value;
1822 else if (strcmp (name, "reply_timeout") == 0)
1824 must_be_positive = TRUE;
1826 parser->limits.reply_timeout = value;
1828 else if (strcmp (name, "max_completed_connections") == 0)
1830 must_be_positive = TRUE;
1832 parser->limits.max_completed_connections = value;
1834 else if (strcmp (name, "max_incomplete_connections") == 0)
1836 must_be_positive = TRUE;
1838 parser->limits.max_incomplete_connections = value;
1840 else if (strcmp (name, "max_connections_per_user") == 0)
1842 must_be_positive = TRUE;
1844 parser->limits.max_connections_per_user = value;
1846 else if (strcmp (name, "max_pending_service_starts") == 0)
1848 must_be_positive = TRUE;
1850 parser->limits.max_pending_activations = value;
1852 else if (strcmp (name, "max_names_per_connection") == 0)
1854 must_be_positive = TRUE;
1856 parser->limits.max_services_per_connection = value;
1858 else if (strcmp (name, "max_match_rules_per_connection") == 0)
1860 must_be_positive = TRUE;
1862 parser->limits.max_match_rules_per_connection = value;
1864 else if (strcmp (name, "max_replies_per_connection") == 0)
1866 must_be_positive = TRUE;
1868 parser->limits.max_replies_per_connection = value;
1872 dbus_set_error (error, DBUS_ERROR_FAILED,
1873 "There is no limit called \"%s\"\n",
1878 if (must_be_positive && value < 0)
1880 dbus_set_error (error, DBUS_ERROR_FAILED,
1881 "<limit name=\"%s\"> must be a positive number\n",
1887 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1889 dbus_set_error (error, DBUS_ERROR_FAILED,
1890 "<limit name=\"%s\"> value is too large\n",
1899 bus_config_parser_end_element (BusConfigParser *parser,
1900 const char *element_name,
1907 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1909 /* printf ("END: %s\n", element_name); */
1911 t = top_element_type (parser);
1913 if (t == ELEMENT_NONE)
1915 /* should probably be an assertion failure but
1916 * being paranoid about XML parsers
1918 dbus_set_error (error, DBUS_ERROR_FAILED,
1919 "XML parser ended element with no element on the stack");
1923 n = element_type_to_name (t);
1924 _dbus_assert (n != NULL);
1925 if (strcmp (n, element_name) != 0)
1927 /* should probably be an assertion failure but
1928 * being paranoid about XML parsers
1930 dbus_set_error (error, DBUS_ERROR_FAILED,
1931 "XML element <%s> ended but topmost element on the stack was <%s>",
1936 e = peek_element (parser);
1937 _dbus_assert (e != NULL);
1942 _dbus_assert_not_reached ("element in stack has no type");
1945 case ELEMENT_INCLUDE:
1948 case ELEMENT_LISTEN:
1949 case ELEMENT_PIDFILE:
1951 case ELEMENT_SERVICEDIR:
1952 case ELEMENT_INCLUDEDIR:
1954 if (!e->had_content)
1956 dbus_set_error (error, DBUS_ERROR_FAILED,
1957 "XML element <%s> was expected to have content inside it",
1958 element_type_to_name (e->type));
1962 if (e->type == ELEMENT_LIMIT)
1964 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1970 case ELEMENT_BUSCONFIG:
1971 case ELEMENT_POLICY:
1975 case ELEMENT_SELINUX:
1976 case ELEMENT_ASSOCIATE:
1977 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
1981 pop_element (parser);
1987 all_whitespace (const DBusString *str)
1991 _dbus_string_skip_white (str, 0, &i);
1993 return i == _dbus_string_get_length (str);
1997 make_full_path (const DBusString *basedir,
1998 const DBusString *filename,
1999 DBusString *full_path)
2001 if (_dbus_path_is_absolute (filename))
2003 return _dbus_string_copy (filename, 0, full_path, 0);
2007 if (!_dbus_string_copy (basedir, 0, full_path, 0))
2010 if (!_dbus_concat_dir_and_file (full_path, filename))
2018 include_file (BusConfigParser *parser,
2019 const DBusString *filename,
2020 dbus_bool_t ignore_missing,
2023 /* FIXME good test case for this would load each config file in the
2024 * test suite both alone, and as an include, and check
2025 * that the result is the same
2027 BusConfigParser *included;
2028 const char *filename_str;
2029 DBusError tmp_error;
2031 dbus_error_init (&tmp_error);
2033 filename_str = _dbus_string_get_const_data (filename);
2035 /* Check to make sure this file hasn't already been included. */
2036 if (seen_include (parser, filename))
2038 dbus_set_error (error, DBUS_ERROR_FAILED,
2039 "Circular inclusion of file '%s'",
2044 if (! _dbus_list_append (&parser->included_files, (void *) filename_str))
2046 BUS_SET_OOM (error);
2050 /* Since parser is passed in as the parent, included
2051 inherits parser's limits. */
2052 included = bus_config_load (filename, FALSE, parser, &tmp_error);
2054 _dbus_list_pop_last (&parser->included_files);
2056 if (included == NULL)
2058 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
2060 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
2063 dbus_error_free (&tmp_error);
2068 dbus_move_error (&tmp_error, error);
2074 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
2076 if (!merge_included (parser, included, error))
2078 bus_config_parser_unref (included);
2082 /* Copy included's limits back to parser. */
2083 parser->limits = included->limits;
2085 bus_config_parser_unref (included);
2091 include_dir (BusConfigParser *parser,
2092 const DBusString *dirname,
2095 DBusString filename;
2097 DBusError tmp_error;
2101 if (!_dbus_string_init (&filename))
2103 BUS_SET_OOM (error);
2109 dir = _dbus_directory_open (dirname, error);
2114 dbus_error_init (&tmp_error);
2115 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
2117 DBusString full_path;
2119 if (!_dbus_string_init (&full_path))
2121 BUS_SET_OOM (error);
2125 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
2127 BUS_SET_OOM (error);
2128 _dbus_string_free (&full_path);
2132 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2134 BUS_SET_OOM (error);
2135 _dbus_string_free (&full_path);
2139 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
2141 if (!include_file (parser, &full_path, TRUE, error))
2143 _dbus_string_free (&full_path);
2148 _dbus_string_free (&full_path);
2151 if (dbus_error_is_set (&tmp_error))
2153 dbus_move_error (&tmp_error, error);
2158 if (!_dbus_string_copy_data (dirname, &s))
2160 BUS_SET_OOM (error);
2164 if (!_dbus_list_append (&parser->conf_dirs, s))
2167 BUS_SET_OOM (error);
2174 _dbus_string_free (&filename);
2177 _dbus_directory_close (dir);
2183 bus_config_parser_content (BusConfigParser *parser,
2184 const DBusString *content,
2189 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2195 _dbus_string_get_const_data (content, &c_str);
2197 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
2201 e = peek_element (parser);
2204 dbus_set_error (error, DBUS_ERROR_FAILED,
2205 "Text content outside of any XML element in configuration file");
2208 else if (e->had_content)
2210 _dbus_assert_not_reached ("Element had multiple content blocks");
2214 switch (top_element_type (parser))
2217 _dbus_assert_not_reached ("element at top of stack has no type");
2220 case ELEMENT_BUSCONFIG:
2221 case ELEMENT_POLICY:
2225 case ELEMENT_STANDARD_SESSION_SERVICEDIRS:
2226 case ELEMENT_SELINUX:
2227 case ELEMENT_ASSOCIATE:
2228 if (all_whitespace (content))
2232 dbus_set_error (error, DBUS_ERROR_FAILED,
2233 "No text content expected inside XML element %s in configuration file",
2234 element_type_to_name (top_element_type (parser)));
2238 case ELEMENT_PIDFILE:
2242 e->had_content = TRUE;
2244 if (!_dbus_string_copy_data (content, &s))
2247 dbus_free (parser->pidfile);
2248 parser->pidfile = s;
2252 case ELEMENT_INCLUDE:
2254 DBusString full_path, selinux_policy_root;
2256 e->had_content = TRUE;
2258 if (e->d.include.if_selinux_enabled
2259 && !bus_selinux_enabled ())
2262 if (!_dbus_string_init (&full_path))
2265 if (e->d.include.selinux_root_relative)
2267 if (!bus_selinux_get_policy_root ())
2269 dbus_set_error (error, DBUS_ERROR_FAILED,
2270 "Could not determine SELinux policy root for relative inclusion");
2271 _dbus_string_free (&full_path);
2274 _dbus_string_init_const (&selinux_policy_root,
2275 bus_selinux_get_policy_root ());
2276 if (!make_full_path (&selinux_policy_root, content, &full_path))
2278 _dbus_string_free (&full_path);
2282 else if (!make_full_path (&parser->basedir, content, &full_path))
2284 _dbus_string_free (&full_path);
2288 if (!include_file (parser, &full_path,
2289 e->d.include.ignore_missing, error))
2291 _dbus_string_free (&full_path);
2295 _dbus_string_free (&full_path);
2299 case ELEMENT_INCLUDEDIR:
2301 DBusString full_path;
2303 e->had_content = TRUE;
2305 if (!_dbus_string_init (&full_path))
2308 if (!make_full_path (&parser->basedir, content, &full_path))
2310 _dbus_string_free (&full_path);
2314 if (!include_dir (parser, &full_path, error))
2316 _dbus_string_free (&full_path);
2320 _dbus_string_free (&full_path);
2328 e->had_content = TRUE;
2330 if (!_dbus_string_copy_data (content, &s))
2333 dbus_free (parser->user);
2342 e->had_content = TRUE;
2344 if (!_dbus_string_copy_data (content, &s))
2347 dbus_free (parser->bus_type);
2348 parser->bus_type = s;
2352 case ELEMENT_LISTEN:
2356 e->had_content = TRUE;
2358 if (!_dbus_string_copy_data (content, &s))
2361 if (!_dbus_list_append (&parser->listen_on,
2374 e->had_content = TRUE;
2376 if (!_dbus_string_copy_data (content, &s))
2379 if (!_dbus_list_append (&parser->mechanisms,
2388 case ELEMENT_SERVICEDIR:
2391 DBusString full_path;
2393 e->had_content = TRUE;
2395 if (!_dbus_string_init (&full_path))
2398 if (!make_full_path (&parser->basedir, content, &full_path))
2400 _dbus_string_free (&full_path);
2404 if (!_dbus_string_copy_data (&full_path, &s))
2406 _dbus_string_free (&full_path);
2410 if (!service_dirs_append_unique_or_free (&parser->service_dirs, s))
2412 _dbus_string_free (&full_path);
2417 _dbus_string_free (&full_path);
2425 e->had_content = TRUE;
2428 if (!_dbus_string_parse_int (content, 0, &val, NULL))
2430 dbus_set_error (error, DBUS_ERROR_FAILED,
2431 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
2436 e->d.limit.value = val;
2438 _dbus_verbose ("Loaded value %ld for limit %s\n",
2445 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2449 BUS_SET_OOM (error);
2454 bus_config_parser_finished (BusConfigParser *parser,
2457 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2459 if (parser->stack != NULL)
2461 dbus_set_error (error, DBUS_ERROR_FAILED,
2462 "Element <%s> was not closed in configuration file",
2463 element_type_to_name (top_element_type (parser)));
2468 if (parser->is_toplevel && parser->listen_on == NULL)
2470 dbus_set_error (error, DBUS_ERROR_FAILED,
2471 "Configuration file needs one or more <listen> elements giving addresses");
2479 bus_config_parser_get_user (BusConfigParser *parser)
2481 return parser->user;
2485 bus_config_parser_get_type (BusConfigParser *parser)
2487 return parser->bus_type;
2491 bus_config_parser_get_addresses (BusConfigParser *parser)
2493 return &parser->listen_on;
2497 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2499 return &parser->mechanisms;
2503 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2505 return &parser->service_dirs;
2509 bus_config_parser_get_conf_dirs (BusConfigParser *parser)
2511 return &parser->conf_dirs;
2515 bus_config_parser_get_fork (BusConfigParser *parser)
2517 return parser->fork;
2521 bus_config_parser_get_pidfile (BusConfigParser *parser)
2523 return parser->pidfile;
2527 bus_config_parser_steal_policy (BusConfigParser *parser)
2531 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2533 policy = parser->policy;
2535 parser->policy = NULL;
2540 /* Overwrite any limits that were set in the configuration file */
2542 bus_config_parser_get_limits (BusConfigParser *parser,
2545 *limits = parser->limits;
2549 bus_config_parser_steal_service_context_table (BusConfigParser *parser)
2551 DBusHashTable *table;
2553 _dbus_assert (parser->service_context_table != NULL); /* can only steal once */
2555 table = parser->service_context_table;
2557 parser->service_context_table = NULL;
2562 #ifdef DBUS_BUILD_TESTS
2573 do_load (const DBusString *full_path,
2575 dbus_bool_t oom_possible)
2577 BusConfigParser *parser;
2580 dbus_error_init (&error);
2582 parser = bus_config_load (full_path, TRUE, NULL, &error);
2585 _DBUS_ASSERT_ERROR_IS_SET (&error);
2588 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2590 _dbus_verbose ("Failed to load valid file due to OOM\n");
2591 dbus_error_free (&error);
2594 else if (validity == VALID)
2596 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2599 dbus_error_free (&error);
2604 dbus_error_free (&error);
2610 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2612 bus_config_parser_unref (parser);
2614 if (validity == INVALID)
2616 _dbus_warn ("Accepted invalid file\n");
2626 const DBusString *full_path;
2631 check_loader_oom_func (void *data)
2633 LoaderOomData *d = data;
2635 return do_load (d->full_path, d->validity, TRUE);
2639 process_test_valid_subdir (const DBusString *test_base_dir,
2643 DBusString test_directory;
2644 DBusString filename;
2652 if (!_dbus_string_init (&test_directory))
2653 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2655 _dbus_string_init_const (&filename, subdir);
2657 if (!_dbus_string_copy (test_base_dir, 0,
2658 &test_directory, 0))
2659 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2661 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2662 _dbus_assert_not_reached ("couldn't allocate full path");
2664 _dbus_string_free (&filename);
2665 if (!_dbus_string_init (&filename))
2666 _dbus_assert_not_reached ("didn't allocate filename string\n");
2668 dbus_error_init (&error);
2669 dir = _dbus_directory_open (&test_directory, &error);
2672 _dbus_warn ("Could not open %s: %s\n",
2673 _dbus_string_get_const_data (&test_directory),
2675 dbus_error_free (&error);
2679 if (validity == VALID)
2680 printf ("Testing valid files:\n");
2681 else if (validity == INVALID)
2682 printf ("Testing invalid files:\n");
2684 printf ("Testing unknown files:\n");
2687 while (_dbus_directory_get_next_file (dir, &filename, &error))
2689 DBusString full_path;
2692 if (!_dbus_string_init (&full_path))
2693 _dbus_assert_not_reached ("couldn't init string");
2695 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2696 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2698 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2699 _dbus_assert_not_reached ("couldn't concat file to dir");
2701 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2703 _dbus_verbose ("Skipping non-.conf file %s\n",
2704 _dbus_string_get_const_data (&filename));
2705 _dbus_string_free (&full_path);
2709 printf (" %s\n", _dbus_string_get_const_data (&filename));
2711 _dbus_verbose (" expecting %s\n",
2712 validity == VALID ? "valid" :
2713 (validity == INVALID ? "invalid" :
2714 (validity == UNKNOWN ? "unknown" : "???")));
2716 d.full_path = &full_path;
2717 d.validity = validity;
2719 /* FIXME hackaround for an expat problem, see
2720 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
2721 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
2723 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
2724 if (!check_loader_oom_func (&d))
2725 _dbus_assert_not_reached ("test failed");
2727 _dbus_string_free (&full_path);
2730 if (dbus_error_is_set (&error))
2732 _dbus_warn ("Could not get next file in %s: %s\n",
2733 _dbus_string_get_const_data (&test_directory),
2735 dbus_error_free (&error);
2744 _dbus_directory_close (dir);
2745 _dbus_string_free (&test_directory);
2746 _dbus_string_free (&filename);
2752 bools_equal (dbus_bool_t a,
2759 strings_equal_or_both_null (const char *a,
2762 if (a == NULL || b == NULL)
2765 return !strcmp (a, b);
2769 elements_equal (const Element *a,
2772 if (a->type != b->type)
2775 if (!bools_equal (a->had_content, b->had_content))
2781 case ELEMENT_INCLUDE:
2782 if (!bools_equal (a->d.include.ignore_missing,
2783 b->d.include.ignore_missing))
2787 case ELEMENT_POLICY:
2788 if (a->d.policy.type != b->d.policy.type)
2790 if (a->d.policy.gid_uid_or_at_console != b->d.policy.gid_uid_or_at_console)
2795 if (strcmp (a->d.limit.name, b->d.limit.name))
2797 if (a->d.limit.value != b->d.limit.value)
2811 lists_of_elements_equal (DBusList *a,
2820 while (ia != NULL && ib != NULL)
2822 if (elements_equal (ia->data, ib->data))
2824 ia = _dbus_list_get_next_link (&a, ia);
2825 ib = _dbus_list_get_next_link (&b, ib);
2828 return ia == NULL && ib == NULL;
2832 lists_of_c_strings_equal (DBusList *a,
2841 while (ia != NULL && ib != NULL)
2843 if (strcmp (ia->data, ib->data))
2845 ia = _dbus_list_get_next_link (&a, ia);
2846 ib = _dbus_list_get_next_link (&b, ib);
2849 return ia == NULL && ib == NULL;
2853 limits_equal (const BusLimits *a,
2857 (a->max_incoming_bytes == b->max_incoming_bytes
2858 || a->max_outgoing_bytes == b->max_outgoing_bytes
2859 || a->max_message_size == b->max_message_size
2860 || a->activation_timeout == b->activation_timeout
2861 || a->auth_timeout == b->auth_timeout
2862 || a->max_completed_connections == b->max_completed_connections
2863 || a->max_incomplete_connections == b->max_incomplete_connections
2864 || a->max_connections_per_user == b->max_connections_per_user
2865 || a->max_pending_activations == b->max_pending_activations
2866 || a->max_services_per_connection == b->max_services_per_connection
2867 || a->max_match_rules_per_connection == b->max_match_rules_per_connection
2868 || a->max_replies_per_connection == b->max_replies_per_connection
2869 || a->reply_timeout == b->reply_timeout);
2873 config_parsers_equal (const BusConfigParser *a,
2874 const BusConfigParser *b)
2876 if (!_dbus_string_equal (&a->basedir, &b->basedir))
2879 if (!lists_of_elements_equal (a->stack, b->stack))
2882 if (!strings_equal_or_both_null (a->user, b->user))
2885 if (!lists_of_c_strings_equal (a->listen_on, b->listen_on))
2888 if (!lists_of_c_strings_equal (a->mechanisms, b->mechanisms))
2891 if (!lists_of_c_strings_equal (a->service_dirs, b->service_dirs))
2894 /* FIXME: compare policy */
2896 /* FIXME: compare service selinux ID table */
2898 if (! limits_equal (&a->limits, &b->limits))
2901 if (!strings_equal_or_both_null (a->pidfile, b->pidfile))
2904 if (! bools_equal (a->fork, b->fork))
2907 if (! bools_equal (a->is_toplevel, b->is_toplevel))
2914 all_are_equiv (const DBusString *target_directory)
2916 DBusString filename;
2918 BusConfigParser *first_parser;
2919 BusConfigParser *parser;
2925 first_parser = NULL;
2929 if (!_dbus_string_init (&filename))
2930 _dbus_assert_not_reached ("didn't allocate filename string");
2932 dbus_error_init (&error);
2933 dir = _dbus_directory_open (target_directory, &error);
2936 _dbus_warn ("Could not open %s: %s\n",
2937 _dbus_string_get_const_data (target_directory),
2939 dbus_error_free (&error);
2943 printf ("Comparing equivalent files:\n");
2946 while (_dbus_directory_get_next_file (dir, &filename, &error))
2948 DBusString full_path;
2950 if (!_dbus_string_init (&full_path))
2951 _dbus_assert_not_reached ("couldn't init string");
2953 if (!_dbus_string_copy (target_directory, 0, &full_path, 0))
2954 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2956 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2957 _dbus_assert_not_reached ("couldn't concat file to dir");
2959 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2961 _dbus_verbose ("Skipping non-.conf file %s\n",
2962 _dbus_string_get_const_data (&filename));
2963 _dbus_string_free (&full_path);
2967 printf (" %s\n", _dbus_string_get_const_data (&filename));
2969 parser = bus_config_load (&full_path, TRUE, NULL, &error);
2973 _dbus_warn ("Could not load file %s: %s\n",
2974 _dbus_string_get_const_data (&full_path),
2976 _dbus_string_free (&full_path);
2977 dbus_error_free (&error);
2980 else if (first_parser == NULL)
2982 _dbus_string_free (&full_path);
2983 first_parser = parser;
2987 _dbus_string_free (&full_path);
2988 equal = config_parsers_equal (first_parser, parser);
2989 bus_config_parser_unref (parser);
2998 _dbus_string_free (&filename);
3000 bus_config_parser_unref (first_parser);
3002 _dbus_directory_close (dir);
3009 process_test_equiv_subdir (const DBusString *test_base_dir,
3012 DBusString test_directory;
3013 DBusString filename;
3022 if (!_dbus_string_init (&test_directory))
3023 _dbus_assert_not_reached ("didn't allocate test_directory");
3025 _dbus_string_init_const (&filename, subdir);
3027 if (!_dbus_string_copy (test_base_dir, 0,
3028 &test_directory, 0))
3029 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
3031 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
3032 _dbus_assert_not_reached ("couldn't allocate full path");
3034 _dbus_string_free (&filename);
3035 if (!_dbus_string_init (&filename))
3036 _dbus_assert_not_reached ("didn't allocate filename string");
3038 dbus_error_init (&error);
3039 dir = _dbus_directory_open (&test_directory, &error);
3042 _dbus_warn ("Could not open %s: %s\n",
3043 _dbus_string_get_const_data (&test_directory),
3045 dbus_error_free (&error);
3049 while (_dbus_directory_get_next_file (dir, &filename, &error))
3051 DBusString full_path;
3053 /* Skip CVS's magic directories! */
3054 if (_dbus_string_equal_c_str (&filename, "CVS"))
3057 if (!_dbus_string_init (&full_path))
3058 _dbus_assert_not_reached ("couldn't init string");
3060 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
3061 _dbus_assert_not_reached ("couldn't copy dir to full_path");
3063 if (!_dbus_concat_dir_and_file (&full_path, &filename))
3064 _dbus_assert_not_reached ("couldn't concat file to dir");
3066 equal = all_are_equiv (&full_path);
3067 _dbus_string_free (&full_path);
3076 _dbus_string_free (&test_directory);
3077 _dbus_string_free (&filename);
3079 _dbus_directory_close (dir);
3085 static const char *test_service_dir_matches[] =
3088 "/testusr/testlocal/testshare/dbus-1/services",
3089 "/testusr/testshare/dbus-1/services",
3091 DBUS_DATADIR"/dbus-1/services",
3093 "/testhome/foo/.testlocal/testshare/dbus-1/services",
3099 test_default_session_servicedirs (void)
3104 const char *common_progs;
3107 /* On Unix we don't actually use this variable, but it's easier to handle the
3108 * deallocation if we always allocate it, whether needed or not */
3109 if (!_dbus_string_init (&progs))
3110 _dbus_assert_not_reached ("OOM allocating progs");
3112 common_progs = _dbus_getenv ("CommonProgramFiles");
3116 if (!_dbus_string_append (&progs, common_progs))
3118 _dbus_string_free (&progs);
3122 if (!_dbus_string_append (&progs, "/dbus-1/services"))
3124 _dbus_string_free (&progs);
3127 test_service_dir_matches[1] = _dbus_string_get_const_data(&progs);
3132 printf ("Testing retrieving the default session service directories\n");
3133 if (!_dbus_get_standard_session_servicedirs (&dirs))
3134 _dbus_assert_not_reached ("couldn't get stardard dirs");
3136 /* make sure our defaults end with share/dbus-1/service */
3137 while ((link = _dbus_list_pop_first_link (&dirs)))
3141 printf (" default service dir: %s\n", (char *)link->data);
3142 _dbus_string_init_const (&path, (char *)link->data);
3143 if (!_dbus_string_ends_with_c_str (&path, "dbus-1/services"))
3145 printf ("error with default session service directories\n");
3146 dbus_free (link->data);
3147 _dbus_list_free_link (link);
3148 _dbus_string_free (&progs);
3152 dbus_free (link->data);
3153 _dbus_list_free_link (link);
3157 if (!_dbus_setenv ("XDG_DATA_HOME", "/testhome/foo/.testlocal/testshare"))
3158 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_HOME");
3160 if (!_dbus_setenv ("XDG_DATA_DIRS", ":/testusr/testlocal/testshare: :/testusr/testshare:"))
3161 _dbus_assert_not_reached ("couldn't setenv XDG_DATA_DIRS");
3163 if (!_dbus_get_standard_session_servicedirs (&dirs))
3164 _dbus_assert_not_reached ("couldn't get stardard dirs");
3166 /* make sure we read and parse the env variable correctly */
3168 while ((link = _dbus_list_pop_first_link (&dirs)))
3170 printf (" test service dir: %s\n", (char *)link->data);
3171 if (test_service_dir_matches[i] == NULL)
3173 printf ("more directories parsed than in match set\n");
3174 dbus_free (link->data);
3175 _dbus_list_free_link (link);
3176 _dbus_string_free (&progs);
3180 if (strcmp (test_service_dir_matches[i],
3181 (char *)link->data) != 0)
3183 printf ("%s directory does not match %s in the match set\n",
3185 test_service_dir_matches[i]);
3186 dbus_free (link->data);
3187 _dbus_list_free_link (link);
3188 _dbus_string_free (&progs);
3194 dbus_free (link->data);
3195 _dbus_list_free_link (link);
3198 if (test_service_dir_matches[i] != NULL)
3200 printf ("extra data %s in the match set was not matched\n",
3201 test_service_dir_matches[i]);
3203 _dbus_string_free (&progs);
3207 _dbus_string_free (&progs);
3212 bus_config_parser_test (const DBusString *test_data_dir)
3214 if (test_data_dir == NULL ||
3215 _dbus_string_get_length (test_data_dir) == 0)
3217 printf ("No test data\n");
3221 if (!test_default_session_servicedirs())
3224 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
3227 if (!process_test_valid_subdir (test_data_dir, "invalid-config-files", INVALID))
3230 if (!process_test_equiv_subdir (test_data_dir, "equiv-config-files"))
3236 #endif /* DBUS_BUILD_TESTS */