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"
26 #include <dbus/dbus-list.h>
27 #include <dbus/dbus-internals.h>
52 unsigned int had_content : 1;
58 unsigned int ignore_missing : 1;
78 struct BusConfigParser
82 DBusString basedir; /**< Directory we resolve paths relative to */
84 DBusList *stack; /**< stack of Element */
86 char *user; /**< user to run as */
88 char *bus_type; /**< Message bus type */
90 DBusList *listen_on; /**< List of addresses to listen to */
92 DBusList *mechanisms; /**< Auth mechanisms */
94 DBusList *service_dirs; /**< Directories to look for services in */
96 unsigned int fork : 1; /**< TRUE to fork into daemon mode */
100 element_type_to_name (ElementType type)
106 case ELEMENT_BUSCONFIG:
108 case ELEMENT_INCLUDE:
126 case ELEMENT_SERVICEDIR:
128 case ELEMENT_INCLUDEDIR:
134 _dbus_assert_not_reached ("bad element type");
140 push_element (BusConfigParser *parser,
145 _dbus_assert (type != ELEMENT_NONE);
147 e = dbus_new0 (Element, 1);
151 if (!_dbus_list_append (&parser->stack, e))
163 element_free (Element *e)
170 pop_element (BusConfigParser *parser)
174 e = _dbus_list_pop_last (&parser->stack);
180 peek_element (BusConfigParser *parser)
184 e = _dbus_list_get_last (&parser->stack);
190 top_element_type (BusConfigParser *parser)
194 e = _dbus_list_get_last (&parser->stack);
203 merge_included (BusConfigParser *parser,
204 BusConfigParser *included,
209 if (included->user != NULL)
211 dbus_free (parser->user);
212 parser->user = included->user;
213 included->user = NULL;
216 if (included->bus_type != NULL)
218 dbus_free (parser->bus_type);
219 parser->bus_type = included->bus_type;
220 included->bus_type = NULL;
226 while ((link = _dbus_list_pop_first_link (&included->listen_on)))
227 _dbus_list_append_link (&parser->listen_on, link);
229 while ((link = _dbus_list_pop_first_link (&included->mechanisms)))
230 _dbus_list_append_link (&parser->mechanisms, link);
232 while ((link = _dbus_list_pop_first_link (&included->service_dirs)))
233 _dbus_list_append_link (&parser->service_dirs, link);
239 bus_config_parser_new (const DBusString *basedir)
241 BusConfigParser *parser;
243 parser = dbus_new0 (BusConfigParser, 1);
247 if (!_dbus_string_init (&parser->basedir))
253 if (!_dbus_string_copy (basedir, 0, &parser->basedir, 0))
255 _dbus_string_free (&parser->basedir);
260 parser->refcount = 1;
266 bus_config_parser_ref (BusConfigParser *parser)
268 _dbus_assert (parser->refcount > 0);
270 parser->refcount += 1;
274 bus_config_parser_unref (BusConfigParser *parser)
276 _dbus_assert (parser->refcount > 0);
278 parser->refcount -= 1;
280 if (parser->refcount == 0)
282 while (parser->stack != NULL)
283 pop_element (parser);
285 dbus_free (parser->user);
286 dbus_free (parser->bus_type);
288 _dbus_list_foreach (&parser->listen_on,
289 (DBusForeachFunction) dbus_free,
292 _dbus_list_clear (&parser->listen_on);
294 _dbus_list_foreach (&parser->service_dirs,
295 (DBusForeachFunction) dbus_free,
298 _dbus_list_clear (&parser->service_dirs);
300 _dbus_list_foreach (&parser->mechanisms,
301 (DBusForeachFunction) dbus_free,
304 _dbus_list_clear (&parser->mechanisms);
306 _dbus_string_free (&parser->basedir);
313 bus_config_parser_check_doctype (BusConfigParser *parser,
317 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
319 if (strcmp (doctype, "busconfig") != 0)
321 dbus_set_error (error,
323 "Configuration file has the wrong document type %s",
338 locate_attributes (BusConfigParser *parser,
339 const char *element_name,
340 const char **attribute_names,
341 const char **attribute_values,
343 const char *first_attribute_name,
344 const char **first_attribute_retloc,
352 LocateAttr attrs[MAX_ATTRS];
356 _dbus_assert (first_attribute_name != NULL);
357 _dbus_assert (first_attribute_retloc != NULL);
362 attrs[0].name = first_attribute_name;
363 attrs[0].retloc = first_attribute_retloc;
364 *first_attribute_retloc = NULL;
366 va_start (args, first_attribute_retloc);
368 name = va_arg (args, const char*);
369 retloc = va_arg (args, const char**);
373 _dbus_assert (retloc != NULL);
374 _dbus_assert (n_attrs < MAX_ATTRS);
376 attrs[n_attrs].name = name;
377 attrs[n_attrs].retloc = retloc;
381 name = va_arg (args, const char*);
382 retloc = va_arg (args, const char**);
391 while (attribute_names[i])
400 if (strcmp (attrs[j].name, attribute_names[i]) == 0)
402 retloc = attrs[j].retloc;
406 dbus_set_error (error, DBUS_ERROR_FAILED,
407 "Attribute \"%s\" repeated twice on the same <%s> element",
408 attrs[j].name, element_name);
413 *retloc = attribute_values[i];
422 dbus_set_error (error, DBUS_ERROR_FAILED,
423 "Attribute \"%s\" is invalid on <%s> element in this context",
424 attribute_names[i], element_name);
437 check_no_attributes (BusConfigParser *parser,
438 const char *element_name,
439 const char **attribute_names,
440 const char **attribute_values,
443 if (attribute_names[0] != NULL)
445 dbus_set_error (error, DBUS_ERROR_FAILED,
446 "Attribute \"%s\" is invalid on <%s> element in this context",
447 attribute_names[0], element_name);
455 start_busconfig_child (BusConfigParser *parser,
456 const char *element_name,
457 const char **attribute_names,
458 const char **attribute_values,
461 if (strcmp (element_name, "user") == 0)
463 if (!check_no_attributes (parser, "user", attribute_names, attribute_values, error))
466 if (push_element (parser, ELEMENT_USER) == NULL)
474 else if (strcmp (element_name, "type") == 0)
476 if (!check_no_attributes (parser, "type", attribute_names, attribute_values, error))
479 if (push_element (parser, ELEMENT_TYPE) == NULL)
487 else if (strcmp (element_name, "fork") == 0)
489 if (!check_no_attributes (parser, "fork", attribute_names, attribute_values, error))
492 if (push_element (parser, ELEMENT_FORK) == NULL)
502 else if (strcmp (element_name, "listen") == 0)
504 if (!check_no_attributes (parser, "listen", attribute_names, attribute_values, error))
507 if (push_element (parser, ELEMENT_LISTEN) == NULL)
515 else if (strcmp (element_name, "auth") == 0)
517 if (!check_no_attributes (parser, "auth", attribute_names, attribute_values, error))
520 if (push_element (parser, ELEMENT_AUTH) == NULL)
528 else if (strcmp (element_name, "includedir") == 0)
530 if (!check_no_attributes (parser, "includedir", attribute_names, attribute_values, error))
533 if (push_element (parser, ELEMENT_INCLUDEDIR) == NULL)
541 else if (strcmp (element_name, "servicedir") == 0)
543 if (!check_no_attributes (parser, "servicedir", attribute_names, attribute_values, error))
546 if (push_element (parser, ELEMENT_SERVICEDIR) == NULL)
554 else if (strcmp (element_name, "include") == 0)
557 const char *ignore_missing;
559 if ((e = push_element (parser, ELEMENT_INCLUDE)) == NULL)
565 e->d.include.ignore_missing = FALSE;
567 if (!locate_attributes (parser, "include",
571 "ignore_missing", &ignore_missing,
575 if (ignore_missing != NULL)
577 if (strcmp (ignore_missing, "yes") == 0)
578 e->d.include.ignore_missing = TRUE;
579 else if (strcmp (ignore_missing, "no") == 0)
580 e->d.include.ignore_missing = FALSE;
583 dbus_set_error (error, DBUS_ERROR_FAILED,
584 "ignore_missing attribute must have value \"yes\" or \"no\"");
591 else if (strcmp (element_name, "policy") == 0)
598 if ((e = push_element (parser, ELEMENT_POLICY)) == NULL)
604 if (!locate_attributes (parser, "include",
620 dbus_set_error (error, DBUS_ERROR_FAILED,
621 "Element <%s> not allowed inside <%s> in configuration file",
622 element_name, "busconfig");
628 start_policy_child (BusConfigParser *parser,
629 const char *element_name,
630 const char **attribute_names,
631 const char **attribute_values,
634 if (strcmp (element_name, "allow") == 0)
636 if (push_element (parser, ELEMENT_ALLOW) == NULL)
644 else if (strcmp (element_name, "deny") == 0)
646 if (push_element (parser, ELEMENT_DENY) == NULL)
656 dbus_set_error (error, DBUS_ERROR_FAILED,
657 "Element <%s> not allowed inside <%s> in configuration file",
658 element_name, "policy");
664 bus_config_parser_start_element (BusConfigParser *parser,
665 const char *element_name,
666 const char **attribute_names,
667 const char **attribute_values,
672 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
674 /* printf ("START: %s\n", element_name); */
676 t = top_element_type (parser);
678 if (t == ELEMENT_NONE)
680 if (strcmp (element_name, "busconfig") == 0)
682 if (!check_no_attributes (parser, "busconfig", attribute_names, attribute_values, error))
685 if (push_element (parser, ELEMENT_BUSCONFIG) == NULL)
695 dbus_set_error (error, DBUS_ERROR_FAILED,
696 "Unknown element <%s> at root of configuration file",
701 else if (t == ELEMENT_BUSCONFIG)
703 return start_busconfig_child (parser, element_name,
704 attribute_names, attribute_values,
707 else if (t == ELEMENT_POLICY)
709 return start_policy_child (parser, element_name,
710 attribute_names, attribute_values,
715 dbus_set_error (error, DBUS_ERROR_FAILED,
716 "Element <%s> is not allowed in this context",
723 bus_config_parser_end_element (BusConfigParser *parser,
724 const char *element_name,
731 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
733 /* printf ("END: %s\n", element_name); */
735 t = top_element_type (parser);
737 if (t == ELEMENT_NONE)
739 /* should probably be an assertion failure but
740 * being paranoid about XML parsers
742 dbus_set_error (error, DBUS_ERROR_FAILED,
743 "XML parser ended element with no element on the stack");
747 n = element_type_to_name (t);
748 _dbus_assert (n != NULL);
749 if (strcmp (n, element_name) != 0)
751 /* should probably be an assertion failure but
752 * being paranoid about XML parsers
754 dbus_set_error (error, DBUS_ERROR_FAILED,
755 "XML element <%s> ended but topmost element on the stack was <%s>",
760 e = peek_element (parser);
761 _dbus_assert (e != NULL);
766 _dbus_assert_not_reached ("element in stack has no type");
769 case ELEMENT_INCLUDE:
774 case ELEMENT_SERVICEDIR:
775 case ELEMENT_INCLUDEDIR:
778 dbus_set_error (error, DBUS_ERROR_FAILED,
779 "XML element <%s> was expected to have content inside it",
780 element_type_to_name (e->type));
785 case ELEMENT_BUSCONFIG:
794 pop_element (parser);
800 all_whitespace (const DBusString *str)
804 _dbus_string_skip_white (str, 0, &i);
806 return i == _dbus_string_get_length (str);
810 make_full_path (const DBusString *basedir,
811 const DBusString *filename,
812 DBusString *full_path)
814 if (_dbus_path_is_absolute (filename))
816 return _dbus_string_copy (filename, 0, full_path, 0);
820 if (!_dbus_string_copy (basedir, 0, full_path, 0))
823 if (!_dbus_concat_dir_and_file (full_path, filename))
831 include_file (BusConfigParser *parser,
832 const DBusString *filename,
833 dbus_bool_t ignore_missing,
836 /* FIXME good test case for this would load each config file in the
837 * test suite both alone, and as an include, and check
838 * that the result is the same
840 BusConfigParser *included;
843 dbus_error_init (&tmp_error);
844 included = bus_config_load (filename, &tmp_error);
845 if (included == NULL)
847 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
849 if (dbus_error_has_name (&tmp_error, DBUS_ERROR_FILE_NOT_FOUND) &&
852 dbus_error_free (&tmp_error);
857 dbus_move_error (&tmp_error, error);
863 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
865 if (!merge_included (parser, included, error))
867 bus_config_parser_unref (included);
871 bus_config_parser_unref (included);
877 include_dir (BusConfigParser *parser,
878 const DBusString *dirname,
886 if (!_dbus_string_init (&filename))
894 dir = _dbus_directory_open (dirname, error);
899 dbus_error_init (&tmp_error);
900 while (_dbus_directory_get_next_file (dir, &filename, &tmp_error))
902 DBusString full_path;
904 if (!_dbus_string_init (&full_path))
910 if (!_dbus_string_copy (dirname, 0, &full_path, 0))
913 _dbus_string_free (&full_path);
917 if (!_dbus_concat_dir_and_file (&full_path, &filename))
920 _dbus_string_free (&full_path);
924 if (_dbus_string_ends_with_c_str (&full_path, ".conf"))
926 if (!include_file (parser, &full_path, TRUE, error))
928 _dbus_string_free (&full_path);
933 _dbus_string_free (&full_path);
936 if (dbus_error_is_set (&tmp_error))
938 dbus_move_error (&tmp_error, error);
945 _dbus_string_free (&filename);
948 _dbus_directory_close (dir);
954 bus_config_parser_content (BusConfigParser *parser,
955 const DBusString *content,
960 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
966 _dbus_string_get_const_data (content, &c_str);
968 printf ("CONTENT %d bytes: %s\n", _dbus_string_get_length (content), c_str);
972 e = peek_element (parser);
975 dbus_set_error (error, DBUS_ERROR_FAILED,
976 "Text content outside of any XML element in configuration file");
979 else if (e->had_content)
981 _dbus_assert_not_reached ("Element had multiple content blocks");
985 switch (top_element_type (parser))
988 _dbus_assert_not_reached ("element at top of stack has no type");
991 case ELEMENT_BUSCONFIG:
997 if (all_whitespace (content))
1001 dbus_set_error (error, DBUS_ERROR_FAILED,
1002 "No text content expected inside XML element %s in configuration file",
1003 element_type_to_name (top_element_type (parser)));
1007 case ELEMENT_INCLUDE:
1009 DBusString full_path;
1011 e->had_content = TRUE;
1013 if (!_dbus_string_init (&full_path))
1016 if (!make_full_path (&parser->basedir, content, &full_path))
1018 _dbus_string_free (&full_path);
1022 if (!include_file (parser, &full_path,
1023 e->d.include.ignore_missing, error))
1025 _dbus_string_free (&full_path);
1029 _dbus_string_free (&full_path);
1033 case ELEMENT_INCLUDEDIR:
1035 DBusString full_path;
1037 e->had_content = TRUE;
1039 if (!_dbus_string_init (&full_path))
1042 if (!make_full_path (&parser->basedir, content, &full_path))
1044 _dbus_string_free (&full_path);
1048 if (!include_dir (parser, &full_path, error))
1050 _dbus_string_free (&full_path);
1054 _dbus_string_free (&full_path);
1062 e->had_content = TRUE;
1064 if (!_dbus_string_copy_data (content, &s))
1067 dbus_free (parser->user);
1076 e->had_content = TRUE;
1078 if (!_dbus_string_copy_data (content, &s))
1081 dbus_free (parser->bus_type);
1082 parser->bus_type = s;
1086 case ELEMENT_LISTEN:
1090 e->had_content = TRUE;
1092 if (!_dbus_string_copy_data (content, &s))
1095 if (!_dbus_list_append (&parser->listen_on,
1108 e->had_content = TRUE;
1110 if (!_dbus_string_copy_data (content, &s))
1113 if (!_dbus_list_append (&parser->mechanisms,
1122 case ELEMENT_SERVICEDIR:
1125 DBusString full_path;
1127 e->had_content = TRUE;
1129 if (!_dbus_string_init (&full_path))
1132 if (!make_full_path (&parser->basedir, content, &full_path))
1134 _dbus_string_free (&full_path);
1138 if (!_dbus_string_copy_data (&full_path, &s))
1140 _dbus_string_free (&full_path);
1144 if (!_dbus_list_append (&parser->service_dirs, s))
1146 _dbus_string_free (&full_path);
1151 _dbus_string_free (&full_path);
1156 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1160 BUS_SET_OOM (error);
1165 bus_config_parser_finished (BusConfigParser *parser,
1168 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1170 if (parser->stack != NULL)
1172 dbus_set_error (error, DBUS_ERROR_FAILED,
1173 "Element <%s> was not closed in configuration file",
1174 element_type_to_name (top_element_type (parser)));
1179 if (parser->listen_on == NULL)
1181 dbus_set_error (error, DBUS_ERROR_FAILED,
1182 "Configuration file needs one or more <listen> elements giving addresses");
1190 bus_config_parser_get_user (BusConfigParser *parser)
1192 return parser->user;
1196 bus_config_parser_get_type (BusConfigParser *parser)
1198 return parser->bus_type;
1202 bus_config_parser_get_addresses (BusConfigParser *parser)
1204 return &parser->listen_on;
1208 bus_config_parser_get_mechanisms (BusConfigParser *parser)
1210 return &parser->mechanisms;
1214 bus_config_parser_get_service_dirs (BusConfigParser *parser)
1216 return &parser->service_dirs;
1220 bus_config_parser_get_fork (BusConfigParser *parser)
1222 return parser->fork;
1225 #ifdef DBUS_BUILD_TESTS
1236 do_load (const DBusString *full_path,
1238 dbus_bool_t oom_possible)
1240 BusConfigParser *parser;
1243 dbus_error_init (&error);
1245 parser = bus_config_load (full_path, &error);
1248 _DBUS_ASSERT_ERROR_IS_SET (&error);
1251 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1253 _dbus_verbose ("Failed to load valid file due to OOM\n");
1254 dbus_error_free (&error);
1257 else if (validity == VALID)
1259 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
1262 dbus_error_free (&error);
1267 dbus_error_free (&error);
1273 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
1275 bus_config_parser_unref (parser);
1277 if (validity == INVALID)
1279 _dbus_warn ("Accepted invalid file\n");
1289 const DBusString *full_path;
1294 check_loader_oom_func (void *data)
1296 LoaderOomData *d = data;
1298 return do_load (d->full_path, d->validity, TRUE);
1302 process_test_subdir (const DBusString *test_base_dir,
1306 DBusString test_directory;
1307 DBusString filename;
1315 if (!_dbus_string_init (&test_directory))
1316 _dbus_assert_not_reached ("didn't allocate test_directory\n");
1318 _dbus_string_init_const (&filename, subdir);
1320 if (!_dbus_string_copy (test_base_dir, 0,
1321 &test_directory, 0))
1322 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
1324 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
1325 _dbus_assert_not_reached ("couldn't allocate full path");
1327 _dbus_string_free (&filename);
1328 if (!_dbus_string_init (&filename))
1329 _dbus_assert_not_reached ("didn't allocate filename string\n");
1331 dbus_error_init (&error);
1332 dir = _dbus_directory_open (&test_directory, &error);
1335 _dbus_warn ("Could not open %s: %s\n",
1336 _dbus_string_get_const_data (&test_directory),
1338 dbus_error_free (&error);
1342 printf ("Testing:\n");
1345 while (_dbus_directory_get_next_file (dir, &filename, &error))
1347 DBusString full_path;
1350 if (!_dbus_string_init (&full_path))
1351 _dbus_assert_not_reached ("couldn't init string");
1353 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
1354 _dbus_assert_not_reached ("couldn't copy dir to full_path");
1356 if (!_dbus_concat_dir_and_file (&full_path, &filename))
1357 _dbus_assert_not_reached ("couldn't concat file to dir");
1359 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
1361 _dbus_verbose ("Skipping non-.conf file %s\n",
1362 _dbus_string_get_const_data (&filename));
1363 _dbus_string_free (&full_path);
1367 printf (" %s\n", _dbus_string_get_const_data (&filename));
1369 _dbus_verbose (" expecting %s\n",
1370 validity == VALID ? "valid" :
1371 (validity == INVALID ? "invalid" :
1372 (validity == UNKNOWN ? "unknown" : "???")));
1374 d.full_path = &full_path;
1375 d.validity = validity;
1376 if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d))
1377 _dbus_assert_not_reached ("test failed");
1379 _dbus_string_free (&full_path);
1382 if (dbus_error_is_set (&error))
1384 _dbus_warn ("Could not get next file in %s: %s\n",
1385 _dbus_string_get_const_data (&test_directory),
1387 dbus_error_free (&error);
1396 _dbus_directory_close (dir);
1397 _dbus_string_free (&test_directory);
1398 _dbus_string_free (&filename);
1404 bus_config_parser_test (const DBusString *test_data_dir)
1406 if (test_data_dir == NULL ||
1407 _dbus_string_get_length (test_data_dir) == 0)
1409 printf ("No test data\n");
1413 if (!process_test_subdir (test_data_dir, "valid-config-files", VALID))
1419 #endif /* DBUS_BUILD_TESTS */