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;
91 struct BusConfigParser
95 DBusString basedir; /**< Directory we resolve paths relative to */
97 DBusList *stack; /**< stack of Element */
99 char *user; /**< user to run as */
101 char *bus_type; /**< Message bus type */
103 DBusList *listen_on; /**< List of addresses to listen to */
105 DBusList *mechanisms; /**< Auth mechanisms */
107 DBusList *service_dirs; /**< Directories to look for services in */
109 BusPolicy *policy; /**< Security policy */
111 BusLimits limits; /**< Limits */
113 char *pidfile; /**< PID file */
115 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
117 unsigned int is_toplevel : 1; /**< FALSE if we are a sub-config-file inside another one */
121 element_type_to_name (ElementType type)
127 case ELEMENT_BUSCONFIG:
129 case ELEMENT_INCLUDE:
147 case ELEMENT_PIDFILE:
149 case ELEMENT_SERVICEDIR:
151 case ELEMENT_INCLUDEDIR:
157 _dbus_assert_not_reached ("bad element type");
163 push_element (BusConfigParser *parser,
168 _dbus_assert (type != ELEMENT_NONE);
170 e = dbus_new0 (Element, 1);
174 if (!_dbus_list_append (&parser->stack, e))
186 element_free (Element *e)
188 if (e->type == ELEMENT_LIMIT)
189 dbus_free (e->d.limit.name);
195 pop_element (BusConfigParser *parser)
199 e = _dbus_list_pop_last (&parser->stack);
205 peek_element (BusConfigParser *parser)
209 e = _dbus_list_get_last (&parser->stack);
215 top_element_type (BusConfigParser *parser)
219 e = _dbus_list_get_last (&parser->stack);
228 merge_included (BusConfigParser *parser,
229 BusConfigParser *included,
234 if (!bus_policy_merge (parser->policy,
241 if (included->user != NULL)
243 dbus_free (parser->user);
244 parser->user = included->user;
245 included->user = NULL;
248 if (included->bus_type != NULL)
250 dbus_free (parser->bus_type);
251 parser->bus_type = included->bus_type;
252 included->bus_type = NULL;
258 if (included->pidfile != NULL)
260 dbus_free (parser->pidfile);
261 parser->pidfile = included->pidfile;
262 included->pidfile = NULL;
265 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
266 _dbus_list_append_link (&parser->listen_on, link);
268 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
269 _dbus_list_append_link (&parser->mechanisms, link);
271 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
272 _dbus_list_append_link (&parser->service_dirs, link);
278 bus_config_parser_new (const DBusString *basedir,
279 dbus_bool_t is_toplevel)
281 BusConfigParser *parser;
283 parser = dbus_new0 (BusConfigParser, 1);
287 parser->is_toplevel = !!is_toplevel;
289 if (!_dbus_string_init (&parser->basedir))
295 if (((parser->policy = bus_policy_new ()) == NULL) ||
296 !_dbus_string_copy (basedir, 0, &parser->basedir, 0))
299 bus_policy_unref (parser->policy);
301 _dbus_string_free (&parser->basedir);
306 /* Make up some numbers! woot! */
307 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
308 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
309 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
311 /* Making this long means the user has to wait longer for an error
312 * message if something screws up, but making it too short means
313 * they might see a false failure.
315 parser->limits.activation_timeout = 25000; /* 25 seconds */
317 /* Making this long risks making a DOS attack easier, but too short
318 * and legitimate auth will fail. If interactive auth (ask user for
319 * password) is allowed, then potentially it has to be quite long.
321 parser->limits.auth_timeout = 30000; /* 30 seconds */
323 parser->limits.max_incomplete_connections = 32;
324 parser->limits.max_connections_per_user = 128;
326 /* Note that max_completed_connections / max_connections_per_user
327 * is the number of users that would have to work together to
328 * DOS all the other users.
330 parser->limits.max_completed_connections = 1024;
332 parser->limits.max_pending_activations = 256;
333 parser->limits.max_services_per_connection = 256;
335 parser->refcount = 1;
341 bus_config_parser_ref (BusConfigParser *parser)
343 _dbus_assert (parser->refcount > 0);
345 parser->refcount += 1;
349 bus_config_parser_unref (BusConfigParser *parser)
351 _dbus_assert (parser->refcount > 0);
353 parser->refcount -= 1;
355 if (parser->refcount == 0)
357 while (parser->stack != NULL)
358 pop_element (parser);
360 dbus_free (parser->user);
361 dbus_free (parser->bus_type);
362 dbus_free (parser->pidfile);
364 _dbus_list_foreach (&parser->listen_on,
365 (DBusForeachFunction) dbus_free,
368 _dbus_list_clear (&parser->listen_on);
370 _dbus_list_foreach (&parser->service_dirs,
371 (DBusForeachFunction) dbus_free,
374 _dbus_list_clear (&parser->service_dirs);
376 _dbus_list_foreach (&parser->mechanisms,
377 (DBusForeachFunction) dbus_free,
380 _dbus_list_clear (&parser->mechanisms);
382 _dbus_string_free (&parser->basedir);
385 bus_policy_unref (parser->policy);
392 bus_config_parser_check_doctype (BusConfigParser *parser,
396 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
398 if (strcmp (doctype, "busconfig") != 0)
400 dbus_set_error (error,
402 "Configuration file has the wrong document type %s",
417 locate_attributes (BusConfigParser *parser,
418 const char *element_name,
419 const char **attribute_names,
420 const char **attribute_values,
422 const char *first_attribute_name,
423 const char **first_attribute_retloc,
431 LocateAttr attrs[MAX_ATTRS];
435 _dbus_assert (first_attribute_name != NULL);
436 _dbus_assert (first_attribute_retloc != NULL);
441 attrs[0].name = first_attribute_name;
442 attrs[0].retloc = first_attribute_retloc;
443 *first_attribute_retloc = NULL;
445 va_start (args, first_attribute_retloc);
447 name = va_arg (args, const char*);
448 retloc = va_arg (args, const char**);
452 _dbus_assert (retloc != NULL);
453 _dbus_assert (n_attrs < MAX_ATTRS);
455 attrs[n_attrs].name = name;
456 attrs[n_attrs].retloc = retloc;
460 name = va_arg (args, const char*);
461 retloc = va_arg (args, const char**);
470 while (attribute_names[i])
479 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
481 retloc = attrs[j].retloc;
485 dbus_set_error (error, DBUS_ERROR_FAILED,
486 "Attribute \"%s\" repeated twice on the same <%s> element",
487 attrs[j].name, element_name);
492 *retloc = attribute_values[i];
501 dbus_set_error (error, DBUS_ERROR_FAILED,
502 "Attribute \"%s\" is invalid on <%s> element in this context",
503 attribute_names[i], element_name);
516 check_no_attributes (BusConfigParser *parser,
517 const char *element_name,
518 const char **attribute_names,
519 const char **attribute_values,
522 if (attribute_names[0] != NULL)
524 dbus_set_error (error, DBUS_ERROR_FAILED,
525 "Attribute \"%s\" is invalid on <%s> element in this context",
526 attribute_names[0], element_name);
534 start_busconfig_child (BusConfigParser *parser,
535 const char *element_name,
536 const char **attribute_names,
537 const char **attribute_values,
540 if (strcmp (element_name, "user") == 0)
542 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
545 if (push_element (parser, ELEMENT_USER) == NULL)
553 else if (strcmp (element_name, "type") == 0)
555 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
558 if (push_element (parser, ELEMENT_TYPE) == NULL)
566 else if (strcmp (element_name, "fork") == 0)
568 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
571 if (push_element (parser, ELEMENT_FORK) == NULL)
581 else if (strcmp (element_name, "pidfile") == 0)
583 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
586 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
594 else if (strcmp (element_name, "listen") == 0)
596 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
599 if (push_element (parser, ELEMENT_LISTEN) == NULL)
607 else if (strcmp (element_name, "auth") == 0)
609 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
612 if (push_element (parser, ELEMENT_AUTH) == NULL)
620 else if (strcmp (element_name, "includedir") == 0)
622 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
625 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
633 else if (strcmp (element_name, "servicedir") == 0)
635 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
638 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
646 else if (strcmp (element_name, "include") == 0)
649 const char *ignore_missing;
651 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
657 e->d.include.ignore_missing = FALSE;
659 if (!locate_attributes (parser, "include",
663 "ignore_missing", &ignore_missing,
667 if (ignore_missing != NULL)
669 if (strcmp (ignore_missing, "yes") == 0)
670 e->d.include.ignore_missing = TRUE;
671 else if (strcmp (ignore_missing, "no") == 0)
672 e->d.include.ignore_missing = FALSE;
675 dbus_set_error (error, DBUS_ERROR_FAILED,
676 "ignore_missing attribute must have value \"yes\" or \"no\"");
683 else if (strcmp (element_name, "policy") == 0)
690 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
696 e->d.policy.type = POLICY_IGNORED;
698 if (!locate_attributes (parser, "policy",
708 if (((context && user) ||
709 (context && group)) ||
711 !(context || user || group))
713 dbus_set_error (error, DBUS_ERROR_FAILED,
714 "<policy> element must have exactly one of (context|user|group) attributes");
720 if (strcmp (context, "default") == 0)
722 e->d.policy.type = POLICY_DEFAULT;
724 else if (strcmp (context, "mandatory") == 0)
726 e->d.policy.type = POLICY_MANDATORY;
730 dbus_set_error (error, DBUS_ERROR_FAILED,
731 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
736 else if (user != NULL)
739 _dbus_string_init_const (&username, user);
741 if (_dbus_get_user_id (&username,
742 &e->d.policy.gid_or_uid))
743 e->d.policy.type = POLICY_USER;
745 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
748 else if (group != NULL)
750 DBusString group_name;
751 _dbus_string_init_const (&group_name, group);
753 if (_dbus_get_group_id (&group_name,
754 &e->d.policy.gid_or_uid))
755 e->d.policy.type = POLICY_GROUP;
757 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
762 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
767 else if (strcmp (element_name, "limit") == 0)
772 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
778 if (!locate_attributes (parser, "limit",
788 dbus_set_error (error, DBUS_ERROR_FAILED,
789 "<limit> element must have a \"name\" attribute");
793 e->d.limit.name = _dbus_strdup (name);
794 if (e->d.limit.name == NULL)
804 dbus_set_error (error, DBUS_ERROR_FAILED,
805 "Element <%s> not allowed inside <%s> in configuration file",
806 element_name, "busconfig");
812 append_rule_from_element (BusConfigParser *parser,
813 const char *element_name,
814 const char **attribute_names,
815 const char **attribute_values,
819 const char *send_interface;
820 const char *send_member;
821 const char *send_error;
822 const char *send_service;
823 const char *receive_interface;
824 const char *receive_member;
825 const char *receive_error;
826 const char *receive_service;
832 if (!locate_attributes (parser, element_name,
836 "send_interface", &send_interface,
837 "send_member", &send_member,
838 "send_error", &send_error,
839 "send_service", &send_service,
840 "receive_interface", &receive_interface,
841 "receive_member", &receive_member,
842 "receive_error", &receive_error,
843 "receive_service", &receive_service,
850 if (!(send_interface || send_member || send_error || send_service ||
851 receive_interface || receive_member || receive_error || receive_service ||
852 own || user || group))
854 dbus_set_error (error, DBUS_ERROR_FAILED,
855 "Element <%s> must have one or more attributes",
860 if ((send_member && send_interface == NULL) ||
861 (receive_member && receive_interface == NULL))
863 dbus_set_error (error, DBUS_ERROR_FAILED,
864 "On element <%s>, if you specify a member you must specify an interface",
869 /* Allowed combinations of elements are:
871 * base, must be all send or all receive:
876 * base send_ can combine with send_service,
877 * base receive_ with receive_service
879 * user, group, own must occur alone
882 if (((send_interface && send_error) ||
883 (send_interface && receive_interface) ||
884 (send_interface && receive_member) ||
885 (send_interface && receive_error) ||
886 (send_interface && receive_service) ||
887 (send_interface && own) ||
888 (send_interface && user) ||
889 (send_interface && group)) ||
891 ((send_member && send_error) ||
892 (send_member && receive_interface) ||
893 (send_member && receive_member) ||
894 (send_member && receive_error) ||
895 (send_member && receive_service) ||
896 (send_member && own) ||
897 (send_member && user) ||
898 (send_member && group)) ||
900 ((send_error && receive_interface) ||
901 (send_error && receive_member) ||
902 (send_error && receive_error) ||
903 (send_error && receive_service) ||
904 (send_error && own) ||
905 (send_error && user) ||
906 (send_error && group)) ||
908 ((send_service && receive_interface) ||
909 (send_service && receive_member) ||
910 (send_service && receive_error) ||
911 (send_service && receive_service) ||
912 (send_service && own) ||
913 (send_service && user) ||
914 (send_service && group)) ||
916 ((receive_interface && receive_error) ||
917 (receive_interface && own) ||
918 (receive_interface && user) ||
919 (receive_interface && group)) ||
921 ((receive_member && receive_error) ||
922 (receive_member && own) ||
923 (receive_member && user) ||
924 (receive_member && group)) ||
926 ((receive_error && own) ||
927 (receive_error && user) ||
928 (receive_error && group)) ||
935 dbus_set_error (error, DBUS_ERROR_FAILED,
936 "Invalid combination of attributes on element <%s>, "
937 "only send_foo/send_service or receive_foo/receive_service may be paired",
944 /* In BusPolicyRule, NULL represents wildcard.
945 * In the config file, '*' represents it.
947 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
949 if (send_interface || send_member || send_error || send_service)
951 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
955 if (IS_WILDCARD (send_interface))
956 send_interface = NULL;
957 if (IS_WILDCARD (send_member))
959 if (IS_WILDCARD (send_error))
961 if (IS_WILDCARD (send_service))
964 rule->d.send.interface = _dbus_strdup (send_interface);
965 rule->d.send.member = _dbus_strdup (send_member);
966 rule->d.send.error = _dbus_strdup (send_error);
967 rule->d.send.destination = _dbus_strdup (send_service);
968 if (send_interface && rule->d.send.interface == NULL)
970 if (send_member && rule->d.send.member == NULL)
972 if (send_error && rule->d.send.error == NULL)
974 if (send_service && rule->d.send.destination == NULL)
977 else if (receive_interface || receive_member || receive_error || receive_service)
979 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
983 if (IS_WILDCARD (receive_interface))
984 receive_interface = NULL;
985 if (IS_WILDCARD (receive_member))
986 receive_member = NULL;
987 if (IS_WILDCARD (receive_error))
988 receive_error = NULL;
989 if (IS_WILDCARD (receive_service))
990 receive_service = NULL;
992 rule->d.receive.interface = _dbus_strdup (receive_interface);
993 rule->d.receive.member = _dbus_strdup (receive_member);
994 rule->d.receive.error = _dbus_strdup (receive_error);
995 rule->d.receive.origin = _dbus_strdup (receive_service);
996 if (receive_interface && rule->d.receive.interface == NULL)
998 if (receive_member && rule->d.receive.member == NULL)
1000 if (receive_error && rule->d.receive.error == NULL)
1002 if (receive_service && rule->d.receive.origin == NULL)
1007 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
1011 if (IS_WILDCARD (own))
1014 rule->d.own.service_name = _dbus_strdup (own);
1015 if (own && rule->d.own.service_name == NULL)
1020 if (IS_WILDCARD (user))
1022 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1026 rule->d.user.uid = DBUS_UID_UNSET;
1030 DBusString username;
1033 _dbus_string_init_const (&username, user);
1035 if (_dbus_get_user_id (&username, &uid))
1037 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
1041 rule->d.user.uid = uid;
1045 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
1046 user, element_name);
1052 if (IS_WILDCARD (group))
1054 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1058 rule->d.group.gid = DBUS_GID_UNSET;
1062 DBusString groupname;
1065 _dbus_string_init_const (&groupname, group);
1067 if (_dbus_get_user_id (&groupname, &gid))
1069 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
1073 rule->d.group.gid = gid;
1077 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
1078 group, element_name);
1083 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1089 pe = peek_element (parser);
1090 _dbus_assert (pe != NULL);
1091 _dbus_assert (pe->type == ELEMENT_POLICY);
1093 switch (pe->d.policy.type)
1095 case POLICY_IGNORED:
1096 /* drop the rule on the floor */
1099 case POLICY_DEFAULT:
1100 if (!bus_policy_append_default_rule (parser->policy, rule))
1103 case POLICY_MANDATORY:
1104 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1108 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1110 dbus_set_error (error, DBUS_ERROR_FAILED,
1111 "<%s> rule cannot be per-user because it has bus-global semantics",
1116 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
1121 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1123 dbus_set_error (error, DBUS_ERROR_FAILED,
1124 "<%s> rule cannot be per-group because it has bus-global semantics",
1129 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
1135 bus_policy_rule_unref (rule);
1142 BUS_SET_OOM (error);
1145 bus_policy_rule_unref (rule);
1150 start_policy_child (BusConfigParser *parser,
1151 const char *element_name,
1152 const char **attribute_names,
1153 const char **attribute_values,
1156 if (strcmp (element_name, "allow") == 0)
1158 if (!append_rule_from_element (parser, element_name,
1159 attribute_names, attribute_values,
1163 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1165 BUS_SET_OOM (error);
1171 else if (strcmp (element_name, "deny") == 0)
1173 if (!append_rule_from_element (parser, element_name,
1174 attribute_names, attribute_values,
1178 if (push_element (parser, ELEMENT_DENY) == NULL)
1180 BUS_SET_OOM (error);
1188 dbus_set_error (error, DBUS_ERROR_FAILED,
1189 "Element <%s> not allowed inside <%s> in configuration file",
1190 element_name, "policy");
1196 bus_config_parser_start_element (BusConfigParser *parser,
1197 const char *element_name,
1198 const char **attribute_names,
1199 const char **attribute_values,
1204 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1206 /* printf ("START: %s\n", element_name); */
1208 t = top_element_type (parser);
1210 if (t == ELEMENT_NONE)
1212 if (strcmp (element_name, "busconfig") == 0)
1214 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1217 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1219 BUS_SET_OOM (error);
1227 dbus_set_error (error, DBUS_ERROR_FAILED,
1228 "Unknown element <%s> at root of configuration file",
1233 else if (t == ELEMENT_BUSCONFIG)
1235 return start_busconfig_child (parser, element_name,
1236 attribute_names, attribute_values,
1239 else if (t == ELEMENT_POLICY)
1241 return start_policy_child (parser, element_name,
1242 attribute_names, attribute_values,
1247 dbus_set_error (error, DBUS_ERROR_FAILED,
1248 "Element <%s> is not allowed in this context",
1255 set_limit (BusConfigParser *parser,
1260 dbus_bool_t must_be_positive;
1261 dbus_bool_t must_be_int;
1263 must_be_int = FALSE;
1264 must_be_positive = FALSE;
1266 if (strcmp (name, "max_incoming_bytes") == 0)
1268 must_be_positive = TRUE;
1269 parser->limits.max_incoming_bytes = value;
1271 else if (strcmp (name, "max_outgoing_bytes") == 0)
1273 must_be_positive = TRUE;
1274 parser->limits.max_outgoing_bytes = value;
1276 else if (strcmp (name, "max_message_size") == 0)
1278 must_be_positive = TRUE;
1279 parser->limits.max_message_size = value;
1281 else if (strcmp (name, "activation_timeout") == 0)
1283 must_be_positive = TRUE;
1285 parser->limits.activation_timeout = value;
1287 else if (strcmp (name, "auth_timeout") == 0)
1289 must_be_positive = TRUE;
1291 parser->limits.auth_timeout = value;
1293 else if (strcmp (name, "max_completed_connections") == 0)
1295 must_be_positive = TRUE;
1297 parser->limits.max_completed_connections = value;
1299 else if (strcmp (name, "max_incomplete_connections") == 0)
1301 must_be_positive = TRUE;
1303 parser->limits.max_incomplete_connections = value;
1305 else if (strcmp (name, "max_connections_per_user") == 0)
1307 must_be_positive = TRUE;
1309 parser->limits.max_connections_per_user = value;
1311 else if (strcmp (name, "max_pending_activations") == 0)
1313 must_be_positive = TRUE;
1315 parser->limits.max_pending_activations = value;
1317 else if (strcmp (name, "max_services_per_connection") == 0)
1319 must_be_positive = TRUE;
1321 parser->limits.max_services_per_connection = value;
1325 dbus_set_error (error, DBUS_ERROR_FAILED,
1326 "There is no limit called \"%s\"\n",
1331 if (must_be_positive && value < 0)
1333 dbus_set_error (error, DBUS_ERROR_FAILED,
1334 "<limit name=\"%s\"> must be a positive number\n",
1340 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1342 dbus_set_error (error, DBUS_ERROR_FAILED,
1343 "<limit name=\"%s\"> value is too large\n",
1352 bus_config_parser_end_element (BusConfigParser *parser,
1353 const char *element_name,
1360 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1362 /* printf ("END: %s\n", element_name); */
1364 t = top_element_type (parser);
1366 if (t == ELEMENT_NONE)
1368 /* should probably be an assertion failure but
1369 * being paranoid about XML parsers
1371 dbus_set_error (error, DBUS_ERROR_FAILED,
1372 "XML parser ended element with no element on the stack");
1376 n = element_type_to_name (t);
1377 _dbus_assert (n != NULL);
1378 if (strcmp (n, element_name) != 0)
1380 /* should probably be an assertion failure but
1381 * being paranoid about XML parsers
1383 dbus_set_error (error, DBUS_ERROR_FAILED,
1384 "XML element <%s> ended but topmost element on the stack was <%s>",
1389 e = peek_element (parser);
1390 _dbus_assert (e != NULL);
1395 _dbus_assert_not_reached ("element in stack has no type");
1398 case ELEMENT_INCLUDE:
1401 case ELEMENT_LISTEN:
1402 case ELEMENT_PIDFILE:
1404 case ELEMENT_SERVICEDIR:
1405 case ELEMENT_INCLUDEDIR:
1407 if (!e->had_content)
1409 dbus_set_error (error, DBUS_ERROR_FAILED,
1410 "XML element <%s> was expected to have content inside it",
1411 element_type_to_name (e->type));
1415 if (e->type == ELEMENT_LIMIT)
1417 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1423 case ELEMENT_BUSCONFIG:
1424 case ELEMENT_POLICY:
1431 pop_element (parser);
1437 all_whitespace (const DBusString *str)
1441 _dbus_string_skip_white (str, 0, &i);
1443 return i == _dbus_string_get_length (str);
1447 make_full_path (const DBusString *basedir,
1448 const DBusString *filename,
1449 DBusString *full_path)
1451 if (_dbus_path_is_absolute (filename))
1453 return _dbus_string_copy (filename, 0, full_path, 0);
1457 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1460 if (!_dbus_concat_dir_and_file (full_path, filename))
1468 include_file (BusConfigParser *parser,
1469 const DBusString *filename,
1470 dbus_bool_t ignore_missing,
1473 /* FIXME good test case for this would load each config file in the
1474 * test suite both alone, and as an include, and check
1475 * that the result is the same
1477 BusConfigParser *included;
1478 DBusError tmp_error;
1480 dbus_error_init (&tmp_error);
1481 included = bus_config_load (filename, FALSE, &tmp_error);
1482 if (included == NULL)
1484 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1486 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1489 dbus_error_free (&tmp_error);
1494 dbus_move_error (&tmp_error, error);
1500 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1502 if (!merge_included (parser, included, error))
1504 bus_config_parser_unref (included);
1508 bus_config_parser_unref (included);
1514 include_dir (BusConfigParser *parser,
1515 const DBusString *dirname,
1518 DBusString filename;
1520 DBusError tmp_error;
1523 if (!_dbus_string_init (&filename))
1525 BUS_SET_OOM (error);
1531 dir = _dbus_directory_open (dirname, error);
1536 dbus_error_init (&tmp_error);
1537 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1539 DBusString full_path;
1541 if (!_dbus_string_init (&full_path))
1543 BUS_SET_OOM (error);
1547 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1549 BUS_SET_OOM (error);
1550 _dbus_string_free (&full_path);
1554 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1556 BUS_SET_OOM (error);
1557 _dbus_string_free (&full_path);
1561 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1563 if (!include_file (parser, &full_path, TRUE, error))
1565 _dbus_string_free (&full_path);
1570 _dbus_string_free (&full_path);
1573 if (dbus_error_is_set (&tmp_error))
1575 dbus_move_error (&tmp_error, error);
1582 _dbus_string_free (&filename);
1585 _dbus_directory_close (dir);
1591 bus_config_parser_content (BusConfigParser *parser,
1592 const DBusString *content,
1597 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1603 _dbus_string_get_const_data (content, &c_str);
1605 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1609 e = peek_element (parser);
1612 dbus_set_error (error, DBUS_ERROR_FAILED,
1613 "Text content outside of any XML element in configuration file");
1616 else if (e->had_content)
1618 _dbus_assert_not_reached ("Element had multiple content blocks");
1622 switch (top_element_type (parser))
1625 _dbus_assert_not_reached ("element at top of stack has no type");
1628 case ELEMENT_BUSCONFIG:
1629 case ELEMENT_POLICY:
1633 if (all_whitespace (content))
1637 dbus_set_error (error, DBUS_ERROR_FAILED,
1638 "No text content expected inside XML element %s in configuration file",
1639 element_type_to_name (top_element_type (parser)));
1643 case ELEMENT_PIDFILE:
1647 e->had_content = TRUE;
1649 if (!_dbus_string_copy_data (content, &s))
1652 dbus_free (parser->pidfile);
1653 parser->pidfile = s;
1657 case ELEMENT_INCLUDE:
1659 DBusString full_path;
1661 e->had_content = TRUE;
1663 if (!_dbus_string_init (&full_path))
1666 if (!make_full_path (&parser->basedir, content, &full_path))
1668 _dbus_string_free (&full_path);
1672 if (!include_file (parser, &full_path,
1673 e->d.include.ignore_missing, error))
1675 _dbus_string_free (&full_path);
1679 _dbus_string_free (&full_path);
1683 case ELEMENT_INCLUDEDIR:
1685 DBusString full_path;
1687 e->had_content = TRUE;
1689 if (!_dbus_string_init (&full_path))
1692 if (!make_full_path (&parser->basedir, content, &full_path))
1694 _dbus_string_free (&full_path);
1698 if (!include_dir (parser, &full_path, error))
1700 _dbus_string_free (&full_path);
1704 _dbus_string_free (&full_path);
1712 e->had_content = TRUE;
1714 if (!_dbus_string_copy_data (content, &s))
1717 dbus_free (parser->user);
1726 e->had_content = TRUE;
1728 if (!_dbus_string_copy_data (content, &s))
1731 dbus_free (parser->bus_type);
1732 parser->bus_type = s;
1736 case ELEMENT_LISTEN:
1740 e->had_content = TRUE;
1742 if (!_dbus_string_copy_data (content, &s))
1745 if (!_dbus_list_append (&parser->listen_on,
1758 e->had_content = TRUE;
1760 if (!_dbus_string_copy_data (content, &s))
1763 if (!_dbus_list_append (&parser->mechanisms,
1772 case ELEMENT_SERVICEDIR:
1775 DBusString full_path;
1777 e->had_content = TRUE;
1779 if (!_dbus_string_init (&full_path))
1782 if (!make_full_path (&parser->basedir, content, &full_path))
1784 _dbus_string_free (&full_path);
1788 if (!_dbus_string_copy_data (&full_path, &s))
1790 _dbus_string_free (&full_path);
1794 if (!_dbus_list_append (&parser->service_dirs, s))
1796 _dbus_string_free (&full_path);
1801 _dbus_string_free (&full_path);
1809 e->had_content = TRUE;
1812 if (!_dbus_string_parse_int (content, 0, &val, NULL))
1814 dbus_set_error (error, DBUS_ERROR_FAILED,
1815 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
1820 e->d.limit.value = val;
1822 _dbus_verbose ("Loaded value %ld for limit %s\n",
1829 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1833 BUS_SET_OOM (error);
1838 bus_config_parser_finished (BusConfigParser *parser,
1841 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1843 if (parser->stack != NULL)
1845 dbus_set_error (error, DBUS_ERROR_FAILED,
1846 "Element <%s> was not closed in configuration file",
1847 element_type_to_name (top_element_type (parser)));
1852 if (parser->is_toplevel && parser->listen_on == NULL)
1854 dbus_set_error (error, DBUS_ERROR_FAILED,
1855 "Configuration file needs one or more <listen> elements giving addresses");
1863 bus_config_parser_get_user (BusConfigParser *parser)
1865 return parser->user;
1869 bus_config_parser_get_type (BusConfigParser *parser)
1871 return parser->bus_type;
1875 bus_config_parser_get_addresses (BusConfigParser *parser)
1877 return &parser->listen_on;
1881 bus_config_parser_get_mechanisms (BusConfigParser *parser)
1883 return &parser->mechanisms;
1887 bus_config_parser_get_service_dirs (BusConfigParser *parser)
1889 return &parser->service_dirs;
1893 bus_config_parser_get_fork (BusConfigParser *parser)
1895 return parser->fork;
1899 bus_config_parser_get_pidfile (BusConfigParser *parser)
1901 return parser->pidfile;
1905 bus_config_parser_steal_policy (BusConfigParser *parser)
1909 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
1911 policy = parser->policy;
1913 parser->policy = NULL;
1918 /* Overwrite any limits that were set in the configuration file */
1920 bus_config_parser_get_limits (BusConfigParser *parser,
1923 *limits = parser->limits;
1926 #ifdef DBUS_BUILD_TESTS
1937 do_load (const DBusString *full_path,
1939 dbus_bool_t oom_possible)
1941 BusConfigParser *parser;
1944 dbus_error_init (&error);
1946 parser = bus_config_load (full_path, TRUE, &error);
1949 _DBUS_ASSERT_ERROR_IS_SET (&error);
1952 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1954 _dbus_verbose ("Failed to load valid file due to OOM\n");
1955 dbus_error_free (&error);
1958 else if (validity == VALID)
1960 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
1963 dbus_error_free (&error);
1968 dbus_error_free (&error);
1974 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
1976 bus_config_parser_unref (parser);
1978 if (validity == INVALID)
1980 _dbus_warn ("Accepted invalid file\n");
1990 const DBusString *full_path;
1995 check_loader_oom_func (void *data)
1997 LoaderOomData *d = data;
1999 return do_load (d->full_path, d->validity, TRUE);
2003 process_test_subdir (const DBusString *test_base_dir,
2007 DBusString test_directory;
2008 DBusString filename;
2016 if (!_dbus_string_init (&test_directory))
2017 _dbus_assert_not_reached ("didn't allocate test_directory\n");
2019 _dbus_string_init_const (&filename, subdir);
2021 if (!_dbus_string_copy (test_base_dir, 0,
2022 &test_directory, 0))
2023 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2025 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
2026 _dbus_assert_not_reached ("couldn't allocate full path");
2028 _dbus_string_free (&filename);
2029 if (!_dbus_string_init (&filename))
2030 _dbus_assert_not_reached ("didn't allocate filename string\n");
2032 dbus_error_init (&error);
2033 dir = _dbus_directory_open (&test_directory, &error);
2036 _dbus_warn ("Could not open %s: %s\n",
2037 _dbus_string_get_const_data (&test_directory),
2039 dbus_error_free (&error);
2043 printf ("Testing:\n");
2046 while (_dbus_directory_get_next_file (dir, &filename, &error))
2048 DBusString full_path;
2051 if (!_dbus_string_init (&full_path))
2052 _dbus_assert_not_reached ("couldn't init string");
2054 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2055 _dbus_assert_not_reached ("couldn't copy dir to full_path");
2057 if (!_dbus_concat_dir_and_file (&full_path, &filename))
2058 _dbus_assert_not_reached ("couldn't concat file to dir");
2060 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
2062 _dbus_verbose ("Skipping non-.conf file %s\n",
2063 _dbus_string_get_const_data (&filename));
2064 _dbus_string_free (&full_path);
2068 printf (" %s\n", _dbus_string_get_const_data (&filename));
2070 _dbus_verbose (" expecting %s\n",
2071 validity == VALID ? "valid" :
2072 (validity == INVALID ? "invalid" :
2073 (validity == UNKNOWN ? "unknown" : "???")));
2075 d.full_path = &full_path;
2076 d.validity = validity;
2077 if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d))
2078 _dbus_assert_not_reached ("test failed");
2080 _dbus_string_free (&full_path);
2083 if (dbus_error_is_set (&error))
2085 _dbus_warn ("Could not get next file in %s: %s\n",
2086 _dbus_string_get_const_data (&test_directory),
2088 dbus_error_free (&error);
2097 _dbus_directory_close (dir);
2098 _dbus_string_free (&test_directory);
2099 _dbus_string_free (&filename);
2105 bus_config_parser_test (const DBusString *test_data_dir)
2107 if (test_data_dir == NULL ||
2108 _dbus_string_get_length (test_data_dir) == 0)
2110 printf ("No test data\n");
2114 if (!process_test_subdir (test_data_dir, "valid-config-files", VALID))
2120 #endif /* DBUS_BUILD_TESTS */