1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* config-parser-trivial.c XML-library-agnostic configuration file parser
3 * Does not do includes or anything remotely complicated.
5 * Copyright (C) 2003, 2004, 2007 Red Hat, Inc.
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "config-parser-common.h"
26 #include "config-parser-trivial.h"
28 #include <dbus/dbus-list.h>
29 #include <dbus/dbus-internals.h>
33 * TRIVIAL parser for bus configuration file.
35 struct BusConfigParser
38 DBusString user; /**< User the dbus-daemon runs as */
39 DBusString bus_type; /**< Message bus type */
40 DBusString service_helper; /**< Location of the setuid helper */
41 DBusList *service_dirs; /**< Directories to look for services in */
45 service_dirs_find_dir (DBusList **service_dirs,
50 _dbus_assert (dir != NULL);
52 for (link = *service_dirs; link; link = _dbus_list_get_next_link(service_dirs, link))
56 link_dir = (const char *)link->data;
57 if (strcmp (dir, link_dir) == 0)
65 service_dirs_append_link_unique_or_free (DBusList **service_dirs,
68 if (!service_dirs_find_dir (service_dirs, dir_link->data))
70 _dbus_list_append_link (service_dirs, dir_link);
74 dbus_free (dir_link->data);
75 _dbus_list_free_link (dir_link);
80 bus_config_parser_new (const DBusString *basedir,
81 dbus_bool_t is_toplevel,
82 const BusConfigParser *parent)
84 BusConfigParser *parser;
86 parser = dbus_new0 (BusConfigParser, 1);
90 parser->type = ELEMENT_NONE;
93 parser->service_dirs = NULL;
95 /* init the strings */
96 if (!_dbus_string_init (&parser->user))
98 if (!_dbus_string_init (&parser->bus_type))
100 if (!_dbus_string_init (&parser->service_helper))
106 /* argh. we have do do this carefully because of OOM */
108 _dbus_string_free (&parser->bus_type);
110 _dbus_string_free (&parser->user);
118 bus_config_parser_unref (BusConfigParser *parser)
120 _dbus_string_free (&parser->user);
121 _dbus_string_free (&parser->service_helper);
122 _dbus_string_free (&parser->bus_type);
124 _dbus_list_foreach (&parser->service_dirs,
125 (DBusForeachFunction) dbus_free,
128 _dbus_list_clear (&parser->service_dirs);
134 bus_config_parser_check_doctype (BusConfigParser *parser,
138 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
140 if (strcmp (doctype, "busconfig") != 0)
142 dbus_set_error (error,
144 "Configuration file has the wrong document type %s",
153 bus_config_parser_start_element (BusConfigParser *parser,
154 const char *element_name,
155 const char **attribute_names,
156 const char **attribute_values,
159 /* we don't do processing of attribute names, we don't need to */
160 parser->type = bus_config_parser_element_name_to_type (element_name);
162 switch (parser->type)
164 case ELEMENT_SERVICEHELPER:
167 /* content about to be handled */
170 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
176 if (!_dbus_get_standard_system_servicedirs (&dirs))
182 while ((link = _dbus_list_pop_first_link (&dirs)))
183 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
189 /* we really don't care about the others... */
190 _dbus_verbose (" START We dont care about '%s' type '%i'\n", element_name, parser->type);
198 bus_config_parser_end_element (BusConfigParser *parser,
199 const char *element_name,
207 bus_config_parser_content (BusConfigParser *parser,
208 const DBusString *content,
211 DBusString content_sane;
216 if (!_dbus_string_init (&content_sane))
221 if (!_dbus_string_copy (content, 0, &content_sane, 0))
227 /* rip out white space */
228 _dbus_string_chop_white (&content_sane);
229 if (_dbus_string_get_length (&content_sane) == 0)
231 /* optimise, there is no content */
236 switch (parser->type)
238 case ELEMENT_SERVICEDIR:
242 /* copy the sane data into a char array */
243 if (!_dbus_string_copy_data(&content_sane, &cpath))
249 /* append the dynamic char string to service dirs */
250 if (!_dbus_list_append (&parser->service_dirs, cpath))
259 case ELEMENT_SERVICEHELPER:
261 if (!_dbus_string_copy (&content_sane, 0, &parser->service_helper, 0))
271 if (!_dbus_string_copy (&content_sane, 0, &parser->user, 0))
281 if (!_dbus_string_copy (&content_sane, 0, &parser->bus_type, 0))
290 /* we don't care about the others... really */
291 _dbus_verbose (" CONTENTS We dont care '%s' type '%i'\n", _dbus_string_get_const_data (&content_sane), parser->type);
300 _dbus_string_free (&content_sane);
306 bus_config_parser_finished (BusConfigParser *parser,
309 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
310 _dbus_verbose ("finished scanning!\n");
315 bus_config_parser_get_user (BusConfigParser *parser)
317 return _dbus_string_get_const_data (&parser->user);
321 bus_config_parser_get_type (BusConfigParser *parser)
323 return _dbus_string_get_const_data (&parser->bus_type);
327 bus_config_parser_get_service_dirs (BusConfigParser *parser)
329 return &parser->service_dirs;
332 #ifdef DBUS_BUILD_TESTS
344 check_return_values (const DBusString *full_path)
346 BusConfigParser *parser;
353 dbus_error_init (&error);
356 printf ("Testing values from: %s\n", _dbus_string_get_const_data (full_path));
358 parser = bus_config_load (full_path, TRUE, NULL, &error);
361 _DBUS_ASSERT_ERROR_IS_SET (&error);
362 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
363 _dbus_verbose ("Failed to load valid file due to OOM\n");
366 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
368 /* check user return value is okay */
369 user = bus_config_parser_get_user (parser);
372 _dbus_warn ("User was NULL!\n");
376 /* the username can be configured in configure.in so this test doesn't work */
377 if (strcmp (user, "dbus") != 0)
379 _dbus_warn ("User was invalid; '%s'!\n", user);
382 printf (" <user>dbus</user> OKAY!\n");
385 /* check type return value is okay */
386 type = bus_config_parser_get_type (parser);
389 _dbus_warn ("Type was NULL!\n");
392 if (strcmp (type, "system") != 0)
394 _dbus_warn ("Type was invalid; '%s'!\n", user);
397 printf (" <type>system</type> OKAY!\n");
399 /* check dirs return value is okay */
400 dirs = bus_config_parser_get_service_dirs (parser);
403 _dbus_warn ("Service dirs are NULL!\n");
406 printf (" <standard_system_service_dirs/> OKAY!\n");
407 /* NOTE: We tested the specific return values in the config-parser tests */
413 bus_config_parser_unref (parser);
414 dbus_error_free (&error);
419 do_load (const DBusString *full_path,
421 dbus_bool_t oom_possible)
423 BusConfigParser *parser;
426 dbus_error_init (&error);
428 parser = bus_config_load (full_path, TRUE, NULL, &error);
431 _DBUS_ASSERT_ERROR_IS_SET (&error);
434 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
436 _dbus_verbose ("Failed to load valid file due to OOM\n");
437 dbus_error_free (&error);
440 else if (validity == VALID)
442 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
445 dbus_error_free (&error);
450 dbus_error_free (&error);
456 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
458 bus_config_parser_unref (parser);
460 if (validity == INVALID)
462 _dbus_warn ("Accepted invalid file\n");
472 const DBusString *full_path;
477 check_loader_oom_func (void *data)
479 LoaderOomData *d = data;
481 return do_load (d->full_path, d->validity, TRUE);
485 process_test_valid_subdir (const DBusString *test_base_dir,
489 DBusString test_directory;
498 if (!_dbus_string_init (&test_directory))
499 _dbus_assert_not_reached ("didn't allocate test_directory\n");
501 _dbus_string_init_const (&filename, subdir);
503 if (!_dbus_string_copy (test_base_dir, 0,
505 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
507 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
508 _dbus_assert_not_reached ("couldn't allocate full path");
510 _dbus_string_free (&filename);
511 if (!_dbus_string_init (&filename))
512 _dbus_assert_not_reached ("didn't allocate filename string\n");
514 dbus_error_init (&error);
515 dir = _dbus_directory_open (&test_directory, &error);
518 _dbus_warn ("Could not open %s: %s\n",
519 _dbus_string_get_const_data (&test_directory),
521 dbus_error_free (&error);
525 if (validity == VALID)
526 printf ("Testing valid files:\n");
527 else if (validity == INVALID)
528 printf ("Testing invalid files:\n");
530 printf ("Testing unknown files:\n");
533 while (_dbus_directory_get_next_file (dir, &filename, &error))
535 DBusString full_path;
538 if (!_dbus_string_init (&full_path))
539 _dbus_assert_not_reached ("couldn't init string");
541 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
542 _dbus_assert_not_reached ("couldn't copy dir to full_path");
544 if (!_dbus_concat_dir_and_file (&full_path, &filename))
545 _dbus_assert_not_reached ("couldn't concat file to dir");
547 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
549 _dbus_verbose ("Skipping non-.conf file %s\n",
550 _dbus_string_get_const_data (&filename));
551 _dbus_string_free (&full_path);
555 printf (" %s\n", _dbus_string_get_const_data (&filename));
557 _dbus_verbose (" expecting %s\n",
558 validity == VALID ? "valid" :
559 (validity == INVALID ? "invalid" :
560 (validity == UNKNOWN ? "unknown" : "???")));
562 d.full_path = &full_path;
563 d.validity = validity;
565 /* FIXME hackaround for an expat problem, see
566 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
567 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
569 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
570 if (!check_loader_oom_func (&d))
571 _dbus_assert_not_reached ("test failed");
573 _dbus_string_free (&full_path);
576 if (dbus_error_is_set (&error))
578 _dbus_warn ("Could not get next file in %s: %s\n",
579 _dbus_string_get_const_data (&test_directory),
581 dbus_error_free (&error);
590 _dbus_directory_close (dir);
591 _dbus_string_free (&test_directory);
592 _dbus_string_free (&filename);
597 /* convenience function, do not reuse outside of TEST */
599 make_full_path (const DBusString *test_data_dir,
602 DBusString *full_path)
609 if (!_dbus_string_init (full_path))
611 _dbus_warn ("couldn't allocate full path");
615 if (!_dbus_string_copy (test_data_dir, 0, full_path, 0))
617 _dbus_warn ("couldn't allocate full path");
621 _dbus_string_init_const (&filename, subdir);
622 if (!_dbus_concat_dir_and_file (full_path, &filename))
624 _dbus_warn ("couldn't allocate full path");
627 _dbus_string_free (&filename);
629 _dbus_string_init_const (&filename, file);
630 if (!_dbus_concat_dir_and_file (full_path, &filename))
632 _dbus_warn ("couldn't allocate full path");
640 _dbus_string_free (&filename);
645 check_file_valid (DBusString *full_path,
650 if (validity == VALID)
651 printf ("Testing valid file:\n");
652 else if (validity == INVALID)
653 printf ("Testing invalid file:\n");
655 printf ("Testing unknown file:\n");
657 /* print the filename, just so we match the other output */
658 printf (" %s\n", _dbus_string_get_const_data (full_path));
660 /* only test one file */
661 retval = do_load (full_path, validity, TRUE);
667 bus_config_parser_trivial_test (const DBusString *test_data_dir)
669 DBusString full_path;
674 if (test_data_dir == NULL ||
675 _dbus_string_get_length (test_data_dir) == 0)
677 printf ("No test data\n");
681 /* We already test default_session_servicedirs and default_system_servicedirs
682 * in bus_config_parser_test() */
683 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
686 /* we don't process all the invalid files, as the trivial parser can't hope
687 * to validate them all for all different syntaxes. We just check one broken
688 * file to see if junk is received */
689 if (!make_full_path (test_data_dir, "invalid-config-files", "not-well-formed.conf", &full_path))
691 if (!check_file_valid (&full_path, INVALID))
693 _dbus_string_free (&full_path);
695 /* just test if the check_file_valid works okay and we got sane values */
696 if (!make_full_path (test_data_dir, "valid-config-files", "system.conf", &full_path))
698 if (!check_file_valid (&full_path, VALID))
700 /* check to see if we got the correct values from the parser */
701 if (!check_return_values (&full_path))
708 _dbus_string_free (&full_path);
710 /* we don't process equiv-config-files as we don't handle <include> */
714 #endif /* DBUS_BUILD_TESTS */