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 2.0
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;
359 bus_config_parser_unref (BusConfigParser *parser)
361 _dbus_assert (parser->refcount > 0);
363 parser->refcount -= 1;
365 if (parser->refcount == 0)
367 while (parser->stack != NULL)
368 pop_element (parser);
370 dbus_free (parser->user);
371 dbus_free (parser->bus_type);
372 dbus_free (parser->pidfile);
374 _dbus_list_foreach (&parser->listen_on,
375 (DBusForeachFunction) dbus_free,
378 _dbus_list_clear (&parser->listen_on);
380 _dbus_list_foreach (&parser->service_dirs,
381 (DBusForeachFunction) dbus_free,
384 _dbus_list_clear (&parser->service_dirs);
386 _dbus_list_foreach (&parser->mechanisms,
387 (DBusForeachFunction) dbus_free,
390 _dbus_list_clear (&parser->mechanisms);
392 _dbus_string_free (&parser->basedir);
395 bus_policy_unref (parser->policy);
402 bus_config_parser_check_doctype (BusConfigParser *parser,
406 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
408 if (strcmp (doctype, "busconfig") != 0)
410 dbus_set_error (error,
412 "Configuration file has the wrong document type %s",
427 locate_attributes (BusConfigParser *parser,
428 const char *element_name,
429 const char **attribute_names,
430 const char **attribute_values,
432 const char *first_attribute_name,
433 const char **first_attribute_retloc,
441 LocateAttr attrs[MAX_ATTRS];
445 _dbus_assert (first_attribute_name != NULL);
446 _dbus_assert (first_attribute_retloc != NULL);
451 attrs[0].name = first_attribute_name;
452 attrs[0].retloc = first_attribute_retloc;
453 *first_attribute_retloc = NULL;
455 va_start (args, first_attribute_retloc);
457 name = va_arg (args, const char*);
458 retloc = va_arg (args, const char**);
462 _dbus_assert (retloc != NULL);
463 _dbus_assert (n_attrs < MAX_ATTRS);
465 attrs[n_attrs].name = name;
466 attrs[n_attrs].retloc = retloc;
470 name = va_arg (args, const char*);
471 retloc = va_arg (args, const char**);
480 while (attribute_names[i])
489 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
491 retloc = attrs[j].retloc;
495 dbus_set_error (error, DBUS_ERROR_FAILED,
496 "Attribute \"%s\" repeated twice on the same <%s> element",
497 attrs[j].name, element_name);
502 *retloc = attribute_values[i];
511 dbus_set_error (error, DBUS_ERROR_FAILED,
512 "Attribute \"%s\" is invalid on <%s> element in this context",
513 attribute_names[i], element_name);
526 check_no_attributes (BusConfigParser *parser,
527 const char *element_name,
528 const char **attribute_names,
529 const char **attribute_values,
532 if (attribute_names[0] != NULL)
534 dbus_set_error (error, DBUS_ERROR_FAILED,
535 "Attribute \"%s\" is invalid on <%s> element in this context",
536 attribute_names[0], element_name);
544 start_busconfig_child (BusConfigParser *parser,
545 const char *element_name,
546 const char **attribute_names,
547 const char **attribute_values,
550 if (strcmp (element_name, "user") == 0)
552 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
555 if (push_element (parser, ELEMENT_USER) == NULL)
563 else if (strcmp (element_name, "type") == 0)
565 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
568 if (push_element (parser, ELEMENT_TYPE) == NULL)
576 else if (strcmp (element_name, "fork") == 0)
578 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
581 if (push_element (parser, ELEMENT_FORK) == NULL)
591 else if (strcmp (element_name, "pidfile") == 0)
593 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
596 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
604 else if (strcmp (element_name, "listen") == 0)
606 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
609 if (push_element (parser, ELEMENT_LISTEN) == NULL)
617 else if (strcmp (element_name, "auth") == 0)
619 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
622 if (push_element (parser, ELEMENT_AUTH) == NULL)
630 else if (strcmp (element_name, "includedir") == 0)
632 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
635 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
643 else if (strcmp (element_name, "servicedir") == 0)
645 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
648 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
656 else if (strcmp (element_name, "include") == 0)
659 const char *ignore_missing;
661 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
667 e->d.include.ignore_missing = FALSE;
669 if (!locate_attributes (parser, "include",
673 "ignore_missing", &ignore_missing,
677 if (ignore_missing != NULL)
679 if (strcmp (ignore_missing, "yes") == 0)
680 e->d.include.ignore_missing = TRUE;
681 else if (strcmp (ignore_missing, "no") == 0)
682 e->d.include.ignore_missing = FALSE;
685 dbus_set_error (error, DBUS_ERROR_FAILED,
686 "ignore_missing attribute must have value \"yes\" or \"no\"");
693 else if (strcmp (element_name, "policy") == 0)
700 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
706 e->d.policy.type = POLICY_IGNORED;
708 if (!locate_attributes (parser, "policy",
718 if (((context && user) ||
719 (context && group)) ||
721 !(context || user || group))
723 dbus_set_error (error, DBUS_ERROR_FAILED,
724 "<policy> element must have exactly one of (context|user|group) attributes");
730 if (strcmp (context, "default") == 0)
732 e->d.policy.type = POLICY_DEFAULT;
734 else if (strcmp (context, "mandatory") == 0)
736 e->d.policy.type = POLICY_MANDATORY;
740 dbus_set_error (error, DBUS_ERROR_FAILED,
741 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
746 else if (user != NULL)
749 _dbus_string_init_const (&username, user);
751 if (_dbus_get_user_id (&username,
752 &e->d.policy.gid_or_uid))
753 e->d.policy.type = POLICY_USER;
755 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
758 else if (group != NULL)
760 DBusString group_name;
761 _dbus_string_init_const (&group_name, group);
763 if (_dbus_get_group_id (&group_name,
764 &e->d.policy.gid_or_uid))
765 e->d.policy.type = POLICY_GROUP;
767 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
772 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
777 else if (strcmp (element_name, "limit") == 0)
782 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
788 if (!locate_attributes (parser, "limit",
798 dbus_set_error (error, DBUS_ERROR_FAILED,
799 "<limit> element must have a \"name\" attribute");
803 e->d.limit.name = _dbus_strdup (name);
804 if (e->d.limit.name == NULL)
814 dbus_set_error (error, DBUS_ERROR_FAILED,
815 "Element <%s> not allowed inside <%s> in configuration file",
816 element_name, "busconfig");
822 append_rule_from_element (BusConfigParser *parser,
823 const char *element_name,
824 const char **attribute_names,
825 const char **attribute_values,
829 const char *send_interface;
830 const char *send_member;
831 const char *send_error;
832 const char *send_destination;
833 const char *send_path;
834 const char *send_type;
835 const char *receive_interface;
836 const char *receive_member;
837 const char *receive_error;
838 const char *receive_sender;
839 const char *receive_path;
840 const char *receive_type;
841 const char *eavesdrop;
842 const char *requested_reply;
848 if (!locate_attributes (parser, element_name,
852 "send_interface", &send_interface,
853 "send_member", &send_member,
854 "send_error", &send_error,
855 "send_destination", &send_destination,
856 "send_path", &send_path,
857 "send_type", &send_type,
858 "receive_interface", &receive_interface,
859 "receive_member", &receive_member,
860 "receive_error", &receive_error,
861 "receive_sender", &receive_sender,
862 "receive_path", &receive_path,
863 "receive_type", &receive_type,
864 "eavesdrop", &eavesdrop,
865 "requested_reply", &requested_reply,
872 if (!(send_interface || send_member || send_error || send_destination ||
873 send_type || send_path ||
874 receive_interface || receive_member || receive_error || receive_sender ||
875 receive_type || receive_path || eavesdrop || requested_reply ||
876 own || user || group))
878 dbus_set_error (error, DBUS_ERROR_FAILED,
879 "Element <%s> must have one or more attributes",
884 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
885 (receive_member && (receive_interface == NULL && receive_path == NULL)))
887 dbus_set_error (error, DBUS_ERROR_FAILED,
888 "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.",
893 /* Allowed combinations of elements are:
895 * base, must be all send or all receive:
901 * base send_ can combine with send_destination, send_path, send_type
902 * base receive_ with receive_sender, receive_path, receive_type, eavesdrop, requested_reply
904 * user, group, own must occur alone
906 * Pretty sure the below stuff is broken, FIXME think about it more.
909 if (((send_interface && send_error) ||
910 (send_interface && receive_interface) ||
911 (send_interface && receive_member) ||
912 (send_interface && receive_error) ||
913 (send_interface && receive_sender) ||
914 (send_interface && eavesdrop) ||
915 (send_interface && requested_reply) ||
916 (send_interface && own) ||
917 (send_interface && user) ||
918 (send_interface && group)) ||
920 ((send_member && send_error) ||
921 (send_member && receive_interface) ||
922 (send_member && receive_member) ||
923 (send_member && receive_error) ||
924 (send_member && receive_sender) ||
925 (send_member && eavesdrop) ||
926 (send_member && requested_reply) ||
927 (send_member && own) ||
928 (send_member && user) ||
929 (send_member && group)) ||
931 ((send_error && receive_interface) ||
932 (send_error && receive_member) ||
933 (send_error && receive_error) ||
934 (send_error && receive_sender) ||
935 (send_error && eavesdrop) ||
936 (send_error && requested_reply) ||
937 (send_error && own) ||
938 (send_error && user) ||
939 (send_error && group)) ||
941 ((send_destination && receive_interface) ||
942 (send_destination && receive_member) ||
943 (send_destination && receive_error) ||
944 (send_destination && receive_sender) ||
945 (send_destination && eavesdrop) ||
946 (send_destination && requested_reply) ||
947 (send_destination && own) ||
948 (send_destination && user) ||
949 (send_destination && group)) ||
951 ((send_type && receive_interface) ||
952 (send_type && receive_member) ||
953 (send_type && receive_error) ||
954 (send_type && receive_sender) ||
955 (send_type && eavesdrop) ||
956 (send_type && requested_reply) ||
957 (send_type && own) ||
958 (send_type && user) ||
959 (send_type && group)) ||
961 ((send_path && receive_interface) ||
962 (send_path && receive_member) ||
963 (send_path && receive_error) ||
964 (send_path && receive_sender) ||
965 (send_path && eavesdrop) ||
966 (send_path && requested_reply) ||
967 (send_path && own) ||
968 (send_path && user) ||
969 (send_path && group)) ||
971 ((receive_interface && receive_error) ||
972 (receive_interface && own) ||
973 (receive_interface && user) ||
974 (receive_interface && group)) ||
976 ((receive_member && receive_error) ||
977 (receive_member && own) ||
978 (receive_member && user) ||
979 (receive_member && group)) ||
981 ((receive_error && own) ||
982 (receive_error && user) ||
983 (receive_error && group)) ||
985 ((eavesdrop && own) ||
986 (eavesdrop && user) ||
987 (eavesdrop && group)) ||
989 ((requested_reply && own) ||
990 (requested_reply && user) ||
991 (requested_reply && group)) ||
998 dbus_set_error (error, DBUS_ERROR_FAILED,
999 "Invalid combination of attributes on element <%s>",
1006 /* In BusPolicyRule, NULL represents wildcard.
1007 * In the config file, '*' represents it.
1009 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
1011 if (send_interface || send_member || send_error || send_destination ||
1012 send_path || send_type)
1016 if (IS_WILDCARD (send_interface))
1017 send_interface = NULL;
1018 if (IS_WILDCARD (send_member))
1020 if (IS_WILDCARD (send_error))
1022 if (IS_WILDCARD (send_destination))
1023 send_destination = NULL;
1024 if (IS_WILDCARD (send_path))
1026 if (IS_WILDCARD (send_type))
1029 message_type = DBUS_MESSAGE_TYPE_INVALID;
1030 if (send_type != NULL)
1032 message_type = dbus_message_type_from_string (send_type);
1033 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1035 dbus_set_error (error, DBUS_ERROR_FAILED,
1036 "Bad message type \"%s\"",
1042 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1046 rule->d.send.message_type = message_type;
1047 rule->d.send.path = _dbus_strdup (send_path);
1048 rule->d.send.interface = _dbus_strdup (send_interface);
1049 rule->d.send.member = _dbus_strdup (send_member);
1050 rule->d.send.error = _dbus_strdup (send_error);
1051 rule->d.send.destination = _dbus_strdup (send_destination);
1052 if (send_path && rule->d.send.path == NULL)
1054 if (send_interface && rule->d.send.interface == NULL)
1056 if (send_member && rule->d.send.member == NULL)
1058 if (send_error && rule->d.send.error == NULL)
1060 if (send_destination && rule->d.send.destination == NULL)
1063 else if (receive_interface || receive_member || receive_error || receive_sender ||
1064 receive_path || receive_type || eavesdrop || requested_reply)
1068 if (IS_WILDCARD (receive_interface))
1069 receive_interface = NULL;
1070 if (IS_WILDCARD (receive_member))
1071 receive_member = NULL;
1072 if (IS_WILDCARD (receive_error))
1073 receive_error = NULL;
1074 if (IS_WILDCARD (receive_sender))
1075 receive_sender = NULL;
1076 if (IS_WILDCARD (receive_path))
1077 receive_path = NULL;
1078 if (IS_WILDCARD (receive_type))
1079 receive_type = NULL;
1081 message_type = DBUS_MESSAGE_TYPE_INVALID;
1082 if (receive_type != NULL)
1084 message_type = dbus_message_type_from_string (receive_type);
1085 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1087 dbus_set_error (error, DBUS_ERROR_FAILED,
1088 "Bad message type \"%s\"",
1096 !(strcmp (eavesdrop, "true") == 0 ||
1097 strcmp (eavesdrop, "false") == 0))
1099 dbus_set_error (error, DBUS_ERROR_FAILED,
1100 "Bad value \"%s\" for %s attribute, must be true or false",
1101 "eavesdrop", eavesdrop);
1105 if (requested_reply &&
1106 !(strcmp (requested_reply, "true") == 0 ||
1107 strcmp (requested_reply, "false") == 0))
1109 dbus_set_error (error, DBUS_ERROR_FAILED,
1110 "Bad value \"%s\" for %s attribute, must be true or false",
1111 "requested_reply", requested_reply);
1115 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1120 rule->d.receive.eavesdrop = (strcmp (eavesdrop, "true") == 0);
1122 if (requested_reply)
1123 rule->d.receive.requested_reply = (strcmp (requested_reply, "true") == 0);
1125 rule->d.receive.message_type = message_type;
1126 rule->d.receive.path = _dbus_strdup (receive_path);
1127 rule->d.receive.interface = _dbus_strdup (receive_interface);
1128 rule->d.receive.member = _dbus_strdup (receive_member);
1129 rule->d.receive.error = _dbus_strdup (receive_error);
1130 rule->d.receive.origin = _dbus_strdup (receive_sender);
1132 if (receive_path && rule->d.receive.path == NULL)
1134 if (receive_interface && rule->d.receive.interface == NULL)
1136 if (receive_member && rule->d.receive.member == NULL)
1138 if (receive_error && rule->d.receive.error == NULL)
1140 if (receive_sender && rule->d.receive.origin == NULL)
1145 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1149 if (IS_WILDCARD (own))
1152 rule->d.own.service_name = _dbus_strdup (own);
1153 if (own && rule->d.own.service_name == NULL)
1158 if (IS_WILDCARD (user))
1160 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1164 rule->d.user.uid = DBUS_UID_UNSET;
1168 DBusString username;
1171 _dbus_string_init_const (&username, user);
1173 if (_dbus_get_user_id (&username, &uid))
1175 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1179 rule->d.user.uid = uid;
1183 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1184 user, element_name);
1190 if (IS_WILDCARD (group))
1192 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1196 rule->d.group.gid = DBUS_GID_UNSET;
1200 DBusString groupname;
1203 _dbus_string_init_const (&groupname, group);
1205 if (_dbus_get_user_id (&groupname, &gid))
1207 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1211 rule->d.group.gid = gid;
1215 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1216 group, element_name);
1221 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1227 pe = peek_element (parser);
1228 _dbus_assert (pe != NULL);
1229 _dbus_assert (pe->type == ELEMENT_POLICY);
1231 switch (pe->d.policy.type)
1233 case POLICY_IGNORED:
1234 /* drop the rule on the floor */
1237 case POLICY_DEFAULT:
1238 if (!bus_policy_append_default_rule (parser->policy, rule))
1241 case POLICY_MANDATORY:
1242 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1246 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1248 dbus_set_error (error, DBUS_ERROR_FAILED,
1249 "<%s> rule cannot be per-user because it has bus-global semantics",
1254 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
1259 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1261 dbus_set_error (error, DBUS_ERROR_FAILED,
1262 "<%s> rule cannot be per-group because it has bus-global semantics",
1267 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
1273 bus_policy_rule_unref (rule);
1280 BUS_SET_OOM (error);
1283 bus_policy_rule_unref (rule);
1288 start_policy_child (BusConfigParser *parser,
1289 const char *element_name,
1290 const char **attribute_names,
1291 const char **attribute_values,
1294 if (strcmp (element_name, "allow") == 0)
1296 if (!append_rule_from_element (parser, element_name,
1297 attribute_names, attribute_values,
1301 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1303 BUS_SET_OOM (error);
1309 else if (strcmp (element_name, "deny") == 0)
1311 if (!append_rule_from_element (parser, element_name,
1312 attribute_names, attribute_values,
1316 if (push_element (parser, ELEMENT_DENY) == NULL)
1318 BUS_SET_OOM (error);
1326 dbus_set_error (error, DBUS_ERROR_FAILED,
1327 "Element <%s> not allowed inside <%s> in configuration file",
1328 element_name, "policy");
1334 bus_config_parser_start_element (BusConfigParser *parser,
1335 const char *element_name,
1336 const char **attribute_names,
1337 const char **attribute_values,
1342 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1344 /* printf ("START: %s\n", element_name); */
1346 t = top_element_type (parser);
1348 if (t == ELEMENT_NONE)
1350 if (strcmp (element_name, "busconfig") == 0)
1352 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1355 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1357 BUS_SET_OOM (error);
1365 dbus_set_error (error, DBUS_ERROR_FAILED,
1366 "Unknown element <%s> at root of configuration file",
1371 else if (t == ELEMENT_BUSCONFIG)
1373 return start_busconfig_child (parser, element_name,
1374 attribute_names, attribute_values,
1377 else if (t == ELEMENT_POLICY)
1379 return start_policy_child (parser, element_name,
1380 attribute_names, attribute_values,
1385 dbus_set_error (error, DBUS_ERROR_FAILED,
1386 "Element <%s> is not allowed in this context",
1393 set_limit (BusConfigParser *parser,
1398 dbus_bool_t must_be_positive;
1399 dbus_bool_t must_be_int;
1401 must_be_int = FALSE;
1402 must_be_positive = FALSE;
1404 if (strcmp (name, "max_incoming_bytes") == 0)
1406 must_be_positive = TRUE;
1407 parser->limits.max_incoming_bytes = value;
1409 else if (strcmp (name, "max_outgoing_bytes") == 0)
1411 must_be_positive = TRUE;
1412 parser->limits.max_outgoing_bytes = value;
1414 else if (strcmp (name, "max_message_size") == 0)
1416 must_be_positive = TRUE;
1417 parser->limits.max_message_size = value;
1419 else if (strcmp (name, "activation_timeout") == 0)
1421 must_be_positive = TRUE;
1423 parser->limits.activation_timeout = value;
1425 else if (strcmp (name, "auth_timeout") == 0)
1427 must_be_positive = TRUE;
1429 parser->limits.auth_timeout = value;
1431 else if (strcmp (name, "reply_timeout") == 0)
1433 must_be_positive = TRUE;
1435 parser->limits.reply_timeout = value;
1437 else if (strcmp (name, "max_completed_connections") == 0)
1439 must_be_positive = TRUE;
1441 parser->limits.max_completed_connections = value;
1443 else if (strcmp (name, "max_incomplete_connections") == 0)
1445 must_be_positive = TRUE;
1447 parser->limits.max_incomplete_connections = value;
1449 else if (strcmp (name, "max_connections_per_user") == 0)
1451 must_be_positive = TRUE;
1453 parser->limits.max_connections_per_user = value;
1455 else if (strcmp (name, "max_pending_activations") == 0)
1457 must_be_positive = TRUE;
1459 parser->limits.max_pending_activations = value;
1461 else if (strcmp (name, "max_services_per_connection") == 0)
1463 must_be_positive = TRUE;
1465 parser->limits.max_services_per_connection = value;
1467 else if (strcmp (name, "max_replies_per_connection") == 0)
1469 must_be_positive = TRUE;
1471 parser->limits.max_replies_per_connection = value;
1475 dbus_set_error (error, DBUS_ERROR_FAILED,
1476 "There is no limit called \"%s\"\n",
1481 if (must_be_positive && value < 0)
1483 dbus_set_error (error, DBUS_ERROR_FAILED,
1484 "<limit name=\"%s\"> must be a positive number\n",
1490 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1492 dbus_set_error (error, DBUS_ERROR_FAILED,
1493 "<limit name=\"%s\"> value is too large\n",
1502 bus_config_parser_end_element (BusConfigParser *parser,
1503 const char *element_name,
1510 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1512 /* printf ("END: %s\n", element_name); */
1514 t = top_element_type (parser);
1516 if (t == ELEMENT_NONE)
1518 /* should probably be an assertion failure but
1519 * being paranoid about XML parsers
1521 dbus_set_error (error, DBUS_ERROR_FAILED,
1522 "XML parser ended element with no element on the stack");
1526 n = element_type_to_name (t);
1527 _dbus_assert (n != NULL);
1528 if (strcmp (n, element_name) != 0)
1530 /* should probably be an assertion failure but
1531 * being paranoid about XML parsers
1533 dbus_set_error (error, DBUS_ERROR_FAILED,
1534 "XML element <%s> ended but topmost element on the stack was <%s>",
1539 e = peek_element (parser);
1540 _dbus_assert (e != NULL);
1545 _dbus_assert_not_reached ("element in stack has no type");
1548 case ELEMENT_INCLUDE:
1551 case ELEMENT_LISTEN:
1552 case ELEMENT_PIDFILE:
1554 case ELEMENT_SERVICEDIR:
1555 case ELEMENT_INCLUDEDIR:
1557 if (!e->had_content)
1559 dbus_set_error (error, DBUS_ERROR_FAILED,
1560 "XML element <%s> was expected to have content inside it",
1561 element_type_to_name (e->type));
1565 if (e->type == ELEMENT_LIMIT)
1567 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1573 case ELEMENT_BUSCONFIG:
1574 case ELEMENT_POLICY:
1581 pop_element (parser);
1587 all_whitespace (const DBusString *str)
1591 _dbus_string_skip_white (str, 0, &i);
1593 return i == _dbus_string_get_length (str);
1597 make_full_path (const DBusString *basedir,
1598 const DBusString *filename,
1599 DBusString *full_path)
1601 if (_dbus_path_is_absolute (filename))
1603 return _dbus_string_copy (filename, 0, full_path, 0);
1607 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1610 if (!_dbus_concat_dir_and_file (full_path, filename))
1618 include_file (BusConfigParser *parser,
1619 const DBusString *filename,
1620 dbus_bool_t ignore_missing,
1623 /* FIXME good test case for this would load each config file in the
1624 * test suite both alone, and as an include, and check
1625 * that the result is the same
1627 BusConfigParser *included;
1628 DBusError tmp_error;
1630 dbus_error_init (&tmp_error);
1631 included = bus_config_load (filename, FALSE, &tmp_error);
1632 if (included == NULL)
1634 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1636 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1639 dbus_error_free (&tmp_error);
1644 dbus_move_error (&tmp_error, error);
1650 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1652 if (!merge_included (parser, included, error))
1654 bus_config_parser_unref (included);
1658 bus_config_parser_unref (included);
1664 include_dir (BusConfigParser *parser,
1665 const DBusString *dirname,
1668 DBusString filename;
1670 DBusError tmp_error;
1673 if (!_dbus_string_init (&filename))
1675 BUS_SET_OOM (error);
1681 dir = _dbus_directory_open (dirname, error);
1686 dbus_error_init (&tmp_error);
1687 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1689 DBusString full_path;
1691 if (!_dbus_string_init (&full_path))
1693 BUS_SET_OOM (error);
1697 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1699 BUS_SET_OOM (error);
1700 _dbus_string_free (&full_path);
1704 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1706 BUS_SET_OOM (error);
1707 _dbus_string_free (&full_path);
1711 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1713 if (!include_file (parser, &full_path, TRUE, error))
1715 _dbus_string_free (&full_path);
1720 _dbus_string_free (&full_path);
1723 if (dbus_error_is_set (&tmp_error))
1725 dbus_move_error (&tmp_error, error);
1732 _dbus_string_free (&filename);
1735 _dbus_directory_close (dir);
1741 bus_config_parser_content (BusConfigParser *parser,
1742 const DBusString *content,
1747 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1753 _dbus_string_get_const_data (content, &c_str);
1755 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1759 e = peek_element (parser);
1762 dbus_set_error (error, DBUS_ERROR_FAILED,
1763 "Text content outside of any XML element in configuration file");
1766 else if (e->had_content)
1768 _dbus_assert_not_reached ("Element had multiple content blocks");
1772 switch (top_element_type (parser))
1775 _dbus_assert_not_reached ("element at top of stack has no type");
1778 case ELEMENT_BUSCONFIG:
1779 case ELEMENT_POLICY:
1783 if (all_whitespace (content))
1787 dbus_set_error (error, DBUS_ERROR_FAILED,
1788 "No text content expected inside XML element %s in configuration file",
1789 element_type_to_name (top_element_type (parser)));
1793 case ELEMENT_PIDFILE:
1797 e->had_content = TRUE;
1799 if (!_dbus_string_copy_data (content, &s))
1802 dbus_free (parser->pidfile);
1803 parser->pidfile = s;
1807 case ELEMENT_INCLUDE:
1809 DBusString full_path;
1811 e->had_content = TRUE;
1813 if (!_dbus_string_init (&full_path))
1816 if (!make_full_path (&parser->basedir, content, &full_path))
1818 _dbus_string_free (&full_path);
1822 if (!include_file (parser, &full_path,
1823 e->d.include.ignore_missing, error))
1825 _dbus_string_free (&full_path);
1829 _dbus_string_free (&full_path);
1833 case ELEMENT_INCLUDEDIR:
1835 DBusString full_path;
1837 e->had_content = TRUE;
1839 if (!_dbus_string_init (&full_path))
1842 if (!make_full_path (&parser->basedir, content, &full_path))
1844 _dbus_string_free (&full_path);
1848 if (!include_dir (parser, &full_path, error))
1850 _dbus_string_free (&full_path);
1854 _dbus_string_free (&full_path);
1862 e->had_content = TRUE;
1864 if (!_dbus_string_copy_data (content, &s))
1867 dbus_free (parser->user);
1876 e->had_content = TRUE;
1878 if (!_dbus_string_copy_data (content, &s))
1881 dbus_free (parser->bus_type);
1882 parser->bus_type = s;
1886 case ELEMENT_LISTEN:
1890 e->had_content = TRUE;
1892 if (!_dbus_string_copy_data (content, &s))
1895 if (!_dbus_list_append (&parser->listen_on,
1908 e->had_content = TRUE;
1910 if (!_dbus_string_copy_data (content, &s))
1913 if (!_dbus_list_append (&parser->mechanisms,
1922 case ELEMENT_SERVICEDIR:
1925 DBusString full_path;
1927 e->had_content = TRUE;
1929 if (!_dbus_string_init (&full_path))
1932 if (!make_full_path (&parser->basedir, content, &full_path))
1934 _dbus_string_free (&full_path);
1938 if (!_dbus_string_copy_data (&full_path, &s))
1940 _dbus_string_free (&full_path);
1944 if (!_dbus_list_append (&parser->service_dirs, s))
1946 _dbus_string_free (&full_path);
1951 _dbus_string_free (&full_path);
1959 e->had_content = TRUE;
1962 if (!_dbus_string_parse_int (content, 0, &val, NULL))
1964 dbus_set_error (error, DBUS_ERROR_FAILED,
1965 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
1970 e->d.limit.value = val;
1972 _dbus_verbose ("Loaded value %ld for limit %s\n",
1979 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1983 BUS_SET_OOM (error);
1988 bus_config_parser_finished (BusConfigParser *parser,
1991 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1993 if (parser->stack != NULL)
1995 dbus_set_error (error, DBUS_ERROR_FAILED,
1996 "Element <%s> was not closed in configuration file",
1997 element_type_to_name (top_element_type (parser)));
2002 if (parser->is_toplevel && parser->listen_on == NULL)
2004 dbus_set_error (error, DBUS_ERROR_FAILED,
2005 "Configuration file needs one or more <listen> elements giving addresses");
2013 bus_config_parser_get_user (BusConfigParser *parser)
2015 return parser->user;
2019 bus_config_parser_get_type (BusConfigParser *parser)
2021 return parser->bus_type;
2025 bus_config_parser_get_addresses (BusConfigParser *parser)
2027 return &parser->listen_on;
2031 bus_config_parser_get_mechanisms (BusConfigParser *parser)
2033 return &parser->mechanisms;
2037 bus_config_parser_get_service_dirs (BusConfigParser *parser)
2039 return &parser->service_dirs;
2043 bus_config_parser_get_fork (BusConfigParser *parser)
2045 return parser->fork;
2049 bus_config_parser_get_pidfile (BusConfigParser *parser)
2051 return parser->pidfile;
2055 bus_config_parser_steal_policy (BusConfigParser *parser)
2059 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2061 policy = parser->policy;
2063 parser->policy = NULL;
2068 /* Overwrite any limits that were set in the configuration file */
2070 bus_config_parser_get_limits (BusConfigParser *parser,
2073 *limits = parser->limits;
2076 #ifdef DBUS_BUILD_TESTS
2087 do_load (const DBusString *full_path,
2089 dbus_bool_t oom_possible)
2091 BusConfigParser *parser;
2094 dbus_error_init (&error);
2096 parser = bus_config_load (full_path, TRUE, &error);
2099 _DBUS_ASSERT_ERROR_IS_SET (&error);
2102 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2104 _dbus_verbose ("Failed to load valid file due to OOM\n");
2105 dbus_error_free (&error);
2108 else if (validity == VALID)
2110 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2113 dbus_error_free (&error);
2118 dbus_error_free (&error);
2124 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2126 bus_config_parser_unref (parser);
2128 if (validity == INVALID)
2130 _dbus_warn ("Accepted invalid file\n");
2140 const DBusString *full_path;
2145 check_loader_oom_func (void *data)
2147 LoaderOomData *d = data;
2149 return do_load (d->full_path, d->validity, TRUE);
2153 process_test_subdir (const DBusString *test_base_dir,
2157 DBusString test_directory;
2158 DBusString filename;
2166 if (!_dbus_string_init (&test_directory))
2167 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2169 _dbus_string_init_const (&filename, subdir);
2171 if (!_dbus_string_copy (test_base_dir, 0,
2172 &test_directory, 0))
2173 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2175 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2176 _dbus_assert_not_reached ("couldn't allocate full path");
2178 _dbus_string_free (&filename);
2179 if (!_dbus_string_init (&filename))
2180 _dbus_assert_not_reached ("didn't allocate filename string\n");
2182 dbus_error_init (&error);
2183 dir = _dbus_directory_open (&test_directory, &error);
2186 _dbus_warn ("Could not open %s: %s\n",
2187 _dbus_string_get_const_data (&test_directory),
2189 dbus_error_free (&error);
2193 printf ("Testing:\n");
2196 while (_dbus_directory_get_next_file (dir, &filename, &error))
2198 DBusString full_path;
2201 if (!_dbus_string_init (&full_path))
2202 _dbus_assert_not_reached ("couldn't init string");
2204 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2205 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2207 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2208 _dbus_assert_not_reached ("couldn't concat file to dir");
2210 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2212 _dbus_verbose ("Skipping non-.conf file %s\n",
2213 _dbus_string_get_const_data (&filename));
2214 _dbus_string_free (&full_path);
2218 printf (" %s\n", _dbus_string_get_const_data (&filename));
2220 _dbus_verbose (" expecting %s\n",
2221 validity == VALID ? "valid" :
2222 (validity == INVALID ? "invalid" :
2223 (validity == UNKNOWN ? "unknown" : "???")));
2225 d.full_path = &full_path;
2226 d.validity = validity;
2227 if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d))
2228 _dbus_assert_not_reached ("test failed");
2230 _dbus_string_free (&full_path);
2233 if (dbus_error_is_set (&error))
2235 _dbus_warn ("Could not get next file in %s: %s\n",
2236 _dbus_string_get_const_data (&test_directory),
2238 dbus_error_free (&error);
2247 _dbus_directory_close (dir);
2248 _dbus_string_free (&test_directory);
2249 _dbus_string_free (&filename);
2255 bus_config_parser_test (const DBusString *test_data_dir)
2257 if (test_data_dir == NULL ||
2258 _dbus_string_get_length (test_data_dir) == 0)
2260 printf ("No test data\n");
2264 if (!process_test_subdir (test_data_dir, "valid-config-files", VALID))
2270 #endif /* DBUS_BUILD_TESTS */