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 (included->user != NULL)
236 dbus_free (parser->user);
237 parser->user = included->user;
238 included->user = NULL;
241 if (included->bus_type != NULL)
243 dbus_free (parser->bus_type);
244 parser->bus_type = included->bus_type;
245 included->bus_type = NULL;
251 if (included->pidfile != NULL)
253 dbus_free (parser->pidfile);
254 parser->pidfile = included->pidfile;
255 included->pidfile = NULL;
258 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
259 _dbus_list_append_link (&parser->listen_on, link);
261 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
262 _dbus_list_append_link (&parser->mechanisms, link);
264 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
265 _dbus_list_append_link (&parser->service_dirs, link);
271 bus_config_parser_new (const DBusString *basedir,
272 dbus_bool_t is_toplevel)
274 BusConfigParser *parser;
276 parser = dbus_new0 (BusConfigParser, 1);
280 parser->is_toplevel = !!is_toplevel;
282 if (!_dbus_string_init (&parser->basedir))
288 if (((parser->policy = bus_policy_new ()) == NULL) ||
289 !_dbus_string_copy (basedir, 0, &parser->basedir, 0))
292 bus_policy_unref (parser->policy);
294 _dbus_string_free (&parser->basedir);
299 /* Make up some numbers! woot! */
300 parser->limits.max_incoming_bytes = _DBUS_ONE_MEGABYTE * 63;
301 parser->limits.max_outgoing_bytes = _DBUS_ONE_MEGABYTE * 63;
302 parser->limits.max_message_size = _DBUS_ONE_MEGABYTE * 32;
304 /* Making this long means the user has to wait longer for an error
305 * message if something screws up, but making it too short means
306 * they might see a false failure.
308 parser->limits.activation_timeout = 25000; /* 25 seconds */
310 /* Making this long risks making a DOS attack easier, but too short
311 * and legitimate auth will fail. If interactive auth (ask user for
312 * password) is allowed, then potentially it has to be quite long.
314 parser->limits.auth_timeout = 30000; /* 30 seconds */
316 parser->limits.max_incomplete_connections = 32;
317 parser->limits.max_connections_per_user = 128;
319 /* Note that max_completed_connections / max_connections_per_user
320 * is the number of users that would have to work together to
321 * DOS all the other users.
323 parser->limits.max_completed_connections = 1024;
325 parser->limits.max_pending_activations = 256;
326 parser->limits.max_services_per_connection = 256;
328 parser->refcount = 1;
334 bus_config_parser_ref (BusConfigParser *parser)
336 _dbus_assert (parser->refcount > 0);
338 parser->refcount += 1;
342 bus_config_parser_unref (BusConfigParser *parser)
344 _dbus_assert (parser->refcount > 0);
346 parser->refcount -= 1;
348 if (parser->refcount == 0)
350 while (parser->stack != NULL)
351 pop_element (parser);
353 dbus_free (parser->user);
354 dbus_free (parser->bus_type);
355 dbus_free (parser->pidfile);
357 _dbus_list_foreach (&parser->listen_on,
358 (DBusForeachFunction) dbus_free,
361 _dbus_list_clear (&parser->listen_on);
363 _dbus_list_foreach (&parser->service_dirs,
364 (DBusForeachFunction) dbus_free,
367 _dbus_list_clear (&parser->service_dirs);
369 _dbus_list_foreach (&parser->mechanisms,
370 (DBusForeachFunction) dbus_free,
373 _dbus_list_clear (&parser->mechanisms);
375 _dbus_string_free (&parser->basedir);
378 bus_policy_unref (parser->policy);
385 bus_config_parser_check_doctype (BusConfigParser *parser,
389 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
391 if (strcmp (doctype, "busconfig") != 0)
393 dbus_set_error (error,
395 "Configuration file has the wrong document type %s",
410 locate_attributes (BusConfigParser *parser,
411 const char *element_name,
412 const char **attribute_names,
413 const char **attribute_values,
415 const char *first_attribute_name,
416 const char **first_attribute_retloc,
424 LocateAttr attrs[MAX_ATTRS];
428 _dbus_assert (first_attribute_name != NULL);
429 _dbus_assert (first_attribute_retloc != NULL);
434 attrs[0].name = first_attribute_name;
435 attrs[0].retloc = first_attribute_retloc;
436 *first_attribute_retloc = NULL;
438 va_start (args, first_attribute_retloc);
440 name = va_arg (args, const char*);
441 retloc = va_arg (args, const char**);
445 _dbus_assert (retloc != NULL);
446 _dbus_assert (n_attrs < MAX_ATTRS);
448 attrs[n_attrs].name = name;
449 attrs[n_attrs].retloc = retloc;
453 name = va_arg (args, const char*);
454 retloc = va_arg (args, const char**);
463 while (attribute_names[i])
472 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
474 retloc = attrs[j].retloc;
478 dbus_set_error (error, DBUS_ERROR_FAILED,
479 "Attribute \"%s\" repeated twice on the same <%s> element",
480 attrs[j].name, element_name);
485 *retloc = attribute_values[i];
494 dbus_set_error (error, DBUS_ERROR_FAILED,
495 "Attribute \"%s\" is invalid on <%s> element in this context",
496 attribute_names[i], element_name);
509 check_no_attributes (BusConfigParser *parser,
510 const char *element_name,
511 const char **attribute_names,
512 const char **attribute_values,
515 if (attribute_names[0] != NULL)
517 dbus_set_error (error, DBUS_ERROR_FAILED,
518 "Attribute \"%s\" is invalid on <%s> element in this context",
519 attribute_names[0], element_name);
527 start_busconfig_child (BusConfigParser *parser,
528 const char *element_name,
529 const char **attribute_names,
530 const char **attribute_values,
533 if (strcmp (element_name, "user") == 0)
535 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
538 if (push_element (parser, ELEMENT_USER) == NULL)
546 else if (strcmp (element_name, "type") == 0)
548 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
551 if (push_element (parser, ELEMENT_TYPE) == NULL)
559 else if (strcmp (element_name, "fork") == 0)
561 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
564 if (push_element (parser, ELEMENT_FORK) == NULL)
574 else if (strcmp (element_name, "pidfile") == 0)
576 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
579 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
587 else if (strcmp (element_name, "listen") == 0)
589 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
592 if (push_element (parser, ELEMENT_LISTEN) == NULL)
600 else if (strcmp (element_name, "auth") == 0)
602 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
605 if (push_element (parser, ELEMENT_AUTH) == NULL)
613 else if (strcmp (element_name, "includedir") == 0)
615 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
618 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
626 else if (strcmp (element_name, "servicedir") == 0)
628 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
631 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
639 else if (strcmp (element_name, "include") == 0)
642 const char *ignore_missing;
644 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
650 e->d.include.ignore_missing = FALSE;
652 if (!locate_attributes (parser, "include",
656 "ignore_missing", &ignore_missing,
660 if (ignore_missing != NULL)
662 if (strcmp (ignore_missing, "yes") == 0)
663 e->d.include.ignore_missing = TRUE;
664 else if (strcmp (ignore_missing, "no") == 0)
665 e->d.include.ignore_missing = FALSE;
668 dbus_set_error (error, DBUS_ERROR_FAILED,
669 "ignore_missing attribute must have value \"yes\" or \"no\"");
676 else if (strcmp (element_name, "policy") == 0)
683 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
689 e->d.policy.type = POLICY_IGNORED;
691 if (!locate_attributes (parser, "policy",
701 if (((context && user) ||
702 (context && group)) ||
704 !(context || user || group))
706 dbus_set_error (error, DBUS_ERROR_FAILED,
707 "<policy> element must have exactly one of (context|user|group) attributes");
713 if (strcmp (context, "default") == 0)
715 e->d.policy.type = POLICY_DEFAULT;
717 else if (strcmp (context, "mandatory") == 0)
719 e->d.policy.type = POLICY_MANDATORY;
723 dbus_set_error (error, DBUS_ERROR_FAILED,
724 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
729 else if (user != NULL)
732 _dbus_string_init_const (&username, user);
734 if (_dbus_get_user_id (&username,
735 &e->d.policy.gid_or_uid))
736 e->d.policy.type = POLICY_USER;
738 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
741 else if (group != NULL)
743 DBusString group_name;
744 _dbus_string_init_const (&group_name, group);
746 if (_dbus_get_group_id (&group_name,
747 &e->d.policy.gid_or_uid))
748 e->d.policy.type = POLICY_GROUP;
750 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
755 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
760 else if (strcmp (element_name, "limit") == 0)
765 if ((e = push_element (parser, ELEMENT_LIMIT)) == NULL)
771 if (!locate_attributes (parser, "limit",
781 dbus_set_error (error, DBUS_ERROR_FAILED,
782 "<limit> element must have a \"name\" attribute");
786 e->d.limit.name = _dbus_strdup (name);
787 if (e->d.limit.name == NULL)
797 dbus_set_error (error, DBUS_ERROR_FAILED,
798 "Element <%s> not allowed inside <%s> in configuration file",
799 element_name, "busconfig");
805 append_rule_from_element (BusConfigParser *parser,
806 const char *element_name,
807 const char **attribute_names,
808 const char **attribute_values,
816 const char *receive_from;
821 if (!locate_attributes (parser, element_name,
829 "receive_from", &receive_from,
835 if (!(send || receive || own || send_to || receive_from ||
838 dbus_set_error (error, DBUS_ERROR_FAILED,
839 "Element <%s> must have one or more attributes",
844 if (((send && own) ||
846 (send && receive_from) ||
851 (receive && send_to) ||
853 (receive && group)) ||
856 (own && receive_from) ||
860 ((send_to && receive_from) ||
862 (send_to && group)) ||
864 ((receive_from && user) ||
865 (receive_from && group)) ||
869 dbus_set_error (error, DBUS_ERROR_FAILED,
870 "Invalid combination of attributes on element <%s>, "
871 "only send/send_to or receive/receive_from may be paired",
878 /* In BusPolicyRule, NULL represents wildcard.
879 * In the config file, '*' represents it.
881 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
885 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
889 if (IS_WILDCARD (send))
891 if (IS_WILDCARD (send_to))
894 rule->d.send.message_name = _dbus_strdup (send);
895 rule->d.send.destination = _dbus_strdup (send_to);
896 if (send && rule->d.send.message_name == NULL)
898 if (send_to && rule->d.send.destination == NULL)
901 else if (receive || receive_from)
903 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
907 if (IS_WILDCARD (receive))
910 if (IS_WILDCARD (receive_from))
913 rule->d.receive.message_name = _dbus_strdup (receive);
914 rule->d.receive.origin = _dbus_strdup (receive_from);
915 if (receive && rule->d.receive.message_name == NULL)
917 if (receive_from && rule->d.receive.origin == NULL)
922 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
926 if (IS_WILDCARD (own))
929 rule->d.own.service_name = _dbus_strdup (own);
930 if (own && rule->d.own.service_name == NULL)
935 if (IS_WILDCARD (user))
937 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
941 rule->d.user.uid = DBUS_UID_UNSET;
948 _dbus_string_init_const (&username, user);
950 if (_dbus_get_user_id (&username, &uid))
952 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
956 rule->d.user.uid = uid;
960 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
967 if (IS_WILDCARD (group))
969 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
973 rule->d.group.gid = DBUS_GID_UNSET;
977 DBusString groupname;
980 _dbus_string_init_const (&groupname, group);
982 if (_dbus_get_user_id (&groupname, &gid))
984 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
988 rule->d.group.gid = gid;
992 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
993 group, element_name);
998 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
1004 pe = peek_element (parser);
1005 _dbus_assert (pe != NULL);
1006 _dbus_assert (pe->type == ELEMENT_POLICY);
1008 switch (pe->d.policy.type)
1010 case POLICY_IGNORED:
1011 /* drop the rule on the floor */
1014 case POLICY_DEFAULT:
1015 if (!bus_policy_append_default_rule (parser->policy, rule))
1018 case POLICY_MANDATORY:
1019 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
1023 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1025 dbus_set_error (error, DBUS_ERROR_FAILED,
1026 "<%s> rule cannot be per-user because it has bus-global semantics",
1031 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
1036 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
1038 dbus_set_error (error, DBUS_ERROR_FAILED,
1039 "<%s> rule cannot be per-group because it has bus-global semantics",
1044 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
1050 bus_policy_rule_unref (rule);
1057 BUS_SET_OOM (error);
1060 bus_policy_rule_unref (rule);
1065 start_policy_child (BusConfigParser *parser,
1066 const char *element_name,
1067 const char **attribute_names,
1068 const char **attribute_values,
1071 if (strcmp (element_name, "allow") == 0)
1073 if (!append_rule_from_element (parser, element_name,
1074 attribute_names, attribute_values,
1078 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1080 BUS_SET_OOM (error);
1086 else if (strcmp (element_name, "deny") == 0)
1088 if (!append_rule_from_element (parser, element_name,
1089 attribute_names, attribute_values,
1093 if (push_element (parser, ELEMENT_DENY) == NULL)
1095 BUS_SET_OOM (error);
1103 dbus_set_error (error, DBUS_ERROR_FAILED,
1104 "Element <%s> not allowed inside <%s> in configuration file",
1105 element_name, "policy");
1111 bus_config_parser_start_element (BusConfigParser *parser,
1112 const char *element_name,
1113 const char **attribute_names,
1114 const char **attribute_values,
1119 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1121 /* printf ("START: %s\n", element_name); */
1123 t = top_element_type (parser);
1125 if (t == ELEMENT_NONE)
1127 if (strcmp (element_name, "busconfig") == 0)
1129 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1132 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1134 BUS_SET_OOM (error);
1142 dbus_set_error (error, DBUS_ERROR_FAILED,
1143 "Unknown element <%s> at root of configuration file",
1148 else if (t == ELEMENT_BUSCONFIG)
1150 return start_busconfig_child (parser, element_name,
1151 attribute_names, attribute_values,
1154 else if (t == ELEMENT_POLICY)
1156 return start_policy_child (parser, element_name,
1157 attribute_names, attribute_values,
1162 dbus_set_error (error, DBUS_ERROR_FAILED,
1163 "Element <%s> is not allowed in this context",
1170 set_limit (BusConfigParser *parser,
1175 dbus_bool_t must_be_positive;
1176 dbus_bool_t must_be_int;
1178 must_be_int = FALSE;
1179 must_be_positive = FALSE;
1181 if (strcmp (name, "max_incoming_bytes") == 0)
1183 must_be_positive = TRUE;
1184 parser->limits.max_incoming_bytes = value;
1186 else if (strcmp (name, "max_outgoing_bytes") == 0)
1188 must_be_positive = TRUE;
1189 parser->limits.max_outgoing_bytes = value;
1191 else if (strcmp (name, "max_message_size") == 0)
1193 must_be_positive = TRUE;
1194 parser->limits.max_message_size = value;
1196 else if (strcmp (name, "activation_timeout") == 0)
1198 must_be_positive = TRUE;
1200 parser->limits.activation_timeout = value;
1202 else if (strcmp (name, "auth_timeout") == 0)
1204 must_be_positive = TRUE;
1206 parser->limits.auth_timeout = value;
1208 else if (strcmp (name, "max_completed_connections") == 0)
1210 must_be_positive = TRUE;
1212 parser->limits.max_completed_connections = value;
1214 else if (strcmp (name, "max_incomplete_connections") == 0)
1216 must_be_positive = TRUE;
1218 parser->limits.max_incomplete_connections = value;
1220 else if (strcmp (name, "max_connections_per_user") == 0)
1222 must_be_positive = TRUE;
1224 parser->limits.max_connections_per_user = value;
1226 else if (strcmp (name, "max_pending_activations") == 0)
1228 must_be_positive = TRUE;
1230 parser->limits.max_pending_activations = value;
1232 else if (strcmp (name, "max_services_per_connection") == 0)
1234 must_be_positive = TRUE;
1236 parser->limits.max_services_per_connection = value;
1240 dbus_set_error (error, DBUS_ERROR_FAILED,
1241 "There is no limit called \"%s\"\n",
1246 if (must_be_positive && value < 0)
1248 dbus_set_error (error, DBUS_ERROR_FAILED,
1249 "<limit name=\"%s\"> must be a positive number\n",
1255 (value < _DBUS_INT_MIN || value > _DBUS_INT_MAX))
1257 dbus_set_error (error, DBUS_ERROR_FAILED,
1258 "<limit name=\"%s\"> value is too large\n",
1267 bus_config_parser_end_element (BusConfigParser *parser,
1268 const char *element_name,
1275 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1277 /* printf ("END: %s\n", element_name); */
1279 t = top_element_type (parser);
1281 if (t == ELEMENT_NONE)
1283 /* should probably be an assertion failure but
1284 * being paranoid about XML parsers
1286 dbus_set_error (error, DBUS_ERROR_FAILED,
1287 "XML parser ended element with no element on the stack");
1291 n = element_type_to_name (t);
1292 _dbus_assert (n != NULL);
1293 if (strcmp (n, element_name) != 0)
1295 /* should probably be an assertion failure but
1296 * being paranoid about XML parsers
1298 dbus_set_error (error, DBUS_ERROR_FAILED,
1299 "XML element <%s> ended but topmost element on the stack was <%s>",
1304 e = peek_element (parser);
1305 _dbus_assert (e != NULL);
1310 _dbus_assert_not_reached ("element in stack has no type");
1313 case ELEMENT_INCLUDE:
1316 case ELEMENT_LISTEN:
1317 case ELEMENT_PIDFILE:
1319 case ELEMENT_SERVICEDIR:
1320 case ELEMENT_INCLUDEDIR:
1322 if (!e->had_content)
1324 dbus_set_error (error, DBUS_ERROR_FAILED,
1325 "XML element <%s> was expected to have content inside it",
1326 element_type_to_name (e->type));
1330 if (e->type == ELEMENT_LIMIT)
1332 if (!set_limit (parser, e->d.limit.name, e->d.limit.value,
1338 case ELEMENT_BUSCONFIG:
1339 case ELEMENT_POLICY:
1346 pop_element (parser);
1352 all_whitespace (const DBusString *str)
1356 _dbus_string_skip_white (str, 0, &i);
1358 return i == _dbus_string_get_length (str);
1362 make_full_path (const DBusString *basedir,
1363 const DBusString *filename,
1364 DBusString *full_path)
1366 if (_dbus_path_is_absolute (filename))
1368 return _dbus_string_copy (filename, 0, full_path, 0);
1372 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1375 if (!_dbus_concat_dir_and_file (full_path, filename))
1383 include_file (BusConfigParser *parser,
1384 const DBusString *filename,
1385 dbus_bool_t ignore_missing,
1388 /* FIXME good test case for this would load each config file in the
1389 * test suite both alone, and as an include, and check
1390 * that the result is the same
1392 BusConfigParser *included;
1393 DBusError tmp_error;
1395 dbus_error_init (&tmp_error);
1396 included = bus_config_load (filename, FALSE, &tmp_error);
1397 if (included == NULL)
1399 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1401 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1404 dbus_error_free (&tmp_error);
1409 dbus_move_error (&tmp_error, error);
1415 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1417 if (!merge_included (parser, included, error))
1419 bus_config_parser_unref (included);
1423 bus_config_parser_unref (included);
1429 include_dir (BusConfigParser *parser,
1430 const DBusString *dirname,
1433 DBusString filename;
1435 DBusError tmp_error;
1438 if (!_dbus_string_init (&filename))
1440 BUS_SET_OOM (error);
1446 dir = _dbus_directory_open (dirname, error);
1451 dbus_error_init (&tmp_error);
1452 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1454 DBusString full_path;
1456 if (!_dbus_string_init (&full_path))
1458 BUS_SET_OOM (error);
1462 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1464 BUS_SET_OOM (error);
1465 _dbus_string_free (&full_path);
1469 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1471 BUS_SET_OOM (error);
1472 _dbus_string_free (&full_path);
1476 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1478 if (!include_file (parser, &full_path, TRUE, error))
1480 _dbus_string_free (&full_path);
1485 _dbus_string_free (&full_path);
1488 if (dbus_error_is_set (&tmp_error))
1490 dbus_move_error (&tmp_error, error);
1497 _dbus_string_free (&filename);
1500 _dbus_directory_close (dir);
1506 bus_config_parser_content (BusConfigParser *parser,
1507 const DBusString *content,
1512 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1518 _dbus_string_get_const_data (content, &c_str);
1520 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1524 e = peek_element (parser);
1527 dbus_set_error (error, DBUS_ERROR_FAILED,
1528 "Text content outside of any XML element in configuration file");
1531 else if (e->had_content)
1533 _dbus_assert_not_reached ("Element had multiple content blocks");
1537 switch (top_element_type (parser))
1540 _dbus_assert_not_reached ("element at top of stack has no type");
1543 case ELEMENT_BUSCONFIG:
1544 case ELEMENT_POLICY:
1548 if (all_whitespace (content))
1552 dbus_set_error (error, DBUS_ERROR_FAILED,
1553 "No text content expected inside XML element %s in configuration file",
1554 element_type_to_name (top_element_type (parser)));
1558 case ELEMENT_PIDFILE:
1562 e->had_content = TRUE;
1564 if (!_dbus_string_copy_data (content, &s))
1567 dbus_free (parser->pidfile);
1568 parser->pidfile = s;
1572 case ELEMENT_INCLUDE:
1574 DBusString full_path;
1576 e->had_content = TRUE;
1578 if (!_dbus_string_init (&full_path))
1581 if (!make_full_path (&parser->basedir, content, &full_path))
1583 _dbus_string_free (&full_path);
1587 if (!include_file (parser, &full_path,
1588 e->d.include.ignore_missing, error))
1590 _dbus_string_free (&full_path);
1594 _dbus_string_free (&full_path);
1598 case ELEMENT_INCLUDEDIR:
1600 DBusString full_path;
1602 e->had_content = TRUE;
1604 if (!_dbus_string_init (&full_path))
1607 if (!make_full_path (&parser->basedir, content, &full_path))
1609 _dbus_string_free (&full_path);
1613 if (!include_dir (parser, &full_path, error))
1615 _dbus_string_free (&full_path);
1619 _dbus_string_free (&full_path);
1627 e->had_content = TRUE;
1629 if (!_dbus_string_copy_data (content, &s))
1632 dbus_free (parser->user);
1641 e->had_content = TRUE;
1643 if (!_dbus_string_copy_data (content, &s))
1646 dbus_free (parser->bus_type);
1647 parser->bus_type = s;
1651 case ELEMENT_LISTEN:
1655 e->had_content = TRUE;
1657 if (!_dbus_string_copy_data (content, &s))
1660 if (!_dbus_list_append (&parser->listen_on,
1673 e->had_content = TRUE;
1675 if (!_dbus_string_copy_data (content, &s))
1678 if (!_dbus_list_append (&parser->mechanisms,
1687 case ELEMENT_SERVICEDIR:
1690 DBusString full_path;
1692 e->had_content = TRUE;
1694 if (!_dbus_string_init (&full_path))
1697 if (!make_full_path (&parser->basedir, content, &full_path))
1699 _dbus_string_free (&full_path);
1703 if (!_dbus_string_copy_data (&full_path, &s))
1705 _dbus_string_free (&full_path);
1709 if (!_dbus_list_append (&parser->service_dirs, s))
1711 _dbus_string_free (&full_path);
1716 _dbus_string_free (&full_path);
1724 e->had_content = TRUE;
1727 if (!_dbus_string_parse_int (content, 0, &val, NULL))
1729 dbus_set_error (error, DBUS_ERROR_FAILED,
1730 "<limit name=\"%s\"> element has invalid value (could not parse as integer)",
1735 e->d.limit.value = val;
1737 _dbus_verbose ("Loaded value %ld for limit %s\n",
1744 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1748 BUS_SET_OOM (error);
1753 bus_config_parser_finished (BusConfigParser *parser,
1756 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1758 if (parser->stack != NULL)
1760 dbus_set_error (error, DBUS_ERROR_FAILED,
1761 "Element <%s> was not closed in configuration file",
1762 element_type_to_name (top_element_type (parser)));
1767 if (parser->is_toplevel && parser->listen_on == NULL)
1769 dbus_set_error (error, DBUS_ERROR_FAILED,
1770 "Configuration file needs one or more <listen> elements giving addresses");
1778 bus_config_parser_get_user (BusConfigParser *parser)
1780 return parser->user;
1784 bus_config_parser_get_type (BusConfigParser *parser)
1786 return parser->bus_type;
1790 bus_config_parser_get_addresses (BusConfigParser *parser)
1792 return &parser->listen_on;
1796 bus_config_parser_get_mechanisms (BusConfigParser *parser)
1798 return &parser->mechanisms;
1802 bus_config_parser_get_service_dirs (BusConfigParser *parser)
1804 return &parser->service_dirs;
1808 bus_config_parser_get_fork (BusConfigParser *parser)
1810 return parser->fork;
1814 bus_config_parser_get_pidfile (BusConfigParser *parser)
1816 return parser->pidfile;
1820 bus_config_parser_steal_policy (BusConfigParser *parser)
1824 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
1826 policy = parser->policy;
1828 parser->policy = NULL;
1833 /* Overwrite any limits that were set in the configuration file */
1835 bus_config_parser_get_limits (BusConfigParser *parser,
1838 *limits = parser->limits;
1841 #ifdef DBUS_BUILD_TESTS
1852 do_load (const DBusString *full_path,
1854 dbus_bool_t oom_possible)
1856 BusConfigParser *parser;
1859 dbus_error_init (&error);
1861 parser = bus_config_load (full_path, TRUE, &error);
1864 _DBUS_ASSERT_ERROR_IS_SET (&error);
1867 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1869 _dbus_verbose ("Failed to load valid file due to OOM\n");
1870 dbus_error_free (&error);
1873 else if (validity == VALID)
1875 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
1878 dbus_error_free (&error);
1883 dbus_error_free (&error);
1889 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
1891 bus_config_parser_unref (parser);
1893 if (validity == INVALID)
1895 _dbus_warn ("Accepted invalid file\n");
1905 const DBusString *full_path;
1910 check_loader_oom_func (void *data)
1912 LoaderOomData *d = data;
1914 return do_load (d->full_path, d->validity, TRUE);
1918 process_test_subdir (const DBusString *test_base_dir,
1922 DBusString test_directory;
1923 DBusString filename;
1931 if (!_dbus_string_init (&test_directory))
1932 _dbus_assert_not_reached ("didn't allocate test_directory\n");
1934 _dbus_string_init_const (&filename, subdir);
1936 if (!_dbus_string_copy (test_base_dir, 0,
1937 &test_directory, 0))
1938 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
1940 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
1941 _dbus_assert_not_reached ("couldn't allocate full path");
1943 _dbus_string_free (&filename);
1944 if (!_dbus_string_init (&filename))
1945 _dbus_assert_not_reached ("didn't allocate filename string\n");
1947 dbus_error_init (&error);
1948 dir = _dbus_directory_open (&test_directory, &error);
1951 _dbus_warn ("Could not open %s: %s\n",
1952 _dbus_string_get_const_data (&test_directory),
1954 dbus_error_free (&error);
1958 printf ("Testing:\n");
1961 while (_dbus_directory_get_next_file (dir, &filename, &error))
1963 DBusString full_path;
1966 if (!_dbus_string_init (&full_path))
1967 _dbus_assert_not_reached ("couldn't init string");
1969 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
1970 _dbus_assert_not_reached ("couldn't copy dir to full_path");
1972 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1973 _dbus_assert_not_reached ("couldn't concat file to dir");
1975 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
1977 _dbus_verbose ("Skipping non-.conf file %s\n",
1978 _dbus_string_get_const_data (&filename));
1979 _dbus_string_free (&full_path);
1983 printf (" %s\n", _dbus_string_get_const_data (&filename));
1985 _dbus_verbose (" expecting %s\n",
1986 validity == VALID ? "valid" :
1987 (validity == INVALID ? "invalid" :
1988 (validity == UNKNOWN ? "unknown" : "???")));
1990 d.full_path = &full_path;
1991 d.validity = validity;
1992 if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d))
1993 _dbus_assert_not_reached ("test failed");
1995 _dbus_string_free (&full_path);
1998 if (dbus_error_is_set (&error))
2000 _dbus_warn ("Could not get next file in %s: %s\n",
2001 _dbus_string_get_const_data (&test_directory),
2003 dbus_error_free (&error);
2012 _dbus_directory_close (dir);
2013 _dbus_string_free (&test_directory);
2014 _dbus_string_free (&filename);
2020 bus_config_parser_test (const DBusString *test_data_dir)
2022 if (test_data_dir == NULL ||
2023 _dbus_string_get_length (test_data_dir) == 0)
2025 printf ("No test data\n");
2029 if (!process_test_subdir (test_data_dir, "valid-config-files", VALID))
2035 #endif /* DBUS_BUILD_TESTS */