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->refcount = 1;
344 bus_config_parser_ref (BusConfigParser *parser)
346 _dbus_assert (parser->refcount > 0);
348 parser->refcount += 1;
352 bus_config_parser_unref (BusConfigParser *parser)
354 _dbus_assert (parser->refcount > 0);
356 parser->refcount -= 1;
358 if (parser->refcount == 0)
360 while (parser->stack != NULL)
361 pop_element (parser);
363 dbus_free (parser->user);
364 dbus_free (parser->bus_type);
365 dbus_free (parser->pidfile);
367 _dbus_list_foreach (&parser->listen_on,
368 (DBusForeachFunction) dbus_free,
371 _dbus_list_clear (&parser->listen_on);
373 _dbus_list_foreach (&parser->service_dirs,
374 (DBusForeachFunction) dbus_free,
377 _dbus_list_clear (&parser->service_dirs);
379 _dbus_list_foreach (&parser->mechanisms,
380 (DBusForeachFunction) dbus_free,
383 _dbus_list_clear (&parser->mechanisms);
385 _dbus_string_free (&parser->basedir);
388 bus_policy_unref (parser->policy);
395 bus_config_parser_check_doctype (BusConfigParser *parser,
399 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
401 if (strcmp (doctype, "busconfig") != 0)
403 dbus_set_error (error,
405 "Configuration file has the wrong document type %s",
420 locate_attributes (BusConfigParser *parser,
421 const char *element_name,
422 const char **attribute_names,
423 const char **attribute_values,
425 const char *first_attribute_name,
426 const char **first_attribute_retloc,
434 LocateAttr attrs[MAX_ATTRS];
438 _dbus_assert (first_attribute_name != NULL);
439 _dbus_assert (first_attribute_retloc != NULL);
444 attrs[0].name = first_attribute_name;
445 attrs[0].retloc = first_attribute_retloc;
446 *first_attribute_retloc = NULL;
448 va_start (args, first_attribute_retloc);
450 name = va_arg (args, const char*);
451 retloc = va_arg (args, const char**);
455 _dbus_assert (retloc != NULL);
456 _dbus_assert (n_attrs < MAX_ATTRS);
458 attrs[n_attrs].name = name;
459 attrs[n_attrs].retloc = retloc;
463 name = va_arg (args, const char*);
464 retloc = va_arg (args, const char**);
473 while (attribute_names[i])
482 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
484 retloc = attrs[j].retloc;
488 dbus_set_error (error, DBUS_ERROR_FAILED,
489 "Attribute \"%s\" repeated twice on the same <%s> element",
490 attrs[j].name, element_name);
495 *retloc = attribute_values[i];
504 dbus_set_error (error, DBUS_ERROR_FAILED,
505 "Attribute \"%s\" is invalid on <%s> element in this context",
506 attribute_names[i], element_name);
519 check_no_attributes (BusConfigParser *parser,
520 const char *element_name,
521 const char **attribute_names,
522 const char **attribute_values,
525 if (attribute_names[0] != NULL)
527 dbus_set_error (error, DBUS_ERROR_FAILED,
528 "Attribute \"%s\" is invalid on <%s> element in this context",
529 attribute_names[0], element_name);
537 start_busconfig_child (BusConfigParser *parser,
538 const char *element_name,
539 const char **attribute_names,
540 const char **attribute_values,
543 if (strcmp (element_name, "user") == 0)
545 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
548 if (push_element (parser, ELEMENT_USER) == NULL)
556 else if (strcmp (element_name, "type") == 0)
558 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
561 if (push_element (parser, ELEMENT_TYPE) == NULL)
569 else if (strcmp (element_name, "fork") == 0)
571 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
574 if (push_element (parser, ELEMENT_FORK) == NULL)
584 else if (strcmp (element_name, "pidfile") == 0)
586 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
589 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
597 else if (strcmp (element_name, "listen") == 0)
599 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
602 if (push_element (parser, ELEMENT_LISTEN) == NULL)
610 else if (strcmp (element_name, "auth") == 0)
612 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
615 if (push_element (parser, ELEMENT_AUTH) == NULL)
623 else if (strcmp (element_name, "includedir") == 0)
625 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
628 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
636 else if (strcmp (element_name, "servicedir") == 0)
638 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
641 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
649 else if (strcmp (element_name, "include") == 0)
652 const char *ignore_missing;
654 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
660 e->d.include.ignore_missing = FALSE;
662 if (!locate_attributes (parser, "include",
666 "ignore_missing", &ignore_missing,
670 if (ignore_missing != NULL)
672 if (strcmp (ignore_missing, "yes") == 0)
673 e->d.include.ignore_missing = TRUE;
674 else if (strcmp (ignore_missing, "no") == 0)
675 e->d.include.ignore_missing = FALSE;
678 dbus_set_error (error, DBUS_ERROR_FAILED,
679 "ignore_missing attribute must have value \"yes\" or \"no\"");
686 else if (strcmp (element_name, "policy") == 0)
693 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
699 e->d.policy.type = POLICY_IGNORED;
701 if (!locate_attributes (parser, "policy",
711 if (((context && user) ||
712 (context && group)) ||
714 !(context || user || group))
716 dbus_set_error (error, DBUS_ERROR_FAILED,
717 "<policy> element must have exactly one of (context|user|group) attributes");
723 if (strcmp (context, "default") == 0)
725 e->d.policy.type = POLICY_DEFAULT;
727 else if (strcmp (context, "mandatory") == 0)
729 e->d.policy.type = POLICY_MANDATORY;
733 dbus_set_error (error, DBUS_ERROR_FAILED,
734 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
739 else if (user != NULL)
742 _dbus_string_init_const (&username, user);
744 if (_dbus_get_user_id (&username,
745 &e->d.policy.gid_or_uid))
746 e->d.policy.type = POLICY_USER;
748 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
751 else if (group != NULL)
753 DBusString group_name;
754 _dbus_string_init_const (&group_name, group);
756 if (_dbus_get_group_id (&group_name,
757 &e->d.policy.gid_or_uid))
758 e->d.policy.type = POLICY_GROUP;
760 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
765 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
770 else if (strcmp (element_name, "limit") == 0)
775 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
781 if (!locate_attributes (parser, "limit",
791 dbus_set_error (error, DBUS_ERROR_FAILED,
792 "<limit> element must have a \"name\" attribute");
796 e->d.limit.name = _dbus_strdup (name);
797 if (e->d.limit.name == NULL)
807 dbus_set_error (error, DBUS_ERROR_FAILED,
808 "Element <%s> not allowed inside <%s> in configuration file",
809 element_name, "busconfig");
815 message_type_from_string (const char *type_str)
817 if (strcmp (type_str, "method_call") == 0)
818 return DBUS_MESSAGE_TYPE_METHOD_CALL;
819 if (strcmp (type_str, "method_return") == 0)
820 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
821 else if (strcmp (type_str, "signal") == 0)
822 return DBUS_MESSAGE_TYPE_SIGNAL;
823 else if (strcmp (type_str, "error") == 0)
824 return DBUS_MESSAGE_TYPE_ERROR;
826 return DBUS_MESSAGE_TYPE_INVALID;
830 append_rule_from_element (BusConfigParser *parser,
831 const char *element_name,
832 const char **attribute_names,
833 const char **attribute_values,
837 const char *send_interface;
838 const char *send_member;
839 const char *send_error;
840 const char *send_service;
841 const char *send_path;
842 const char *send_type;
843 const char *receive_interface;
844 const char *receive_member;
845 const char *receive_error;
846 const char *receive_service;
847 const char *receive_path;
848 const char *receive_type;
854 if (!locate_attributes (parser, element_name,
858 "send_interface", &send_interface,
859 "send_member", &send_member,
860 "send_error", &send_error,
861 "send_service", &send_service,
862 "send_path", &send_path,
863 "send_type", &send_type,
864 "receive_interface", &receive_interface,
865 "receive_member", &receive_member,
866 "receive_error", &receive_error,
867 "receive_service", &receive_service,
868 "receive_path", &receive_path,
869 "receive_type", &receive_type,
876 if (!(send_interface || send_member || send_error || send_service ||
877 send_type || send_path ||
878 receive_interface || receive_member || receive_error || receive_service ||
879 receive_type || receive_path ||
880 own || user || group))
882 dbus_set_error (error, DBUS_ERROR_FAILED,
883 "Element <%s> must have one or more attributes",
888 if ((send_member && (send_interface == NULL && send_path == NULL)) ||
889 (receive_member && (receive_interface == NULL && receive_path == NULL)))
891 dbus_set_error (error, DBUS_ERROR_FAILED,
892 "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.",
897 /* Allowed combinations of elements are:
899 * base, must be all send or all receive:
905 * base send_ can combine with send_service, send_path, send_type
906 * base receive_ with receive_service, receive_path, receive_type
908 * user, group, own must occur alone
911 if (((send_interface && send_error) ||
912 (send_interface && receive_interface) ||
913 (send_interface && receive_member) ||
914 (send_interface && receive_error) ||
915 (send_interface && receive_service) ||
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_service) ||
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_service) ||
933 (send_error && own) ||
934 (send_error && user) ||
935 (send_error && group)) ||
937 ((send_service && receive_interface) ||
938 (send_service && receive_member) ||
939 (send_service && receive_error) ||
940 (send_service && receive_service) ||
941 (send_service && own) ||
942 (send_service && user) ||
943 (send_service && group)) ||
945 ((send_type && receive_interface) ||
946 (send_type && receive_member) ||
947 (send_type && receive_error) ||
948 (send_type && receive_service) ||
949 (send_type && own) ||
950 (send_type && user) ||
951 (send_type && group)) ||
953 ((send_path && receive_interface) ||
954 (send_path && receive_member) ||
955 (send_path && receive_error) ||
956 (send_path && receive_service) ||
957 (send_path && own) ||
958 (send_path && user) ||
959 (send_path && group)) ||
961 ((receive_interface && receive_error) ||
962 (receive_interface && own) ||
963 (receive_interface && user) ||
964 (receive_interface && group)) ||
966 ((receive_member && receive_error) ||
967 (receive_member && own) ||
968 (receive_member && user) ||
969 (receive_member && group)) ||
971 ((receive_error && own) ||
972 (receive_error && user) ||
973 (receive_error && group)) ||
980 dbus_set_error (error, DBUS_ERROR_FAILED,
981 "Invalid combination of attributes on element <%s>, "
982 "only send_foo/send_service or receive_foo/receive_service may be paired",
989 /* In BusPolicyRule, NULL represents wildcard.
990 * In the config file, '*' represents it.
992 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
994 if (send_interface || send_member || send_error || send_service ||
995 send_path || send_type)
999 if (IS_WILDCARD (send_interface))
1000 send_interface = NULL;
1001 if (IS_WILDCARD (send_member))
1003 if (IS_WILDCARD (send_error))
1005 if (IS_WILDCARD (send_service))
1006 send_service = NULL;
1007 if (IS_WILDCARD (send_path))
1009 if (IS_WILDCARD (send_type))
1012 message_type = DBUS_MESSAGE_TYPE_INVALID;
1013 if (send_type != NULL)
1015 message_type = message_type_from_string (send_type);
1016 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1018 dbus_set_error (error, DBUS_ERROR_FAILED,
1019 "Bad message type \"%s\"",
1025 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
1029 rule->d.send.message_type = message_type;
1030 rule->d.send.path = _dbus_strdup (send_path);
1031 rule->d.send.interface = _dbus_strdup (send_interface);
1032 rule->d.send.member = _dbus_strdup (send_member);
1033 rule->d.send.error = _dbus_strdup (send_error);
1034 rule->d.send.destination = _dbus_strdup (send_service);
1035 if (send_path && rule->d.send.path == NULL)
1037 if (send_interface && rule->d.send.interface == NULL)
1039 if (send_member && rule->d.send.member == NULL)
1041 if (send_error && rule->d.send.error == NULL)
1043 if (send_service && rule->d.send.destination == NULL)
1046 else if (receive_interface || receive_member || receive_error || receive_service ||
1047 receive_path || receive_type)
1051 if (IS_WILDCARD (receive_interface))
1052 receive_interface = NULL;
1053 if (IS_WILDCARD (receive_member))
1054 receive_member = NULL;
1055 if (IS_WILDCARD (receive_error))
1056 receive_error = NULL;
1057 if (IS_WILDCARD (receive_service))
1058 receive_service = NULL;
1059 if (IS_WILDCARD (receive_path))
1060 receive_path = NULL;
1061 if (IS_WILDCARD (receive_type))
1062 receive_type = NULL;
1065 message_type = DBUS_MESSAGE_TYPE_INVALID;
1066 if (receive_type != NULL)
1068 message_type = message_type_from_string (receive_type);
1069 if (message_type == DBUS_MESSAGE_TYPE_INVALID)
1071 dbus_set_error (error, DBUS_ERROR_FAILED,
1072 "Bad message type \"%s\"",
1078 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
1082 rule->d.receive.message_type = message_type;
1083 rule->d.receive.path = _dbus_strdup (receive_path);
1084 rule->d.receive.interface = _dbus_strdup (receive_interface);
1085 rule->d.receive.member = _dbus_strdup (receive_member);
1086 rule->d.receive.error = _dbus_strdup (receive_error);
1087 rule->d.receive.origin = _dbus_strdup (receive_service);
1088 if (receive_path && rule->d.receive.path == NULL)
1090 if (receive_interface && rule->d.receive.interface == NULL)
1092 if (receive_member && rule->d.receive.member == NULL)
1094 if (receive_error && rule->d.receive.error == NULL)
1096 if (receive_service && rule->d.receive.origin == NULL)
1101 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1105 if (IS_WILDCARD (own))
1108 rule->d.own.service_name = _dbus_strdup (own);
1109 if (own && rule->d.own.service_name == NULL)
1114 if (IS_WILDCARD (user))
1116 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1120 rule->d.user.uid = DBUS_UID_UNSET;
1124 DBusString username;
1127 _dbus_string_init_const (&username, user);
1129 if (_dbus_get_user_id (&username, &uid))
1131 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1135 rule->d.user.uid = uid;
1139 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1140 user, element_name);
1146 if (IS_WILDCARD (group))
1148 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1152 rule->d.group.gid = DBUS_GID_UNSET;
1156 DBusString groupname;
1159 _dbus_string_init_const (&groupname, group);
1161 if (_dbus_get_user_id (&groupname, &gid))
1163 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1167 rule->d.group.gid = gid;
1171 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1172 group, element_name);
1177 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1183 pe = peek_element (parser);
1184 _dbus_assert (pe != NULL);
1185 _dbus_assert (pe->type == ELEMENT_POLICY);
1187 switch (pe->d.policy.type)
1189 case POLICY_IGNORED:
1190 /* drop the rule on the floor */
1193 case POLICY_DEFAULT:
1194 if (!bus_policy_append_default_rule (parser->policy, rule))
1197 case POLICY_MANDATORY:
1198 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1202 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1204 dbus_set_error (error, DBUS_ERROR_FAILED,
1205 "<%s> rule cannot be per-user because it has bus-global semantics",
1210 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
1215 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1217 dbus_set_error (error, DBUS_ERROR_FAILED,
1218 "<%s> rule cannot be per-group because it has bus-global semantics",
1223 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
1229 bus_policy_rule_unref (rule);
1236 BUS_SET_OOM (error);
1239 bus_policy_rule_unref (rule);
1244 start_policy_child (BusConfigParser *parser,
1245 const char *element_name,
1246 const char **attribute_names,
1247 const char **attribute_values,
1250 if (strcmp (element_name, "allow") == 0)
1252 if (!append_rule_from_element (parser, element_name,
1253 attribute_names, attribute_values,
1257 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1259 BUS_SET_OOM (error);
1265 else if (strcmp (element_name, "deny") == 0)
1267 if (!append_rule_from_element (parser, element_name,
1268 attribute_names, attribute_values,
1272 if (push_element (parser, ELEMENT_DENY) == NULL)
1274 BUS_SET_OOM (error);
1282 dbus_set_error (error, DBUS_ERROR_FAILED,
1283 "Element <%s> not allowed inside <%s> in configuration file",
1284 element_name, "policy");
1290 bus_config_parser_start_element (BusConfigParser *parser,
1291 const char *element_name,
1292 const char **attribute_names,
1293 const char **attribute_values,
1298 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1300 /* printf ("START: %s\n", element_name); */
1302 t = top_element_type (parser);
1304 if (t == ELEMENT_NONE)
1306 if (strcmp (element_name, "busconfig") == 0)
1308 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1311 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1313 BUS_SET_OOM (error);
1321 dbus_set_error (error, DBUS_ERROR_FAILED,
1322 "Unknown element <%s> at root of configuration file",
1327 else if (t == ELEMENT_BUSCONFIG)
1329 return start_busconfig_child (parser, element_name,
1330 attribute_names, attribute_values,
1333 else if (t == ELEMENT_POLICY)
1335 return start_policy_child (parser, element_name,
1336 attribute_names, attribute_values,
1341 dbus_set_error (error, DBUS_ERROR_FAILED,
1342 "Element <%s> is not allowed in this context",
1349 set_limit (BusConfigParser *parser,
1354 dbus_bool_t must_be_positive;
1355 dbus_bool_t must_be_int;
1357 must_be_int = FALSE;
1358 must_be_positive = FALSE;
1360 if (strcmp (name, "max_incoming_bytes") == 0)
1362 must_be_positive = TRUE;
1363 parser->limits.max_incoming_bytes = value;
1365 else if (strcmp (name, "max_outgoing_bytes") == 0)
1367 must_be_positive = TRUE;
1368 parser->limits.max_outgoing_bytes = value;
1370 else if (strcmp (name, "max_message_size") == 0)
1372 must_be_positive = TRUE;
1373 parser->limits.max_message_size = value;
1375 else if (strcmp (name, "activation_timeout") == 0)
1377 must_be_positive = TRUE;
1379 parser->limits.activation_timeout = value;
1381 else if (strcmp (name, "auth_timeout") == 0)
1383 must_be_positive = TRUE;
1385 parser->limits.auth_timeout = value;
1387 else if (strcmp (name, "max_completed_connections") == 0)
1389 must_be_positive = TRUE;
1391 parser->limits.max_completed_connections = value;
1393 else if (strcmp (name, "max_incomplete_connections") == 0)
1395 must_be_positive = TRUE;
1397 parser->limits.max_incomplete_connections = value;
1399 else if (strcmp (name, "max_connections_per_user") == 0)
1401 must_be_positive = TRUE;
1403 parser->limits.max_connections_per_user = value;
1405 else if (strcmp (name, "max_pending_activations") == 0)
1407 must_be_positive = TRUE;
1409 parser->limits.max_pending_activations = value;
1411 else if (strcmp (name, "max_services_per_connection") == 0)
1413 must_be_positive = TRUE;
1415 parser->limits.max_services_per_connection = value;
1419 dbus_set_error (error, DBUS_ERROR_FAILED,
1420 "There is no limit called \"%s\"\n",
1425 if (must_be_positive && value < 0)
1427 dbus_set_error (error, DBUS_ERROR_FAILED,
1428 "<limit name=\"%s\"> must be a positive number\n",
1434 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1436 dbus_set_error (error, DBUS_ERROR_FAILED,
1437 "<limit name=\"%s\"> value is too large\n",
1446 bus_config_parser_end_element (BusConfigParser *parser,
1447 const char *element_name,
1454 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1456 /* printf ("END: %s\n", element_name); */
1458 t = top_element_type (parser);
1460 if (t == ELEMENT_NONE)
1462 /* should probably be an assertion failure but
1463 * being paranoid about XML parsers
1465 dbus_set_error (error, DBUS_ERROR_FAILED,
1466 "XML parser ended element with no element on the stack");
1470 n = element_type_to_name (t);
1471 _dbus_assert (n != NULL);
1472 if (strcmp (n, element_name) != 0)
1474 /* should probably be an assertion failure but
1475 * being paranoid about XML parsers
1477 dbus_set_error (error, DBUS_ERROR_FAILED,
1478 "XML element <%s> ended but topmost element on the stack was <%s>",
1483 e = peek_element (parser);
1484 _dbus_assert (e != NULL);
1489 _dbus_assert_not_reached ("element in stack has no type");
1492 case ELEMENT_INCLUDE:
1495 case ELEMENT_LISTEN:
1496 case ELEMENT_PIDFILE:
1498 case ELEMENT_SERVICEDIR:
1499 case ELEMENT_INCLUDEDIR:
1501 if (!e->had_content)
1503 dbus_set_error (error, DBUS_ERROR_FAILED,
1504 "XML element <%s> was expected to have content inside it",
1505 element_type_to_name (e->type));
1509 if (e->type == ELEMENT_LIMIT)
1511 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1517 case ELEMENT_BUSCONFIG:
1518 case ELEMENT_POLICY:
1525 pop_element (parser);
1531 all_whitespace (const DBusString *str)
1535 _dbus_string_skip_white (str, 0, &i);
1537 return i == _dbus_string_get_length (str);
1541 make_full_path (const DBusString *basedir,
1542 const DBusString *filename,
1543 DBusString *full_path)
1545 if (_dbus_path_is_absolute (filename))
1547 return _dbus_string_copy (filename, 0, full_path, 0);
1551 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1554 if (!_dbus_concat_dir_and_file (full_path, filename))
1562 include_file (BusConfigParser *parser,
1563 const DBusString *filename,
1564 dbus_bool_t ignore_missing,
1567 /* FIXME good test case for this would load each config file in the
1568 * test suite both alone, and as an include, and check
1569 * that the result is the same
1571 BusConfigParser *included;
1572 DBusError tmp_error;
1574 dbus_error_init (&tmp_error);
1575 included = bus_config_load (filename, FALSE, &tmp_error);
1576 if (included == NULL)
1578 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1580 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1583 dbus_error_free (&tmp_error);
1588 dbus_move_error (&tmp_error, error);
1594 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1596 if (!merge_included (parser, included, error))
1598 bus_config_parser_unref (included);
1602 bus_config_parser_unref (included);
1608 include_dir (BusConfigParser *parser,
1609 const DBusString *dirname,
1612 DBusString filename;
1614 DBusError tmp_error;
1617 if (!_dbus_string_init (&filename))
1619 BUS_SET_OOM (error);
1625 dir = _dbus_directory_open (dirname, error);
1630 dbus_error_init (&tmp_error);
1631 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1633 DBusString full_path;
1635 if (!_dbus_string_init (&full_path))
1637 BUS_SET_OOM (error);
1641 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1643 BUS_SET_OOM (error);
1644 _dbus_string_free (&full_path);
1648 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1650 BUS_SET_OOM (error);
1651 _dbus_string_free (&full_path);
1655 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1657 if (!include_file (parser, &full_path, TRUE, error))
1659 _dbus_string_free (&full_path);
1664 _dbus_string_free (&full_path);
1667 if (dbus_error_is_set (&tmp_error))
1669 dbus_move_error (&tmp_error, error);
1676 _dbus_string_free (&filename);
1679 _dbus_directory_close (dir);
1685 bus_config_parser_content (BusConfigParser *parser,
1686 const DBusString *content,
1691 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1697 _dbus_string_get_const_data (content, &c_str);
1699 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1703 e = peek_element (parser);
1706 dbus_set_error (error, DBUS_ERROR_FAILED,
1707 "Text content outside of any XML element in configuration file");
1710 else if (e->had_content)
1712 _dbus_assert_not_reached ("Element had multiple content blocks");
1716 switch (top_element_type (parser))
1719 _dbus_assert_not_reached ("element at top of stack has no type");
1722 case ELEMENT_BUSCONFIG:
1723 case ELEMENT_POLICY:
1727 if (all_whitespace (content))
1731 dbus_set_error (error, DBUS_ERROR_FAILED,
1732 "No text content expected inside XML element %s in configuration file",
1733 element_type_to_name (top_element_type (parser)));
1737 case ELEMENT_PIDFILE:
1741 e->had_content = TRUE;
1743 if (!_dbus_string_copy_data (content, &s))
1746 dbus_free (parser->pidfile);
1747 parser->pidfile = s;
1751 case ELEMENT_INCLUDE:
1753 DBusString full_path;
1755 e->had_content = TRUE;
1757 if (!_dbus_string_init (&full_path))
1760 if (!make_full_path (&parser->basedir, content, &full_path))
1762 _dbus_string_free (&full_path);
1766 if (!include_file (parser, &full_path,
1767 e->d.include.ignore_missing, error))
1769 _dbus_string_free (&full_path);
1773 _dbus_string_free (&full_path);
1777 case ELEMENT_INCLUDEDIR:
1779 DBusString full_path;
1781 e->had_content = TRUE;
1783 if (!_dbus_string_init (&full_path))
1786 if (!make_full_path (&parser->basedir, content, &full_path))
1788 _dbus_string_free (&full_path);
1792 if (!include_dir (parser, &full_path, error))
1794 _dbus_string_free (&full_path);
1798 _dbus_string_free (&full_path);
1806 e->had_content = TRUE;
1808 if (!_dbus_string_copy_data (content, &s))
1811 dbus_free (parser->user);
1820 e->had_content = TRUE;
1822 if (!_dbus_string_copy_data (content, &s))
1825 dbus_free (parser->bus_type);
1826 parser->bus_type = s;
1830 case ELEMENT_LISTEN:
1834 e->had_content = TRUE;
1836 if (!_dbus_string_copy_data (content, &s))
1839 if (!_dbus_list_append (&parser->listen_on,
1852 e->had_content = TRUE;
1854 if (!_dbus_string_copy_data (content, &s))
1857 if (!_dbus_list_append (&parser->mechanisms,
1866 case ELEMENT_SERVICEDIR:
1869 DBusString full_path;
1871 e->had_content = TRUE;
1873 if (!_dbus_string_init (&full_path))
1876 if (!make_full_path (&parser->basedir, content, &full_path))
1878 _dbus_string_free (&full_path);
1882 if (!_dbus_string_copy_data (&full_path, &s))
1884 _dbus_string_free (&full_path);
1888 if (!_dbus_list_append (&parser->service_dirs, s))
1890 _dbus_string_free (&full_path);
1895 _dbus_string_free (&full_path);
1903 e->had_content = TRUE;
1906 if (!_dbus_string_parse_int (content, 0, &val, NULL))
1908 dbus_set_error (error, DBUS_ERROR_FAILED,
1909 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
1914 e->d.limit.value = val;
1916 _dbus_verbose ("Loaded value %ld for limit %s\n",
1923 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1927 BUS_SET_OOM (error);
1932 bus_config_parser_finished (BusConfigParser *parser,
1935 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1937 if (parser->stack != NULL)
1939 dbus_set_error (error, DBUS_ERROR_FAILED,
1940 "Element <%s> was not closed in configuration file",
1941 element_type_to_name (top_element_type (parser)));
1946 if (parser->is_toplevel && parser->listen_on == NULL)
1948 dbus_set_error (error, DBUS_ERROR_FAILED,
1949 "Configuration file needs one or more <listen> elements giving addresses");
1957 bus_config_parser_get_user (BusConfigParser *parser)
1959 return parser->user;
1963 bus_config_parser_get_type (BusConfigParser *parser)
1965 return parser->bus_type;
1969 bus_config_parser_get_addresses (BusConfigParser *parser)
1971 return &parser->listen_on;
1975 bus_config_parser_get_mechanisms (BusConfigParser *parser)
1977 return &parser->mechanisms;
1981 bus_config_parser_get_service_dirs (BusConfigParser *parser)
1983 return &parser->service_dirs;
1987 bus_config_parser_get_fork (BusConfigParser *parser)
1989 return parser->fork;
1993 bus_config_parser_get_pidfile (BusConfigParser *parser)
1995 return parser->pidfile;
1999 bus_config_parser_steal_policy (BusConfigParser *parser)
2003 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
2005 policy = parser->policy;
2007 parser->policy = NULL;
2012 /* Overwrite any limits that were set in the configuration file */
2014 bus_config_parser_get_limits (BusConfigParser *parser,
2017 *limits = parser->limits;
2020 #ifdef DBUS_BUILD_TESTS
2031 do_load (const DBusString *full_path,
2033 dbus_bool_t oom_possible)
2035 BusConfigParser *parser;
2038 dbus_error_init (&error);
2040 parser = bus_config_load (full_path, TRUE, &error);
2043 _DBUS_ASSERT_ERROR_IS_SET (&error);
2046 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2048 _dbus_verbose ("Failed to load valid file due to OOM\n");
2049 dbus_error_free (&error);
2052 else if (validity == VALID)
2054 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
2057 dbus_error_free (&error);
2062 dbus_error_free (&error);
2068 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
2070 bus_config_parser_unref (parser);
2072 if (validity == INVALID)
2074 _dbus_warn ("Accepted invalid file\n");
2084 const DBusString *full_path;
2089 check_loader_oom_func (void *data)
2091 LoaderOomData *d = data;
2093 return do_load (d->full_path, d->validity, TRUE);
2097 process_test_subdir (const DBusString *test_base_dir,
2101 DBusString test_directory;
2102 DBusString filename;
2110 if (!_dbus_string_init (&test_directory))
2111 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2113 _dbus_string_init_const (&filename, subdir);
2115 if (!_dbus_string_copy (test_base_dir, 0,
2116 &test_directory, 0))
2117 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2119 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2120 _dbus_assert_not_reached ("couldn't allocate full path");
2122 _dbus_string_free (&filename);
2123 if (!_dbus_string_init (&filename))
2124 _dbus_assert_not_reached ("didn't allocate filename string\n");
2126 dbus_error_init (&error);
2127 dir = _dbus_directory_open (&test_directory, &error);
2130 _dbus_warn ("Could not open %s: %s\n",
2131 _dbus_string_get_const_data (&test_directory),
2133 dbus_error_free (&error);
2137 printf ("Testing:\n");
2140 while (_dbus_directory_get_next_file (dir, &filename, &error))
2142 DBusString full_path;
2145 if (!_dbus_string_init (&full_path))
2146 _dbus_assert_not_reached ("couldn't init string");
2148 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2149 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2151 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2152 _dbus_assert_not_reached ("couldn't concat file to dir");
2154 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2156 _dbus_verbose ("Skipping non-.conf file %s\n",
2157 _dbus_string_get_const_data (&filename));
2158 _dbus_string_free (&full_path);
2162 printf (" %s\n", _dbus_string_get_const_data (&filename));
2164 _dbus_verbose (" expecting %s\n",
2165 validity == VALID ? "valid" :
2166 (validity == INVALID ? "invalid" :
2167 (validity == UNKNOWN ? "unknown" : "???")));
2169 d.full_path = &full_path;
2170 d.validity = validity;
2171 if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d))
2172 _dbus_assert_not_reached ("test failed");
2174 _dbus_string_free (&full_path);
2177 if (dbus_error_is_set (&error))
2179 _dbus_warn ("Could not get next file in %s: %s\n",
2180 _dbus_string_get_const_data (&test_directory),
2182 dbus_error_free (&error);
2191 _dbus_directory_close (dir);
2192 _dbus_string_free (&test_directory);
2193 _dbus_string_free (&filename);
2199 bus_config_parser_test (const DBusString *test_data_dir)
2201 if (test_data_dir == NULL ||
2202 _dbus_string_get_length (test_data_dir) == 0)
2204 printf ("No test data\n");
2208 if (!process_test_subdir (test_data_dir, "valid-config-files", VALID))
2214 #endif /* DBUS_BUILD_TESTS */