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 rule->d.user.uid = DBUS_UID_UNSET;
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.uid = uid;
881 _dbus_warn ("Unknown username \"%s\" on element <%s>\n",
888 if (IS_WILDCARD (group))
890 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
894 rule->d.group.gid = DBUS_GID_UNSET;
898 DBusString groupname;
901 _dbus_string_init_const (&groupname, group);
903 if (_dbus_get_user_id (&groupname, &gid))
905 rule = bus_policy_rule_new (BUS_POLICY_RULE_GROUP, allow);
909 rule->d.group.gid = gid;
913 _dbus_warn ("Unknown group \"%s\" on element <%s>\n",
914 group, element_name);
919 _dbus_assert_not_reached ("Did not handle some combination of attributes on <allow> or <deny>");
925 pe = peek_element (parser);
926 _dbus_assert (pe != NULL);
927 _dbus_assert (pe->type == ELEMENT_POLICY);
929 switch (pe->d.policy.type)
932 /* drop the rule on the floor */
936 if (!bus_policy_append_default_rule (parser->policy, rule))
939 case POLICY_MANDATORY:
940 if (!bus_policy_append_mandatory_rule (parser->policy, rule))
944 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
946 dbus_set_error (error, DBUS_ERROR_FAILED,
947 "<%s> rule cannot be per-user because it has bus-global semantics",
952 if (!bus_policy_append_user_rule (parser->policy, pe->d.policy.gid_or_uid,
957 if (!BUS_POLICY_RULE_IS_PER_CLIENT (rule))
959 dbus_set_error (error, DBUS_ERROR_FAILED,
960 "<%s> rule cannot be per-group because it has bus-global semantics",
965 if (!bus_policy_append_group_rule (parser->policy, pe->d.policy.gid_or_uid,
971 bus_policy_rule_unref (rule);
981 bus_policy_rule_unref (rule);
986 start_policy_child (BusConfigParser *parser,
987 const char *element_name,
988 const char **attribute_names,
989 const char **attribute_values,
992 if (strcmp (element_name, "allow") == 0)
994 if (!append_rule_from_element (parser, element_name,
995 attribute_names, attribute_values,
999 if (push_element (parser, ELEMENT_ALLOW) == NULL)
1001 BUS_SET_OOM (error);
1007 else if (strcmp (element_name, "deny") == 0)
1009 if (!append_rule_from_element (parser, element_name,
1010 attribute_names, attribute_values,
1014 if (push_element (parser, ELEMENT_DENY) == NULL)
1016 BUS_SET_OOM (error);
1024 dbus_set_error (error, DBUS_ERROR_FAILED,
1025 "Element <%s> not allowed inside <%s> in configuration file",
1026 element_name, "policy");
1032 bus_config_parser_start_element (BusConfigParser *parser,
1033 const char *element_name,
1034 const char **attribute_names,
1035 const char **attribute_values,
1040 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1042 /* printf ("START: %s\n", element_name); */
1044 t = top_element_type (parser);
1046 if (t == ELEMENT_NONE)
1048 if (strcmp (element_name, "busconfig") == 0)
1050 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
1053 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
1055 BUS_SET_OOM (error);
1063 dbus_set_error (error, DBUS_ERROR_FAILED,
1064 "Unknown element <%s> at root of configuration file",
1069 else if (t == ELEMENT_BUSCONFIG)
1071 return start_busconfig_child (parser, element_name,
1072 attribute_names, attribute_values,
1075 else if (t == ELEMENT_POLICY)
1077 return start_policy_child (parser, element_name,
1078 attribute_names, attribute_values,
1083 dbus_set_error (error, DBUS_ERROR_FAILED,
1084 "Element <%s> is not allowed in this context",
1091 bus_config_parser_end_element (BusConfigParser *parser,
1092 const char *element_name,
1099 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1101 /* printf ("END: %s\n", element_name); */
1103 t = top_element_type (parser);
1105 if (t == ELEMENT_NONE)
1107 /* should probably be an assertion failure but
1108 * being paranoid about XML parsers
1110 dbus_set_error (error, DBUS_ERROR_FAILED,
1111 "XML parser ended element with no element on the stack");
1115 n = element_type_to_name (t);
1116 _dbus_assert (n != NULL);
1117 if (strcmp (n, element_name) != 0)
1119 /* should probably be an assertion failure but
1120 * being paranoid about XML parsers
1122 dbus_set_error (error, DBUS_ERROR_FAILED,
1123 "XML element <%s> ended but topmost element on the stack was <%s>",
1128 e = peek_element (parser);
1129 _dbus_assert (e != NULL);
1134 _dbus_assert_not_reached ("element in stack has no type");
1137 case ELEMENT_INCLUDE:
1140 case ELEMENT_LISTEN:
1141 case ELEMENT_PIDFILE:
1143 case ELEMENT_SERVICEDIR:
1144 case ELEMENT_INCLUDEDIR:
1145 if (!e->had_content)
1147 dbus_set_error (error, DBUS_ERROR_FAILED,
1148 "XML element <%s> was expected to have content inside it",
1149 element_type_to_name (e->type));
1154 case ELEMENT_BUSCONFIG:
1155 case ELEMENT_POLICY:
1163 pop_element (parser);
1169 all_whitespace (const DBusString *str)
1173 _dbus_string_skip_white (str, 0, &i);
1175 return i == _dbus_string_get_length (str);
1179 make_full_path (const DBusString *basedir,
1180 const DBusString *filename,
1181 DBusString *full_path)
1183 if (_dbus_path_is_absolute (filename))
1185 return _dbus_string_copy (filename, 0, full_path, 0);
1189 if (!_dbus_string_copy (basedir, 0, full_path, 0))
1192 if (!_dbus_concat_dir_and_file (full_path, filename))
1200 include_file (BusConfigParser *parser,
1201 const DBusString *filename,
1202 dbus_bool_t ignore_missing,
1205 /* FIXME good test case for this would load each config file in the
1206 * test suite both alone, and as an include, and check
1207 * that the result is the same
1209 BusConfigParser *included;
1210 DBusError tmp_error;
1212 dbus_error_init (&tmp_error);
1213 included = bus_config_load (filename, &tmp_error);
1214 if (included == NULL)
1216 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1218 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
1221 dbus_error_free (&tmp_error);
1226 dbus_move_error (&tmp_error, error);
1232 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1234 if (!merge_included (parser, included, error))
1236 bus_config_parser_unref (included);
1240 bus_config_parser_unref (included);
1246 include_dir (BusConfigParser *parser,
1247 const DBusString *dirname,
1250 DBusString filename;
1252 DBusError tmp_error;
1255 if (!_dbus_string_init (&filename))
1257 BUS_SET_OOM (error);
1263 dir = _dbus_directory_open (dirname, error);
1268 dbus_error_init (&tmp_error);
1269 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
1271 DBusString full_path;
1273 if (!_dbus_string_init (&full_path))
1275 BUS_SET_OOM (error);
1279 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
1281 BUS_SET_OOM (error);
1282 _dbus_string_free (&full_path);
1286 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1288 BUS_SET_OOM (error);
1289 _dbus_string_free (&full_path);
1293 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
1295 if (!include_file (parser, &full_path, TRUE, error))
1297 _dbus_string_free (&full_path);
1302 _dbus_string_free (&full_path);
1305 if (dbus_error_is_set (&tmp_error))
1307 dbus_move_error (&tmp_error, error);
1314 _dbus_string_free (&filename);
1317 _dbus_directory_close (dir);
1323 bus_config_parser_content (BusConfigParser *parser,
1324 const DBusString *content,
1329 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1335 _dbus_string_get_const_data (content, &c_str);
1337 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
1341 e = peek_element (parser);
1344 dbus_set_error (error, DBUS_ERROR_FAILED,
1345 "Text content outside of any XML element in configuration file");
1348 else if (e->had_content)
1350 _dbus_assert_not_reached ("Element had multiple content blocks");
1354 switch (top_element_type (parser))
1357 _dbus_assert_not_reached ("element at top of stack has no type");
1360 case ELEMENT_BUSCONFIG:
1361 case ELEMENT_POLICY:
1366 if (all_whitespace (content))
1370 dbus_set_error (error, DBUS_ERROR_FAILED,
1371 "No text content expected inside XML element %s in configuration file",
1372 element_type_to_name (top_element_type (parser)));
1376 case ELEMENT_PIDFILE:
1380 e->had_content = TRUE;
1382 if (!_dbus_string_copy_data (content, &s))
1385 dbus_free (parser->pidfile);
1386 parser->pidfile = s;
1390 case ELEMENT_INCLUDE:
1392 DBusString full_path;
1394 e->had_content = TRUE;
1396 if (!_dbus_string_init (&full_path))
1399 if (!make_full_path (&parser->basedir, content, &full_path))
1401 _dbus_string_free (&full_path);
1405 if (!include_file (parser, &full_path,
1406 e->d.include.ignore_missing, error))
1408 _dbus_string_free (&full_path);
1412 _dbus_string_free (&full_path);
1416 case ELEMENT_INCLUDEDIR:
1418 DBusString full_path;
1420 e->had_content = TRUE;
1422 if (!_dbus_string_init (&full_path))
1425 if (!make_full_path (&parser->basedir, content, &full_path))
1427 _dbus_string_free (&full_path);
1431 if (!include_dir (parser, &full_path, error))
1433 _dbus_string_free (&full_path);
1437 _dbus_string_free (&full_path);
1445 e->had_content = TRUE;
1447 if (!_dbus_string_copy_data (content, &s))
1450 dbus_free (parser->user);
1459 e->had_content = TRUE;
1461 if (!_dbus_string_copy_data (content, &s))
1464 dbus_free (parser->bus_type);
1465 parser->bus_type = s;
1469 case ELEMENT_LISTEN:
1473 e->had_content = TRUE;
1475 if (!_dbus_string_copy_data (content, &s))
1478 if (!_dbus_list_append (&parser->listen_on,
1491 e->had_content = TRUE;
1493 if (!_dbus_string_copy_data (content, &s))
1496 if (!_dbus_list_append (&parser->mechanisms,
1505 case ELEMENT_SERVICEDIR:
1508 DBusString full_path;
1510 e->had_content = TRUE;
1512 if (!_dbus_string_init (&full_path))
1515 if (!make_full_path (&parser->basedir, content, &full_path))
1517 _dbus_string_free (&full_path);
1521 if (!_dbus_string_copy_data (&full_path, &s))
1523 _dbus_string_free (&full_path);
1527 if (!_dbus_list_append (&parser->service_dirs, s))
1529 _dbus_string_free (&full_path);
1534 _dbus_string_free (&full_path);
1539 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1543 BUS_SET_OOM (error);
1548 bus_config_parser_finished (BusConfigParser *parser,
1551 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1553 if (parser->stack != NULL)
1555 dbus_set_error (error, DBUS_ERROR_FAILED,
1556 "Element <%s> was not closed in configuration file",
1557 element_type_to_name (top_element_type (parser)));
1562 if (parser->listen_on == NULL)
1564 dbus_set_error (error, DBUS_ERROR_FAILED,
1565 "Configuration file needs one or more <listen> elements giving addresses");
1573 bus_config_parser_get_user (BusConfigParser *parser)
1575 return parser->user;
1579 bus_config_parser_get_type (BusConfigParser *parser)
1581 return parser->bus_type;
1585 bus_config_parser_get_addresses (BusConfigParser *parser)
1587 return &parser->listen_on;
1591 bus_config_parser_get_mechanisms (BusConfigParser *parser)
1593 return &parser->mechanisms;
1597 bus_config_parser_get_service_dirs (BusConfigParser *parser)
1599 return &parser->service_dirs;
1603 bus_config_parser_get_fork (BusConfigParser *parser)
1605 return parser->fork;
1609 bus_config_parser_get_pidfile (BusConfigParser *parser)
1611 return parser->pidfile;
1615 bus_config_parser_steal_policy (BusConfigParser *parser)
1619 _dbus_assert (parser->policy != NULL); /* can only steal the policy 1 time */
1621 policy = parser->policy;
1623 parser->policy = NULL;
1628 #ifdef DBUS_BUILD_TESTS
1639 do_load (const DBusString *full_path,
1641 dbus_bool_t oom_possible)
1643 BusConfigParser *parser;
1646 dbus_error_init (&error);
1648 parser = bus_config_load (full_path, &error);
1651 _DBUS_ASSERT_ERROR_IS_SET (&error);
1654 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1656 _dbus_verbose ("Failed to load valid file due to OOM\n");
1657 dbus_error_free (&error);
1660 else if (validity == VALID)
1662 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
1665 dbus_error_free (&error);
1670 dbus_error_free (&error);
1676 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
1678 bus_config_parser_unref (parser);
1680 if (validity == INVALID)
1682 _dbus_warn ("Accepted invalid file\n");
1692 const DBusString *full_path;
1697 check_loader_oom_func (void *data)
1699 LoaderOomData *d = data;
1701 return do_load (d->full_path, d->validity, TRUE);
1705 process_test_subdir (const DBusString *test_base_dir,
1709 DBusString test_directory;
1710 DBusString filename;
1718 if (!_dbus_string_init (&test_directory))
1719 _dbus_assert_not_reached ("didn't allocate test_directory\n");
1721 _dbus_string_init_const (&filename, subdir);
1723 if (!_dbus_string_copy (test_base_dir, 0,
1724 &test_directory, 0))
1725 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
1727 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
1728 _dbus_assert_not_reached ("couldn't allocate full path");
1730 _dbus_string_free (&filename);
1731 if (!_dbus_string_init (&filename))
1732 _dbus_assert_not_reached ("didn't allocate filename string\n");
1734 dbus_error_init (&error);
1735 dir = _dbus_directory_open (&test_directory, &error);
1738 _dbus_warn ("Could not open %s: %s\n",
1739 _dbus_string_get_const_data (&test_directory),
1741 dbus_error_free (&error);
1745 printf ("Testing:\n");
1748 while (_dbus_directory_get_next_file (dir, &filename, &error))
1750 DBusString full_path;
1753 if (!_dbus_string_init (&full_path))
1754 _dbus_assert_not_reached ("couldn't init string");
1756 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
1757 _dbus_assert_not_reached ("couldn't copy dir to full_path");
1759 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1760 _dbus_assert_not_reached ("couldn't concat file to dir");
1762 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
1764 _dbus_verbose ("Skipping non-.conf file %s\n",
1765 _dbus_string_get_const_data (&filename));
1766 _dbus_string_free (&full_path);
1770 printf (" %s\n", _dbus_string_get_const_data (&filename));
1772 _dbus_verbose (" expecting %s\n",
1773 validity == VALID ? "valid" :
1774 (validity == INVALID ? "invalid" :
1775 (validity == UNKNOWN ? "unknown" : "???")));
1777 d.full_path = &full_path;
1778 d.validity = validity;
1779 if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d))
1780 _dbus_assert_not_reached ("test failed");
1782 _dbus_string_free (&full_path);
1785 if (dbus_error_is_set (&error))
1787 _dbus_warn ("Could not get next file in %s: %s\n",
1788 _dbus_string_get_const_data (&test_directory),
1790 dbus_error_free (&error);
1799 _dbus_directory_close (dir);
1800 _dbus_string_free (&test_directory);
1801 _dbus_string_free (&filename);
1807 bus_config_parser_test (const DBusString *test_data_dir)
1809 if (test_data_dir == NULL ||
1810 _dbus_string_get_length (test_data_dir) == 0)
1812 printf ("No test data\n");
1816 if (!process_test_subdir (test_data_dir, "valid-config-files", VALID))
1822 #endif /* DBUS_BUILD_TESTS */