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;
85 struct BusConfigParser
89 DBusString basedir; /**< Directory we resolve paths relative to */
91 DBusList *stack; /**< stack of Element */
93 char *user; /**< user to run as */
95 char *bus_type; /**< Message bus type */
97 DBusList *listen_on; /**< List of addresses to listen to */
99 DBusList *mechanisms; /**< Auth mechanisms */
101 DBusList *service_dirs; /**< Directories to look for services in */
103 BusPolicy *policy; /**< Security policy */
105 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
111 element_type_to_name (ElementType type)
117 case ELEMENT_BUSCONFIG:
119 case ELEMENT_INCLUDE:
137 case ELEMENT_PIDFILE:
139 case ELEMENT_SERVICEDIR:
141 case ELEMENT_INCLUDEDIR:
147 _dbus_assert_not_reached ("bad element type");
153 push_element (BusConfigParser *parser,
158 _dbus_assert (type != ELEMENT_NONE);
160 e = dbus_new0 (Element, 1);
164 if (!_dbus_list_append (&parser->stack, e))
176 element_free (Element *e)
183 pop_element (BusConfigParser *parser)
187 e = _dbus_list_pop_last (&parser->stack);
193 peek_element (BusConfigParser *parser)
197 e = _dbus_list_get_last (&parser->stack);
203 top_element_type (BusConfigParser *parser)
207 e = _dbus_list_get_last (&parser->stack);
216 merge_included (BusConfigParser *parser,
217 BusConfigParser *included,
222 if (included->user != NULL)
224 dbus_free (parser->user);
225 parser->user = included->user;
226 included->user = NULL;
229 if (included->bus_type != NULL)
231 dbus_free (parser->bus_type);
232 parser->bus_type = included->bus_type;
233 included->bus_type = NULL;
239 if (included->pidfile != NULL)
241 dbus_free (parser->pidfile);
242 parser->pidfile = included->pidfile;
243 included->pidfile = NULL;
246 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
247 _dbus_list_append_link (&parser->listen_on, link);
249 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
250 _dbus_list_append_link (&parser->mechanisms, link);
252 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
253 _dbus_list_append_link (&parser->service_dirs, link);
259 bus_config_parser_new (const DBusString *basedir)
261 BusConfigParser *parser;
263 parser = dbus_new0 (BusConfigParser, 1);
267 if (!_dbus_string_init (&parser->basedir))
273 if (((parser->policy = bus_policy_new ()) == NULL) ||
274 !_dbus_string_copy (basedir, 0, &parser->basedir, 0))
277 bus_policy_unref (parser->policy);
279 _dbus_string_free (&parser->basedir);
284 parser->refcount = 1;
290 bus_config_parser_ref (BusConfigParser *parser)
292 _dbus_assert (parser->refcount > 0);
294 parser->refcount += 1;
298 bus_config_parser_unref (BusConfigParser *parser)
300 _dbus_assert (parser->refcount > 0);
302 parser->refcount -= 1;
304 if (parser->refcount == 0)
306 while (parser->stack != NULL)
307 pop_element (parser);
309 dbus_free (parser->user);
310 dbus_free (parser->bus_type);
311 dbus_free (parser->pidfile);
313 _dbus_list_foreach (&parser->listen_on,
314 (DBusForeachFunction) dbus_free,
317 _dbus_list_clear (&parser->listen_on);
319 _dbus_list_foreach (&parser->service_dirs,
320 (DBusForeachFunction) dbus_free,
323 _dbus_list_clear (&parser->service_dirs);
325 _dbus_list_foreach (&parser->mechanisms,
326 (DBusForeachFunction) dbus_free,
329 _dbus_list_clear (&parser->mechanisms);
331 _dbus_string_free (&parser->basedir);
334 bus_policy_unref (parser->policy);
341 bus_config_parser_check_doctype (BusConfigParser *parser,
345 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
347 if (strcmp (doctype, "busconfig") != 0)
349 dbus_set_error (error,
351 "Configuration file has the wrong document type %s",
366 locate_attributes (BusConfigParser *parser,
367 const char *element_name,
368 const char **attribute_names,
369 const char **attribute_values,
371 const char *first_attribute_name,
372 const char **first_attribute_retloc,
380 LocateAttr attrs[MAX_ATTRS];
384 _dbus_assert (first_attribute_name != NULL);
385 _dbus_assert (first_attribute_retloc != NULL);
390 attrs[0].name = first_attribute_name;
391 attrs[0].retloc = first_attribute_retloc;
392 *first_attribute_retloc = NULL;
394 va_start (args, first_attribute_retloc);
396 name = va_arg (args, const char*);
397 retloc = va_arg (args, const char**);
401 _dbus_assert (retloc != NULL);
402 _dbus_assert (n_attrs < MAX_ATTRS);
404 attrs[n_attrs].name = name;
405 attrs[n_attrs].retloc = retloc;
409 name = va_arg (args, const char*);
410 retloc = va_arg (args, const char**);
419 while (attribute_names[i])
428 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
430 retloc = attrs[j].retloc;
434 dbus_set_error (error, DBUS_ERROR_FAILED,
435 "Attribute \"%s\" repeated twice on the same <%s> element",
436 attrs[j].name, element_name);
441 *retloc = attribute_values[i];
450 dbus_set_error (error, DBUS_ERROR_FAILED,
451 "Attribute \"%s\" is invalid on <%s> element in this context",
452 attribute_names[i], element_name);
465 check_no_attributes (BusConfigParser *parser,
466 const char *element_name,
467 const char **attribute_names,
468 const char **attribute_values,
471 if (attribute_names[0] != NULL)
473 dbus_set_error (error, DBUS_ERROR_FAILED,
474 "Attribute \"%s\" is invalid on <%s> element in this context",
475 attribute_names[0], element_name);
483 start_busconfig_child (BusConfigParser *parser,
484 const char *element_name,
485 const char **attribute_names,
486 const char **attribute_values,
489 if (strcmp (element_name, "user") == 0)
491 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
494 if (push_element (parser, ELEMENT_USER) == NULL)
502 else if (strcmp (element_name, "type") == 0)
504 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
507 if (push_element (parser, ELEMENT_TYPE) == NULL)
515 else if (strcmp (element_name, "fork") == 0)
517 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
520 if (push_element (parser, ELEMENT_FORK) == NULL)
530 else if (strcmp (element_name, "pidfile") == 0)
532 if (!check_no_attributes (parser, "pidfile", attribute_names, attribute_values, error))
535 if (push_element (parser, ELEMENT_PIDFILE) == NULL)
543 else if (strcmp (element_name, "listen") == 0)
545 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
548 if (push_element (parser, ELEMENT_LISTEN) == NULL)
556 else if (strcmp (element_name, "auth") == 0)
558 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
561 if (push_element (parser, ELEMENT_AUTH) == NULL)
569 else if (strcmp (element_name, "includedir") == 0)
571 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
574 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
582 else if (strcmp (element_name, "servicedir") == 0)
584 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
587 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
595 else if (strcmp (element_name, "include") == 0)
598 const char *ignore_missing;
600 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
606 e->d.include.ignore_missing = FALSE;
608 if (!locate_attributes (parser, "include",
612 "ignore_missing", &ignore_missing,
616 if (ignore_missing != NULL)
618 if (strcmp (ignore_missing, "yes") == 0)
619 e->d.include.ignore_missing = TRUE;
620 else if (strcmp (ignore_missing, "no") == 0)
621 e->d.include.ignore_missing = FALSE;
624 dbus_set_error (error, DBUS_ERROR_FAILED,
625 "ignore_missing attribute must have value \"yes\" or \"no\"");
632 else if (strcmp (element_name, "policy") == 0)
639 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
645 e->d.policy.type = POLICY_IGNORED;
647 if (!locate_attributes (parser, "policy",
657 if (((context && user) ||
658 (context && group)) ||
660 !(context || user || group))
662 dbus_set_error (error, DBUS_ERROR_FAILED,
663 "<policy> element must have exactly one of (context|user|group) attributes");
669 if (strcmp (context, "default") == 0)
671 e->d.policy.type = POLICY_DEFAULT;
673 else if (strcmp (context, "mandatory") == 0)
675 e->d.policy.type = POLICY_MANDATORY;
679 dbus_set_error (error, DBUS_ERROR_FAILED,
680 "context attribute on <policy> must have the value \"default\" or \"mandatory\", not \"%s\"",
685 else if (user != NULL)
688 _dbus_string_init_const (&username, user);
690 if (_dbus_get_user_id (&username,
691 &e->d.policy.gid_or_uid))
692 e->d.policy.type = POLICY_USER;
694 _dbus_warn ("Unknown username \"%s\" in message bus configuration file\n",
697 else if (group != NULL)
699 DBusString group_name;
700 _dbus_string_init_const (&group_name, group);
702 if (_dbus_get_group_id (&group_name,
703 &e->d.policy.gid_or_uid))
704 e->d.policy.type = POLICY_GROUP;
706 _dbus_warn ("Unknown group \"%s\" in message bus configuration file\n",
711 _dbus_assert_not_reached ("all <policy> attributes null and we didn't set error");
718 dbus_set_error (error, DBUS_ERROR_FAILED,
719 "Element <%s> not allowed inside <%s> in configuration file",
720 element_name, "busconfig");
726 append_rule_from_element (BusConfigParser *parser,
727 const char *element_name,
728 const char **attribute_names,
729 const char **attribute_values,
737 const char *receive_from;
742 if (!locate_attributes (parser, element_name,
750 "receive_from", &receive_from,
756 if (!(send || receive || own || send_to || receive_from ||
759 dbus_set_error (error, DBUS_ERROR_FAILED,
760 "Element <%s> must have one or more attributes",
765 if (((send && own) ||
767 (send && receive_from) ||
772 (receive && send_to) ||
774 (receive && group)) ||
777 (own && receive_from) ||
781 ((send_to && receive_from) ||
783 (send_to && group)) ||
785 ((receive_from && user) ||
786 (receive_from && group)) ||
790 dbus_set_error (error, DBUS_ERROR_FAILED,
791 "Invalid combination of attributes on element <%s>, "
792 "only send/send_to or receive/receive_from may be paired",
799 /* In BusPolicyRule, NULL represents wildcard.
800 * In the config file, '*' represents it.
802 #define IS_WILDCARD(str) ((str) && ((str)[0]) == '*' && ((str)[1]) == '\0')
806 rule = bus_policy_rule_new (BUS_POLICY_RULE_SEND, allow);
810 if (IS_WILDCARD (send))
812 if (IS_WILDCARD (send_to))
815 rule->d.send.message_name = _dbus_strdup (send);
816 rule->d.send.destination = _dbus_strdup (send_to);
817 if (send && rule->d.send.message_name == NULL)
819 if (send_to && rule->d.send.destination == NULL)
822 else if (receive || receive_from)
824 rule = bus_policy_rule_new (BUS_POLICY_RULE_RECEIVE, allow);
828 if (IS_WILDCARD (receive))
831 if (IS_WILDCARD (receive_from))
834 rule->d.receive.message_name = _dbus_strdup (receive);
835 rule->d.receive.origin = _dbus_strdup (receive_from);
836 if (receive && rule->d.receive.message_name == NULL)
838 if (receive_from && rule->d.receive.origin == NULL)
843 rule = bus_policy_rule_new (BUS_POLICY_RULE_OWN, allow);
847 if (IS_WILDCARD (own))
850 rule->d.own.service_name = _dbus_strdup (own);
851 if (own && rule->d.own.service_name == NULL)
856 if (IS_WILDCARD (user))
858 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
862 /* FIXME the wildcard needs storing in the rule somehow */
869 _dbus_string_init_const (&username, user);
871 if (_dbus_get_user_id (&username, &uid))
873 rule = bus_policy_rule_new (BUS_POLICY_RULE_USER, allow);
877 rule->d.user.user = _dbus_strdup (user);
878 if (rule->d.user.user == NULL)
880 rule->d.user.uid = uid;
884 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
891 if (IS_WILDCARD (group))
893 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
897 /* FIXME the wildcard needs storing in the rule somehow */
901 DBusString groupname;
904 _dbus_string_init_const (&groupname, group);
906 if (_dbus_get_user_id (&groupname, &gid))
908 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
912 rule->d.group.group = _dbus_strdup (group);
913 if (rule->d.group.group == NULL)
915 rule->d.group.gid = gid;
919 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
920 group, element_name);
925 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
931 pe = peek_element (parser);
932 _dbus_assert (pe != NULL);
933 _dbus_assert (pe->type == ELEMENT_POLICY);
935 switch (pe->d.policy.type)
938 /* drop the rule on the floor */
942 if (!bus_policy_append_default_rule (parser->policy, rule))
945 case POLICY_MANDATORY:
946 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
950 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
952 dbus_set_error (error, DBUS_ERROR_FAILED,
953 "<%s> rule cannot be per-user because it has bus-global semantics",
958 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
963 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
965 dbus_set_error (error, DBUS_ERROR_FAILED,
966 "<%s> rule cannot be per-group because it has bus-global semantics",
971 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
977 bus_policy_rule_unref (rule);
987 bus_policy_rule_unref (rule);
992 start_policy_child (BusConfigParser *parser,
993 const char *element_name,
994 const char **attribute_names,
995 const char **attribute_values,
998 if (strcmp (element_name, "allow") == 0)
1000 if (!append_rule_from_element (parser, element_name,
1001 attribute_names, attribute_values,
1005 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1007 BUS_SET_OOM (error);
1013 else if (strcmp (element_name, "deny") == 0)
1015 if (!append_rule_from_element (parser, element_name,
1016 attribute_names, attribute_values,
1020 if (push_element (parser, ELEMENT_DENY) == NULL)
1022 BUS_SET_OOM (error);
1030 dbus_set_error (error, DBUS_ERROR_FAILED,
1031 "Element <%s> not allowed inside <%s> in configuration file",
1032 element_name, "policy");
1038 bus_config_parser_start_element (BusConfigParser *parser,
1039 const char *element_name,
1040 const char **attribute_names,
1041 const char **attribute_values,
1046 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1048 /* printf ("START: %s\n", element_name); */
1050 t = top_element_type (parser);
1052 if (t == ELEMENT_NONE)
1054 if (strcmp (element_name, "busconfig") == 0)
1056 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1059 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1061 BUS_SET_OOM (error);
1069 dbus_set_error (error, DBUS_ERROR_FAILED,
1070 "Unknown element <%s> at root of configuration file",
1075 else if (t == ELEMENT_BUSCONFIG)
1077 return start_busconfig_child (parser, element_name,
1078 attribute_names, attribute_values,
1081 else if (t == ELEMENT_POLICY)
1083 return start_policy_child (parser, element_name,
1084 attribute_names, attribute_values,
1089 dbus_set_error (error, DBUS_ERROR_FAILED,
1090 "Element <%s> is not allowed in this context",
1097 bus_config_parser_end_element (BusConfigParser *parser,
1098 const char *element_name,
1105 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1107 /* printf ("END: %s\n", element_name); */
1109 t = top_element_type (parser);
1111 if (t == ELEMENT_NONE)
1113 /* should probably be an assertion failure but
1114 * being paranoid about XML parsers
1116 dbus_set_error (error, DBUS_ERROR_FAILED,
1117 "XML parser ended element with no element on the stack");
1121 n = element_type_to_name (t);
1122 _dbus_assert (n != NULL);
1123 if (strcmp (n, element_name) != 0)
1125 /* should probably be an assertion failure but
1126 * being paranoid about XML parsers
1128 dbus_set_error (error, DBUS_ERROR_FAILED,
1129 "XML element <%s> ended but topmost element on the stack was <%s>",
1134 e = peek_element (parser);
1135 _dbus_assert (e != NULL);
1140 _dbus_assert_not_reached ("element in stack has no type");
1143 case ELEMENT_INCLUDE:
1146 case ELEMENT_LISTEN:
1147 case ELEMENT_PIDFILE:
1149 case ELEMENT_SERVICEDIR:
1150 case ELEMENT_INCLUDEDIR:
1151 if (!e->had_content)
1153 dbus_set_error (error, DBUS_ERROR_FAILED,
1154 "XML element <%s> was expected to have content inside it",
1155 element_type_to_name (e->type));
1160 case ELEMENT_BUSCONFIG:
1161 case ELEMENT_POLICY:
1169 pop_element (parser);
1175 all_whitespace (const DBusString *str)
1179 _dbus_string_skip_white (str, 0, &i);
1181 return i == _dbus_string_get_length (str);
1185 make_full_path (const DBusString *basedir,
1186 const DBusString *filename,
1187 DBusString *full_path)
1189 if (_dbus_path_is_absolute (filename))
1191 return _dbus_string_copy (filename, 0, full_path, 0);
1195 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1198 if (!_dbus_concat_dir_and_file (full_path, filename))
1206 include_file (BusConfigParser *parser,
1207 const DBusString *filename,
1208 dbus_bool_t ignore_missing,
1211 /* FIXME good test case for this would load each config file in the
1212 * test suite both alone, and as an include, and check
1213 * that the result is the same
1215 BusConfigParser *included;
1216 DBusError tmp_error;
1218 dbus_error_init (&tmp_error);
1219 included = bus_config_load (filename, &tmp_error);
1220 if (included == NULL)
1222 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1224 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1227 dbus_error_free (&tmp_error);
1232 dbus_move_error (&tmp_error, error);
1238 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1240 if (!merge_included (parser, included, error))
1242 bus_config_parser_unref (included);
1246 bus_config_parser_unref (included);
1252 include_dir (BusConfigParser *parser,
1253 const DBusString *dirname,
1256 DBusString filename;
1258 DBusError tmp_error;
1261 if (!_dbus_string_init (&filename))
1263 BUS_SET_OOM (error);
1269 dir = _dbus_directory_open (dirname, error);
1274 dbus_error_init (&tmp_error);
1275 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1277 DBusString full_path;
1279 if (!_dbus_string_init (&full_path))
1281 BUS_SET_OOM (error);
1285 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1287 BUS_SET_OOM (error);
1288 _dbus_string_free (&full_path);
1292 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1294 BUS_SET_OOM (error);
1295 _dbus_string_free (&full_path);
1299 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1301 if (!include_file (parser, &full_path, TRUE, error))
1303 _dbus_string_free (&full_path);
1308 _dbus_string_free (&full_path);
1311 if (dbus_error_is_set (&tmp_error))
1313 dbus_move_error (&tmp_error, error);
1320 _dbus_string_free (&filename);
1323 _dbus_directory_close (dir);
1329 bus_config_parser_content (BusConfigParser *parser,
1330 const DBusString *content,
1335 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1341 _dbus_string_get_const_data (content, &c_str);
1343 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1347 e = peek_element (parser);
1350 dbus_set_error (error, DBUS_ERROR_FAILED,
1351 "Text content outside of any XML element in configuration file");
1354 else if (e->had_content)
1356 _dbus_assert_not_reached ("Element had multiple content blocks");
1360 switch (top_element_type (parser))
1363 _dbus_assert_not_reached ("element at top of stack has no type");
1366 case ELEMENT_BUSCONFIG:
1367 case ELEMENT_POLICY:
1372 if (all_whitespace (content))
1376 dbus_set_error (error, DBUS_ERROR_FAILED,
1377 "No text content expected inside XML element %s in configuration file",
1378 element_type_to_name (top_element_type (parser)));
1382 case ELEMENT_PIDFILE:
1386 e->had_content = TRUE;
1388 if (!_dbus_string_copy_data (content, &s))
1391 dbus_free (parser->pidfile);
1392 parser->pidfile = s;
1396 case ELEMENT_INCLUDE:
1398 DBusString full_path;
1400 e->had_content = TRUE;
1402 if (!_dbus_string_init (&full_path))
1405 if (!make_full_path (&parser->basedir, content, &full_path))
1407 _dbus_string_free (&full_path);
1411 if (!include_file (parser, &full_path,
1412 e->d.include.ignore_missing, error))
1414 _dbus_string_free (&full_path);
1418 _dbus_string_free (&full_path);
1422 case ELEMENT_INCLUDEDIR:
1424 DBusString full_path;
1426 e->had_content = TRUE;
1428 if (!_dbus_string_init (&full_path))
1431 if (!make_full_path (&parser->basedir, content, &full_path))
1433 _dbus_string_free (&full_path);
1437 if (!include_dir (parser, &full_path, error))
1439 _dbus_string_free (&full_path);
1443 _dbus_string_free (&full_path);
1451 e->had_content = TRUE;
1453 if (!_dbus_string_copy_data (content, &s))
1456 dbus_free (parser->user);
1465 e->had_content = TRUE;
1467 if (!_dbus_string_copy_data (content, &s))
1470 dbus_free (parser->bus_type);
1471 parser->bus_type = s;
1475 case ELEMENT_LISTEN:
1479 e->had_content = TRUE;
1481 if (!_dbus_string_copy_data (content, &s))
1484 if (!_dbus_list_append (&parser->listen_on,
1497 e->had_content = TRUE;
1499 if (!_dbus_string_copy_data (content, &s))
1502 if (!_dbus_list_append (&parser->mechanisms,
1511 case ELEMENT_SERVICEDIR:
1514 DBusString full_path;
1516 e->had_content = TRUE;
1518 if (!_dbus_string_init (&full_path))
1521 if (!make_full_path (&parser->basedir, content, &full_path))
1523 _dbus_string_free (&full_path);
1527 if (!_dbus_string_copy_data (&full_path, &s))
1529 _dbus_string_free (&full_path);
1533 if (!_dbus_list_append (&parser->service_dirs, s))
1535 _dbus_string_free (&full_path);
1540 _dbus_string_free (&full_path);
1545 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1549 BUS_SET_OOM (error);
1554 bus_config_parser_finished (BusConfigParser *parser,
1557 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1559 if (parser->stack != NULL)
1561 dbus_set_error (error, DBUS_ERROR_FAILED,
1562 "Element <%s> was not closed in configuration file",
1563 element_type_to_name (top_element_type (parser)));
1568 if (parser->listen_on == NULL)
1570 dbus_set_error (error, DBUS_ERROR_FAILED,
1571 "Configuration file needs one or more <listen> elements giving addresses");
1579 bus_config_parser_get_user (BusConfigParser *parser)
1581 return parser->user;
1585 bus_config_parser_get_type (BusConfigParser *parser)
1587 return parser->bus_type;
1591 bus_config_parser_get_addresses (BusConfigParser *parser)
1593 return &parser->listen_on;
1597 bus_config_parser_get_mechanisms (BusConfigParser *parser)
1599 return &parser->mechanisms;
1603 bus_config_parser_get_service_dirs (BusConfigParser *parser)
1605 return &parser->service_dirs;
1609 bus_config_parser_get_fork (BusConfigParser *parser)
1611 return parser->fork;
1615 bus_config_parser_get_pidfile (BusConfigParser *parser)
1617 return parser->pidfile;
1621 bus_config_parser_steal_policy (BusConfigParser *parser)
1625 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
1627 policy = parser->policy;
1629 parser->policy = NULL;
1634 #ifdef DBUS_BUILD_TESTS
1645 do_load (const DBusString *full_path,
1647 dbus_bool_t oom_possible)
1649 BusConfigParser *parser;
1652 dbus_error_init (&error);
1654 parser = bus_config_load (full_path, &error);
1657 _DBUS_ASSERT_ERROR_IS_SET (&error);
1660 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1662 _dbus_verbose ("Failed to load valid file due to OOM\n");
1663 dbus_error_free (&error);
1666 else if (validity == VALID)
1668 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
1671 dbus_error_free (&error);
1676 dbus_error_free (&error);
1682 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
1684 bus_config_parser_unref (parser);
1686 if (validity == INVALID)
1688 _dbus_warn ("Accepted invalid file\n");
1698 const DBusString *full_path;
1703 check_loader_oom_func (void *data)
1705 LoaderOomData *d = data;
1707 return do_load (d->full_path, d->validity, TRUE);
1711 process_test_subdir (const DBusString *test_base_dir,
1715 DBusString test_directory;
1716 DBusString filename;
1724 if (!_dbus_string_init (&test_directory))
1725 _dbus_assert_not_reached ("didn't allocate test_directory\n");
1727 _dbus_string_init_const (&filename, subdir);
1729 if (!_dbus_string_copy (test_base_dir, 0,
1730 &test_directory, 0))
1731 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
1733 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
1734 _dbus_assert_not_reached ("couldn't allocate full path");
1736 _dbus_string_free (&filename);
1737 if (!_dbus_string_init (&filename))
1738 _dbus_assert_not_reached ("didn't allocate filename string\n");
1740 dbus_error_init (&error);
1741 dir = _dbus_directory_open (&test_directory, &error);
1744 _dbus_warn ("Could not open %s: %s\n",
1745 _dbus_string_get_const_data (&test_directory),
1747 dbus_error_free (&error);
1751 printf ("Testing:\n");
1754 while (_dbus_directory_get_next_file (dir, &filename, &error))
1756 DBusString full_path;
1759 if (!_dbus_string_init (&full_path))
1760 _dbus_assert_not_reached ("couldn't init string");
1762 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
1763 _dbus_assert_not_reached ("couldn't copy dir to full_path");
1765 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1766 _dbus_assert_not_reached ("couldn't concat file to dir");
1768 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
1770 _dbus_verbose ("Skipping non-.conf file %s\n",
1771 _dbus_string_get_const_data (&filename));
1772 _dbus_string_free (&full_path);
1776 printf (" %s\n", _dbus_string_get_const_data (&filename));
1778 _dbus_verbose (" expecting %s\n",
1779 validity == VALID ? "valid" :
1780 (validity == INVALID ? "invalid" :
1781 (validity == UNKNOWN ? "unknown" : "???")));
1783 d.full_path = &full_path;
1784 d.validity = validity;
1785 if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d))
1786 _dbus_assert_not_reached ("test failed");
1788 _dbus_string_free (&full_path);
1791 if (dbus_error_is_set (&error))
1793 _dbus_warn ("Could not get next file in %s: %s\n",
1794 _dbus_string_get_const_data (&test_directory),
1796 dbus_error_free (&error);
1805 _dbus_directory_close (dir);
1806 _dbus_string_free (&test_directory);
1807 _dbus_string_free (&filename);
1813 bus_config_parser_test (const DBusString *test_data_dir)
1815 if (test_data_dir == NULL ||
1816 _dbus_string_get_length (test_data_dir) == 0)
1818 printf ("No test data\n");
1822 if (!process_test_subdir (test_data_dir, "valid-config-files", VALID))
1828 #endif /* DBUS_BUILD_TESTS */