1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* config-parser.c XML-library-agnostic configuration file parser
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
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"
27 #include <dbus/dbus-list.h>
28 #include <dbus/dbus-internals.h>
52 /* we ignore policies for unknown groups/users */
66 unsigned int had_content : 1;
72 unsigned int ignore_missing : 1;
78 unsigned long gid_or_uid;
92 * Parser for bus configuration file.
94 struct BusConfigParser
96 int refcount; /**< Reference count */
98 DBusString basedir; /**< Directory we resolve paths relative to */
100 DBusList *stack; /**< stack of Element */
102 char *user; /**< user to run as */
104 char *bus_type; /**< Message bus type */
106 DBusList *listen_on; /**< List of addresses to listen to */
108 DBusList *mechanisms; /**< Auth mechanisms */
110 DBusList *service_dirs; /**< Directories to look for services in */
112 BusPolicy *policy; /**< Security policy */
114 BusLimits limits; /**< Limits */
116 char *pidfile; /**< PID file */
118 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
120 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
124 element_type_to_name (ElementType type)
130 case ELEMENT_BUSCONFIG:
132 case ELEMENT_INCLUDE:
150 case ELEMENT_PIDFILE:
152 case ELEMENT_SERVICEDIR:
154 case ELEMENT_INCLUDEDIR:
160 _dbus_assert_not_reached ("bad element type");
166 push_element (BusConfigParser *parser,
171 _dbus_assert (type != ELEMENT_NONE);
173 e = dbus_new0 (Element, 1);
177 if (!_dbus_list_append (&parser->stack, e))
189 element_free (Element *e)
191 if (e->type == ELEMENT_LIMIT)
192 dbus_free (e->d.limit.name);
198 pop_element (BusConfigParser *parser)
202 e = _dbus_list_pop_last (&parser->stack);
208 peek_element (BusConfigParser *parser)
212 e = _dbus_list_get_last (&parser->stack);
218 top_element_type (BusConfigParser *parser)
222 e = _dbus_list_get_last (&parser->stack);
231 merge_included (BusConfigParser *parser,
232 BusConfigParser *included,
237 if (!bus_policy_merge (parser->policy,
244 if (included->user != NULL)
246 dbus_free (parser->user);
247 parser->user = included->user;
248 included->user = NULL;
251 if (included->bus_type != NULL)
253 dbus_free (parser->bus_type);
254 parser->bus_type = included->bus_type;
255 included->bus_type = NULL;
261 if (included->pidfile != NULL)
263 dbus_free (parser->pidfile);
264 parser->pidfile = included->pidfile;
265 included->pidfile = NULL;
268 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
269 _dbus_list_append_link (&parser->listen_on, link);
271 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
272 _dbus_list_append_link (&parser->mechanisms, link);
274 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
275 _dbus_list_append_link (&parser->service_dirs, link);
281 bus_config_parser_new (const DBusString *basedir,
282 dbus_bool_t is_toplevel)
284 BusConfigParser *parser;
286 parser = dbus_new0 (BusConfigParser, 1);
290 parser->is_toplevel = !!is_toplevel;
292 if (!_dbus_string_init (&parser->basedir))
298 if (((parser->policy = bus_policy_new ()) == NULL) ||
299 !_dbus_string_copy (basedir, 0, &parser->basedir, 0))
302 bus_policy_unref (parser->policy);
304 _dbus_string_free (&parser->basedir);
309 /* Make up some numbers! woot! */
310 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
311 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
312 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
314 /* Making this long means the user has to wait longer for an error
315 * message if something screws up, but making it too short means
316 * they might see a false failure.
318 parser->limits.activation_timeout = 25000; /* 25 seconds */
320 /* Making this long risks making a DOS attack easier, but too short
321 * and legitimate auth will fail. If interactive auth (ask user for
322 * password) is allowed, then potentially it has to be quite long.
324 parser->limits.auth_timeout = 30000; /* 30 seconds */
326 parser->limits.max_incomplete_connections = 32;
327 parser->limits.max_connections_per_user = 128;
329 /* Note that max_completed_connections / max_connections_per_user
330 * is the number of users that would have to work together to
331 * DOS all the other users.
333 parser->limits.max_completed_connections = 1024;
335 parser->limits.max_pending_activations = 256;
336 parser->limits.max_services_per_connection = 256;
338 parser->limits.max_match_rules_per_connection = 128;
340 parser->limits.reply_timeout = 5 * 60 * 1000; /* 5 minutes */
341 parser->limits.max_replies_per_connection = 32;
343 parser->refcount = 1;
349 bus_config_parser_ref (BusConfigParser *parser)
351 _dbus_assert (parser->refcount > 0);
353 parser->refcount += 1;
357 bus_config_parser_unref (BusConfigParser *parser)
359 _dbus_assert (parser->refcount > 0);
361 parser->refcount -= 1;
363 if (parser->refcount == 0)
365 while (parser->stack != NULL)
366 pop_element (parser);
368 dbus_free (parser->user);
369 dbus_free (parser->bus_type);
370 dbus_free (parser->pidfile);
372 _dbus_list_foreach (&parser->listen_on,
373 (DBusForeachFunction) dbus_free,
376 _dbus_list_clear (&parser->listen_on);
378 _dbus_list_foreach (&parser->service_dirs,
379 (DBusForeachFunction) dbus_free,
382 _dbus_list_clear (&parser->service_dirs);
384 _dbus_list_foreach (&parser->mechanisms,
385 (DBusForeachFunction) dbus_free,
388 _dbus_list_clear (&parser->mechanisms);
390 _dbus_string_free (&parser->basedir);
393 bus_policy_unref (parser->policy);
400 bus_config_parser_check_doctype (BusConfigParser *parser,
404 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
406 if (strcmp (doctype, "busconfig") != 0)
408 dbus_set_error (error,
410 "Configuration file has the wrong document type %s",
425 locate_attributes (BusConfigParser *parser,
426 const char *element_name,
427 const char **attribute_names,
428 const char **attribute_values,
430 const char *first_attribute_name,
431 const char **first_attribute_retloc,
439 LocateAttr attrs[MAX_ATTRS];
443 _dbus_assert (first_attribute_name != NULL);
444 _dbus_assert (first_attribute_retloc != NULL);
449 attrs[0].name = first_attribute_name;
450 attrs[0].retloc = first_attribute_retloc;
451 *first_attribute_retloc = NULL;
453 va_start (args, first_attribute_retloc);
455 name = va_arg (args, const char*);
456 retloc = va_arg (args, const char**);
460 _dbus_assert (retloc != NULL);
461 _dbus_assert (n_attrs < MAX_ATTRS);
463 attrs[n_attrs].name = name;
464 attrs[n_attrs].retloc = retloc;
468 name = va_arg (args, const char*);
469 retloc = va_arg (args, const char**);
478 while (attribute_names[i])
487 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
489 retloc = attrs[j].retloc;
493 dbus_set_error (error, DBUS_ERROR_FAILED,
494 "Attribute \"%s\" repeated twice on the same <%s> element",
495 attrs[j].name, element_name);
500 *retloc = attribute_values[i];
509 dbus_set_error (error, DBUS_ERROR_FAILED,
510 "Attribute \"%s\" is invalid on <%s> element in this context",
511 attribute_names[i], element_name);
524 check_no_attributes (BusConfigParser *parser,
525 const char *element_name,
526 const char **attribute_names,
527 const char **attribute_values,
530 if (attribute_names[0] != NULL)
532 dbus_set_error (error, DBUS_ERROR_FAILED,
533 "Attribute \"%s\" is invalid on <%s> element in this context",
534 attribute_names[0], element_name);
542 start_busconfig_child (BusConfigParser *parser,
543 const char *element_name,
544 const char **attribute_names,
545 const char **attribute_values,
548 if (strcmp (element_name, "user") == 0)
550 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
553 if (push_element (parser, ELEMENT_USER) == NULL)
561 else if (strcmp (element_name, "type") == 0)
563 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
566 if (push_element (parser, ELEMENT_TYPE) == NULL)
574 else if (strcmp (element_name, "fork") == 0)
576 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
579 if (push_element (parser, ELEMENT_FORK) == NULL)
589 else if (strcmp (element_name, "pidfile") == 0)
591 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
594 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
602 else if (strcmp (element_name, "listen") == 0)
604 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
607 if (push_element (parser, ELEMENT_LISTEN) == NULL)
615 else if (strcmp (element_name, "auth") == 0)
617 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
620 if (push_element (parser, ELEMENT_AUTH) == NULL)
628 else if (strcmp (element_name, "includedir") == 0)
630 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
633 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
641 else if (strcmp (element_name, "servicedir") == 0)
643 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
646 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
654 else if (strcmp (element_name, "include") == 0)
657 const char *ignore_missing;
659 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
665 e->d.include.ignore_missing = FALSE;
667 if (!locate_attributes (parser, "include",
671 "ignore_missing", &ignore_missing,
675 if (ignore_missing != NULL)
677 if (strcmp (ignore_missing, "yes") == 0)
678 e->d.include.ignore_missing = TRUE;
679 else if (strcmp (ignore_missing, "no") == 0)
680 e->d.include.ignore_missing = FALSE;
683 dbus_set_error (error, DBUS_ERROR_FAILED,
684 "ignore_missing attribute must have value \"yes\" or \"no\"");
691 else if (strcmp (element_name, "policy") == 0)
698 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
704 e->d.policy.type = POLICY_IGNORED;
706 if (!locate_attributes (parser, "policy",
716 if (((context && user) ||
717 (context && group)) ||
719 !(context || user || group))
721 dbus_set_error (error, DBUS_ERROR_FAILED,
722 "<policy> element must have exactly one of (context|user|group) attributes");
728 if (strcmp (context, "default") == 0)
730 e->d.policy.type = POLICY_DEFAULT;
732 else if (strcmp (context, "mandatory") == 0)
734 e->d.policy.type = POLICY_MANDATORY;
738 dbus_set_error (error, DBUS_ERROR_FAILED,
739 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
744 else if (user != NULL)
747 _dbus_string_init_const (&username, user);
749 if (_dbus_get_user_id (&username,
750 &e->d.policy.gid_or_uid))
751 e->d.policy.type = POLICY_USER;
753 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
756 else if (group != NULL)
758 DBusString group_name;
759 _dbus_string_init_const (&group_name, group);
761 if (_dbus_get_group_id (&group_name,
762 &e->d.policy.gid_or_uid))
763 e->d.policy.type = POLICY_GROUP;
765 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
770 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
775 else if (strcmp (element_name, "limit") == 0)
780 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
786 if (!locate_attributes (parser, "limit",
796 dbus_set_error (error, DBUS_ERROR_FAILED,
797 "<limit> element must have a \"name\" attribute");
801 e->d.limit.name = _dbus_strdup (name);
802 if (e->d.limit.name == NULL)
812 dbus_set_error (error, DBUS_ERROR_FAILED,
813 "Element <%s> not allowed inside <%s> in configuration file",
814 element_name, "busconfig");
820 append_rule_from_element (BusConfigParser *parser,
821 const char *element_name,
822 const char **attribute_names,
823 const char **attribute_values,
827 const char *send_interface;
828 const char *send_member;
829 const char *send_error;
830 const char *send_destination;
831 const char *send_path;
832 const char *send_type;
833 const char *receive_interface;
834 const char *receive_member;
835 const char *receive_error;
836 const char *receive_sender;
837 const char *receive_path;
838 const char *receive_type;
839 const char *eavesdrop;
840 const char *requested_reply;
846 if (!locate_attributes (parser, element_name,
850 "send_interface", &send_interface,
851 "send_member", &send_member,
852 "send_error", &send_error,
853 "send_destination", &send_destination,
854 "send_path", &send_path,
855 "send_type", &send_type,
856 "receive_interface", &receive_interface,
857 "receive_member", &receive_member,
858 "receive_error", &receive_error,
859 "receive_sender", &receive_sender,
860 "receive_path", &receive_path,
861 "receive_type", &receive_type,
862 "eavesdrop", &eavesdrop,
863 "requested_reply", &requested_reply,
870 if (!(send_interface || send_member || send_error || send_destination ||
871 send_type || send_path ||
872 receive_interface || receive_member || receive_error || receive_sender ||
873 receive_type || receive_path || eavesdrop || requested_reply ||
874 own || user || group))
876 dbus_set_error (error, DBUS_ERROR_FAILED,
877 "Element <%s> must have one or more attributes",
882 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
883 (receive_member && (receive_interface == NULL && receive_path == NULL)))
885 dbus_set_error (error, DBUS_ERROR_FAILED,
886 "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.",
891 /* Allowed combinations of elements are:
893 * base, must be all send or all receive:
899 * base send_ can combine with send_destination, send_path, send_type
900 * base receive_ with receive_sender, receive_path, receive_type, eavesdrop, requested_reply
902 * user, group, own must occur alone
904 * Pretty sure the below stuff is broken, FIXME think about it more.
907 if (((send_interface && send_error) ||
908 (send_interface && receive_interface) ||
909 (send_interface && receive_member) ||
910 (send_interface && receive_error) ||
911 (send_interface && receive_sender) ||
912 (send_interface && eavesdrop) ||
913 (send_interface && requested_reply) ||
914 (send_interface && own) ||
915 (send_interface && user) ||
916 (send_interface && group)) ||
918 ((send_member && send_error) ||
919 (send_member && receive_interface) ||
920 (send_member && receive_member) ||
921 (send_member && receive_error) ||
922 (send_member && receive_sender) ||
923 (send_member && eavesdrop) ||
924 (send_member && requested_reply) ||
925 (send_member && own) ||
926 (send_member && user) ||
927 (send_member && group)) ||
929 ((send_error && receive_interface) ||
930 (send_error && receive_member) ||
931 (send_error && receive_error) ||
932 (send_error && receive_sender) ||
933 (send_error && eavesdrop) ||
934 (send_error && requested_reply) ||
935 (send_error && own) ||
936 (send_error && user) ||
937 (send_error && group)) ||
939 ((send_destination && receive_interface) ||
940 (send_destination && receive_member) ||
941 (send_destination && receive_error) ||
942 (send_destination && receive_sender) ||
943 (send_destination && eavesdrop) ||
944 (send_destination && requested_reply) ||
945 (send_destination && own) ||
946 (send_destination && user) ||
947 (send_destination && group)) ||
949 ((send_type && receive_interface) ||
950 (send_type && receive_member) ||
951 (send_type && receive_error) ||
952 (send_type && receive_sender) ||
953 (send_type && eavesdrop) ||
954 (send_type && requested_reply) ||
955 (send_type && own) ||
956 (send_type && user) ||
957 (send_type && group)) ||
959 ((send_path && receive_interface) ||
960 (send_path && receive_member) ||
961 (send_path && receive_error) ||
962 (send_path && receive_sender) ||
963 (send_path && eavesdrop) ||
964 (send_path && requested_reply) ||
965 (send_path && own) ||
966 (send_path && user) ||
967 (send_path && group)) ||
969 ((receive_interface && receive_error) ||
970 (receive_interface && own) ||
971 (receive_interface && user) ||
972 (receive_interface && group)) ||
974 ((receive_member && receive_error) ||
975 (receive_member && own) ||
976 (receive_member && user) ||
977 (receive_member && group)) ||
979 ((receive_error && own) ||
980 (receive_error && user) ||
981 (receive_error && group)) ||
983 ((eavesdrop && own) ||
984 (eavesdrop && user) ||
985 (eavesdrop && group)) ||
987 ((requested_reply && own) ||
988 (requested_reply && user) ||
989 (requested_reply && group)) ||
996 dbus_set_error (error, DBUS_ERROR_FAILED,
997 "Invalid combination of attributes on element <%s>",
1004 /* In BusPolicyRule, NULL represents wildcard.
1005 * In the config file, '*' represents it.
1007 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1009 if (send_interface || send_member || send_error || send_destination ||
1010 send_path || send_type)
1014 if (IS_WILDCARD (send_interface))
1015 send_interface = NULL;
1016 if (IS_WILDCARD (send_member))
1018 if (IS_WILDCARD (send_error))
1020 if (IS_WILDCARD (send_destination))
1021 send_destination = NULL;
1022 if (IS_WILDCARD (send_path))
1024 if (IS_WILDCARD (send_type))
1027 message_type = DBUS_MESSAGE_TYPE_INVALID;
1028 if (send_type != NULL)
1030 message_type = dbus_message_type_from_string (send_type);
1031 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1033 dbus_set_error (error, DBUS_ERROR_FAILED,
1034 "Bad message type \"%s\"",
1040 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1044 rule->d.send.message_type = message_type;
1045 rule->d.send.path = _dbus_strdup (send_path);
1046 rule->d.send.interface = _dbus_strdup (send_interface);
1047 rule->d.send.member = _dbus_strdup (send_member);
1048 rule->d.send.error = _dbus_strdup (send_error);
1049 rule->d.send.destination = _dbus_strdup (send_destination);
1050 if (send_path && rule->d.send.path == NULL)
1052 if (send_interface && rule->d.send.interface == NULL)
1054 if (send_member && rule->d.send.member == NULL)
1056 if (send_error && rule->d.send.error == NULL)
1058 if (send_destination && rule->d.send.destination == NULL)
1061 else if (receive_interface || receive_member || receive_error || receive_sender ||
1062 receive_path || receive_type || eavesdrop || requested_reply)
1066 if (IS_WILDCARD (receive_interface))
1067 receive_interface = NULL;
1068 if (IS_WILDCARD (receive_member))
1069 receive_member = NULL;
1070 if (IS_WILDCARD (receive_error))
1071 receive_error = NULL;
1072 if (IS_WILDCARD (receive_sender))
1073 receive_sender = NULL;
1074 if (IS_WILDCARD (receive_path))
1075 receive_path = NULL;
1076 if (IS_WILDCARD (receive_type))
1077 receive_type = NULL;
1079 message_type = DBUS_MESSAGE_TYPE_INVALID;
1080 if (receive_type != NULL)
1082 message_type = dbus_message_type_from_string (receive_type);
1083 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1085 dbus_set_error (error, DBUS_ERROR_FAILED,
1086 "Bad message type \"%s\"",
1094 !(strcmp (eavesdrop, "true") == 0 ||
1095 strcmp (eavesdrop, "false") == 0))
1097 dbus_set_error (error, DBUS_ERROR_FAILED,
1098 "Bad value \"%s\" for %s attribute, must be true or false",
1099 "eavesdrop", eavesdrop);
1103 if (requested_reply &&
1104 !(strcmp (requested_reply, "true") == 0 ||
1105 strcmp (requested_reply, "false") == 0))
1107 dbus_set_error (error, DBUS_ERROR_FAILED,
1108 "Bad value \"%s\" for %s attribute, must be true or false",
1109 "requested_reply", requested_reply);
1113 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1118 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1120 if (requested_reply)
1121 rule->d.receive.requested_reply = (strcmp (requested_reply, "true") == 0);
1123 rule->d.receive.message_type = message_type;
1124 rule->d.receive.path = _dbus_strdup (receive_path);
1125 rule->d.receive.interface = _dbus_strdup (receive_interface);
1126 rule->d.receive.member = _dbus_strdup (receive_member);
1127 rule->d.receive.error = _dbus_strdup (receive_error);
1128 rule->d.receive.origin = _dbus_strdup (receive_sender);
1130 if (receive_path && rule->d.receive.path == NULL)
1132 if (receive_interface && rule->d.receive.interface == NULL)
1134 if (receive_member && rule->d.receive.member == NULL)
1136 if (receive_error && rule->d.receive.error == NULL)
1138 if (receive_sender && rule->d.receive.origin == NULL)
1143 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1147 if (IS_WILDCARD (own))
1150 rule->d.own.service_name = _dbus_strdup (own);
1151 if (own && rule->d.own.service_name == NULL)
1156 if (IS_WILDCARD (user))
1158 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1162 rule->d.user.uid = DBUS_UID_UNSET;
1166 DBusString username;
1169 _dbus_string_init_const (&username, user);
1171 if (_dbus_get_user_id (&username, &uid))
1173 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1177 rule->d.user.uid = uid;
1181 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1182 user, element_name);
1188 if (IS_WILDCARD (group))
1190 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1194 rule->d.group.gid = DBUS_GID_UNSET;
1198 DBusString groupname;
1201 _dbus_string_init_const (&groupname, group);
1203 if (_dbus_get_user_id (&groupname, &gid))
1205 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1209 rule->d.group.gid = gid;
1213 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1214 group, element_name);
1219 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1225 pe = peek_element (parser);
1226 _dbus_assert (pe != NULL);
1227 _dbus_assert (pe->type == ELEMENT_POLICY);
1229 switch (pe->d.policy.type)
1231 case POLICY_IGNORED:
1232 /* drop the rule on the floor */
1235 case POLICY_DEFAULT:
1236 if (!bus_policy_append_default_rule (parser->policy, rule))
1239 case POLICY_MANDATORY:
1240 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1244 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1246 dbus_set_error (error, DBUS_ERROR_FAILED,
1247 "<%s> rule cannot be per-user because it has bus-global semantics",
1252 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
1257 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1259 dbus_set_error (error, DBUS_ERROR_FAILED,
1260 "<%s> rule cannot be per-group because it has bus-global semantics",
1265 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
1271 bus_policy_rule_unref (rule);
1278 BUS_SET_OOM (error);
1281 bus_policy_rule_unref (rule);
1286 start_policy_child (BusConfigParser *parser,
1287 const char *element_name,
1288 const char **attribute_names,
1289 const char **attribute_values,
1292 if (strcmp (element_name, "allow") == 0)
1294 if (!append_rule_from_element (parser, element_name,
1295 attribute_names, attribute_values,
1299 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1301 BUS_SET_OOM (error);
1307 else if (strcmp (element_name, "deny") == 0)
1309 if (!append_rule_from_element (parser, element_name,
1310 attribute_names, attribute_values,
1314 if (push_element (parser, ELEMENT_DENY) == NULL)
1316 BUS_SET_OOM (error);
1324 dbus_set_error (error, DBUS_ERROR_FAILED,
1325 "Element <%s> not allowed inside <%s> in configuration file",
1326 element_name, "policy");
1332 bus_config_parser_start_element (BusConfigParser *parser,
1333 const char *element_name,
1334 const char **attribute_names,
1335 const char **attribute_values,
1340 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1342 /* printf ("START: %s\n", element_name); */
1344 t = top_element_type (parser);
1346 if (t == ELEMENT_NONE)
1348 if (strcmp (element_name, "busconfig") == 0)
1350 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1353 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1355 BUS_SET_OOM (error);
1363 dbus_set_error (error, DBUS_ERROR_FAILED,
1364 "Unknown element <%s> at root of configuration file",
1369 else if (t == ELEMENT_BUSCONFIG)
1371 return start_busconfig_child (parser, element_name,
1372 attribute_names, attribute_values,
1375 else if (t == ELEMENT_POLICY)
1377 return start_policy_child (parser, element_name,
1378 attribute_names, attribute_values,
1383 dbus_set_error (error, DBUS_ERROR_FAILED,
1384 "Element <%s> is not allowed in this context",
1391 set_limit (BusConfigParser *parser,
1396 dbus_bool_t must_be_positive;
1397 dbus_bool_t must_be_int;
1399 must_be_int = FALSE;
1400 must_be_positive = FALSE;
1402 if (strcmp (name, "max_incoming_bytes") == 0)
1404 must_be_positive = TRUE;
1405 parser->limits.max_incoming_bytes = value;
1407 else if (strcmp (name, "max_outgoing_bytes") == 0)
1409 must_be_positive = TRUE;
1410 parser->limits.max_outgoing_bytes = value;
1412 else if (strcmp (name, "max_message_size") == 0)
1414 must_be_positive = TRUE;
1415 parser->limits.max_message_size = value;
1417 else if (strcmp (name, "activation_timeout") == 0)
1419 must_be_positive = TRUE;
1421 parser->limits.activation_timeout = value;
1423 else if (strcmp (name, "auth_timeout") == 0)
1425 must_be_positive = TRUE;
1427 parser->limits.auth_timeout = value;
1429 else if (strcmp (name, "reply_timeout") == 0)
1431 must_be_positive = TRUE;
1433 parser->limits.reply_timeout = value;
1435 else if (strcmp (name, "max_completed_connections") == 0)
1437 must_be_positive = TRUE;
1439 parser->limits.max_completed_connections = value;
1441 else if (strcmp (name, "max_incomplete_connections") == 0)
1443 must_be_positive = TRUE;
1445 parser->limits.max_incomplete_connections = value;
1447 else if (strcmp (name, "max_connections_per_user") == 0)
1449 must_be_positive = TRUE;
1451 parser->limits.max_connections_per_user = value;
1453 else if (strcmp (name, "max_pending_activations") == 0)
1455 must_be_positive = TRUE;
1457 parser->limits.max_pending_activations = value;
1459 else if (strcmp (name, "max_services_per_connection") == 0)
1461 must_be_positive = TRUE;
1463 parser->limits.max_services_per_connection = value;
1465 else if (strcmp (name, "max_replies_per_connection") == 0)
1467 must_be_positive = TRUE;
1469 parser->limits.max_replies_per_connection = value;
1473 dbus_set_error (error, DBUS_ERROR_FAILED,
1474 "There is no limit called \"%s\"\n",
1479 if (must_be_positive && value < 0)
1481 dbus_set_error (error, DBUS_ERROR_FAILED,
1482 "<limit name=\"%s\"> must be a positive number\n",
1488 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1490 dbus_set_error (error, DBUS_ERROR_FAILED,
1491 "<limit name=\"%s\"> value is too large\n",
1500 bus_config_parser_end_element (BusConfigParser *parser,
1501 const char *element_name,
1508 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1510 /* printf ("END: %s\n", element_name); */
1512 t = top_element_type (parser);
1514 if (t == ELEMENT_NONE)
1516 /* should probably be an assertion failure but
1517 * being paranoid about XML parsers
1519 dbus_set_error (error, DBUS_ERROR_FAILED,
1520 "XML parser ended element with no element on the stack");
1524 n = element_type_to_name (t);
1525 _dbus_assert (n != NULL);
1526 if (strcmp (n, element_name) != 0)
1528 /* should probably be an assertion failure but
1529 * being paranoid about XML parsers
1531 dbus_set_error (error, DBUS_ERROR_FAILED,
1532 "XML element <%s> ended but topmost element on the stack was <%s>",
1537 e = peek_element (parser);
1538 _dbus_assert (e != NULL);
1543 _dbus_assert_not_reached ("element in stack has no type");
1546 case ELEMENT_INCLUDE:
1549 case ELEMENT_LISTEN:
1550 case ELEMENT_PIDFILE:
1552 case ELEMENT_SERVICEDIR:
1553 case ELEMENT_INCLUDEDIR:
1555 if (!e->had_content)
1557 dbus_set_error (error, DBUS_ERROR_FAILED,
1558 "XML element <%s> was expected to have content inside it",
1559 element_type_to_name (e->type));
1563 if (e->type == ELEMENT_LIMIT)
1565 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1571 case ELEMENT_BUSCONFIG:
1572 case ELEMENT_POLICY:
1579 pop_element (parser);
1585 all_whitespace (const DBusString *str)
1589 _dbus_string_skip_white (str, 0, &i);
1591 return i == _dbus_string_get_length (str);
1595 make_full_path (const DBusString *basedir,
1596 const DBusString *filename,
1597 DBusString *full_path)
1599 if (_dbus_path_is_absolute (filename))
1601 return _dbus_string_copy (filename, 0, full_path, 0);
1605 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1608 if (!_dbus_concat_dir_and_file (full_path, filename))
1616 include_file (BusConfigParser *parser,
1617 const DBusString *filename,
1618 dbus_bool_t ignore_missing,
1621 /* FIXME good test case for this would load each config file in the
1622 * test suite both alone, and as an include, and check
1623 * that the result is the same
1625 BusConfigParser *included;
1626 DBusError tmp_error;
1628 dbus_error_init (&tmp_error);
1629 included = bus_config_load (filename, FALSE, &tmp_error);
1630 if (included == NULL)
1632 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1634 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1637 dbus_error_free (&tmp_error);
1642 dbus_move_error (&tmp_error, error);
1648 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1650 if (!merge_included (parser, included, error))
1652 bus_config_parser_unref (included);
1656 bus_config_parser_unref (included);
1662 include_dir (BusConfigParser *parser,
1663 const DBusString *dirname,
1666 DBusString filename;
1668 DBusError tmp_error;
1671 if (!_dbus_string_init (&filename))
1673 BUS_SET_OOM (error);
1679 dir = _dbus_directory_open (dirname, error);
1684 dbus_error_init (&tmp_error);
1685 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1687 DBusString full_path;
1689 if (!_dbus_string_init (&full_path))
1691 BUS_SET_OOM (error);
1695 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1697 BUS_SET_OOM (error);
1698 _dbus_string_free (&full_path);
1702 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1704 BUS_SET_OOM (error);
1705 _dbus_string_free (&full_path);
1709 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1711 if (!include_file (parser, &full_path, TRUE, error))
1713 _dbus_string_free (&full_path);
1718 _dbus_string_free (&full_path);
1721 if (dbus_error_is_set (&tmp_error))
1723 dbus_move_error (&tmp_error, error);
1730 _dbus_string_free (&filename);
1733 _dbus_directory_close (dir);
1739 bus_config_parser_content (BusConfigParser *parser,
1740 const DBusString *content,
1745 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1751 _dbus_string_get_const_data (content, &c_str);
1753 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1757 e = peek_element (parser);
1760 dbus_set_error (error, DBUS_ERROR_FAILED,
1761 "Text content outside of any XML element in configuration file");
1764 else if (e->had_content)
1766 _dbus_assert_not_reached ("Element had multiple content blocks");
1770 switch (top_element_type (parser))
1773 _dbus_assert_not_reached ("element at top of stack has no type");
1776 case ELEMENT_BUSCONFIG:
1777 case ELEMENT_POLICY:
1781 if (all_whitespace (content))
1785 dbus_set_error (error, DBUS_ERROR_FAILED,
1786 "No text content expected inside XML element %s in configuration file",
1787 element_type_to_name (top_element_type (parser)));
1791 case ELEMENT_PIDFILE:
1795 e->had_content = TRUE;
1797 if (!_dbus_string_copy_data (content, &s))
1800 dbus_free (parser->pidfile);
1801 parser->pidfile = s;
1805 case ELEMENT_INCLUDE:
1807 DBusString full_path;
1809 e->had_content = TRUE;
1811 if (!_dbus_string_init (&full_path))
1814 if (!make_full_path (&parser->basedir, content, &full_path))
1816 _dbus_string_free (&full_path);
1820 if (!include_file (parser, &full_path,
1821 e->d.include.ignore_missing, error))
1823 _dbus_string_free (&full_path);
1827 _dbus_string_free (&full_path);
1831 case ELEMENT_INCLUDEDIR:
1833 DBusString full_path;
1835 e->had_content = TRUE;
1837 if (!_dbus_string_init (&full_path))
1840 if (!make_full_path (&parser->basedir, content, &full_path))
1842 _dbus_string_free (&full_path);
1846 if (!include_dir (parser, &full_path, error))
1848 _dbus_string_free (&full_path);
1852 _dbus_string_free (&full_path);
1860 e->had_content = TRUE;
1862 if (!_dbus_string_copy_data (content, &s))
1865 dbus_free (parser->user);
1874 e->had_content = TRUE;
1876 if (!_dbus_string_copy_data (content, &s))
1879 dbus_free (parser->bus_type);
1880 parser->bus_type = s;
1884 case ELEMENT_LISTEN:
1888 e->had_content = TRUE;
1890 if (!_dbus_string_copy_data (content, &s))
1893 if (!_dbus_list_append (&parser->listen_on,
1906 e->had_content = TRUE;
1908 if (!_dbus_string_copy_data (content, &s))
1911 if (!_dbus_list_append (&parser->mechanisms,
1920 case ELEMENT_SERVICEDIR:
1923 DBusString full_path;
1925 e->had_content = TRUE;
1927 if (!_dbus_string_init (&full_path))
1930 if (!make_full_path (&parser->basedir, content, &full_path))
1932 _dbus_string_free (&full_path);
1936 if (!_dbus_string_copy_data (&full_path, &s))
1938 _dbus_string_free (&full_path);
1942 if (!_dbus_list_append (&parser->service_dirs, s))
1944 _dbus_string_free (&full_path);
1949 _dbus_string_free (&full_path);
1957 e->had_content = TRUE;
1960 if (!_dbus_string_parse_int (content, 0, &val, NULL))
1962 dbus_set_error (error, DBUS_ERROR_FAILED,
1963 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
1968 e->d.limit.value = val;
1970 _dbus_verbose ("Loaded value %ld for limit %s\n",
1977 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1981 BUS_SET_OOM (error);
1986 bus_config_parser_finished (BusConfigParser *parser,
1989 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1991 if (parser->stack != NULL)
1993 dbus_set_error (error, DBUS_ERROR_FAILED,
1994 "Element <%s> was not closed in configuration file",
1995 element_type_to_name (top_element_type (parser)));
2000 if (parser->is_toplevel && parser->listen_on == NULL)
2002 dbus_set_error (error, DBUS_ERROR_FAILED,
2003 "Configuration file needs one or more <listen> elements giving addresses");
2011 bus_config_parser_get_user (BusConfigParser *parser)
2013 return parser->user;
2017 bus_config_parser_get_type (BusConfigParser *parser)
2019 return parser->bus_type;
2023 bus_config_parser_get_addresses (BusConfigParser *parser)
2025 return &parser->listen_on;
2029 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2031 return &parser->mechanisms;
2035 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2037 return &parser->service_dirs;
2041 bus_config_parser_get_fork (BusConfigParser *parser)
2043 return parser->fork;
2047 bus_config_parser_get_pidfile (BusConfigParser *parser)
2049 return parser->pidfile;
2053 bus_config_parser_steal_policy (BusConfigParser *parser)
2057 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2059 policy = parser->policy;
2061 parser->policy = NULL;
2066 /* Overwrite any limits that were set in the configuration file */
2068 bus_config_parser_get_limits (BusConfigParser *parser,
2071 *limits = parser->limits;
2074 #ifdef DBUS_BUILD_TESTS
2085 do_load (const DBusString *full_path,
2087 dbus_bool_t oom_possible)
2089 BusConfigParser *parser;
2092 dbus_error_init (&error);
2094 parser = bus_config_load (full_path, TRUE, &error);
2097 _DBUS_ASSERT_ERROR_IS_SET (&error);
2100 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2102 _dbus_verbose ("Failed to load valid file due to OOM\n");
2103 dbus_error_free (&error);
2106 else if (validity == VALID)
2108 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2111 dbus_error_free (&error);
2116 dbus_error_free (&error);
2122 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2124 bus_config_parser_unref (parser);
2126 if (validity == INVALID)
2128 _dbus_warn ("Accepted invalid file\n");
2138 const DBusString *full_path;
2143 check_loader_oom_func (void *data)
2145 LoaderOomData *d = data;
2147 return do_load (d->full_path, d->validity, TRUE);
2151 process_test_subdir (const DBusString *test_base_dir,
2155 DBusString test_directory;
2156 DBusString filename;
2164 if (!_dbus_string_init (&test_directory))
2165 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2167 _dbus_string_init_const (&filename, subdir);
2169 if (!_dbus_string_copy (test_base_dir, 0,
2170 &test_directory, 0))
2171 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2173 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2174 _dbus_assert_not_reached ("couldn't allocate full path");
2176 _dbus_string_free (&filename);
2177 if (!_dbus_string_init (&filename))
2178 _dbus_assert_not_reached ("didn't allocate filename string\n");
2180 dbus_error_init (&error);
2181 dir = _dbus_directory_open (&test_directory, &error);
2184 _dbus_warn ("Could not open %s: %s\n",
2185 _dbus_string_get_const_data (&test_directory),
2187 dbus_error_free (&error);
2191 printf ("Testing:\n");
2194 while (_dbus_directory_get_next_file (dir, &filename, &error))
2196 DBusString full_path;
2199 if (!_dbus_string_init (&full_path))
2200 _dbus_assert_not_reached ("couldn't init string");
2202 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2203 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2205 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2206 _dbus_assert_not_reached ("couldn't concat file to dir");
2208 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2210 _dbus_verbose ("Skipping non-.conf file %s\n",
2211 _dbus_string_get_const_data (&filename));
2212 _dbus_string_free (&full_path);
2216 printf (" %s\n", _dbus_string_get_const_data (&filename));
2218 _dbus_verbose (" expecting %s\n",
2219 validity == VALID ? "valid" :
2220 (validity == INVALID ? "invalid" :
2221 (validity == UNKNOWN ? "unknown" : "???")));
2223 d.full_path = &full_path;
2224 d.validity = validity;
2225 if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d))
2226 _dbus_assert_not_reached ("test failed");
2228 _dbus_string_free (&full_path);
2231 if (dbus_error_is_set (&error))
2233 _dbus_warn ("Could not get next file in %s: %s\n",
2234 _dbus_string_get_const_data (&test_directory),
2236 dbus_error_free (&error);
2245 _dbus_directory_close (dir);
2246 _dbus_string_free (&test_directory);
2247 _dbus_string_free (&filename);
2253 bus_config_parser_test (const DBusString *test_data_dir)
2255 if (test_data_dir == NULL ||
2256 _dbus_string_get_length (test_data_dir) == 0)
2258 printf ("No test data\n");
2262 if (!process_test_subdir (test_data_dir, "valid-config-files", VALID))
2268 #endif /* DBUS_BUILD_TESTS */