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
26 #include "config-parser-common.h"
27 #include "config-parser-trivial.h"
29 #include <dbus/dbus-list.h>
30 #include <dbus/dbus-internals.h>
34 * TRIVIAL parser for bus configuration file.
36 struct BusConfigParser
39 DBusString user; /**< User the dbus-daemon runs as */
40 DBusString bus_type; /**< Message bus type */
41 DBusString service_helper; /**< Location of the setuid helper */
42 DBusList *service_dirs; /**< Directories to look for services in */
46 service_dirs_find_dir (DBusList **service_dirs,
51 _dbus_assert (dir != NULL);
53 for (link = *service_dirs; link; link = _dbus_list_get_next_link(service_dirs, link))
57 link_dir = (const char *)link->data;
58 if (strcmp (dir, link_dir) == 0)
66 service_dirs_append_link_unique_or_free (DBusList **service_dirs,
69 if (!service_dirs_find_dir (service_dirs, dir_link->data))
71 _dbus_list_append_link (service_dirs, dir_link);
75 dbus_free (dir_link->data);
76 _dbus_list_free_link (dir_link);
81 bus_config_parser_new (const DBusString *basedir,
82 dbus_bool_t is_toplevel,
83 const BusConfigParser *parent)
85 BusConfigParser *parser;
87 parser = dbus_new0 (BusConfigParser, 1);
91 parser->type = ELEMENT_NONE;
94 parser->service_dirs = NULL;
96 /* init the strings */
97 if (!_dbus_string_init (&parser->user))
99 if (!_dbus_string_init (&parser->bus_type))
101 if (!_dbus_string_init (&parser->service_helper))
107 /* argh. we have do do this carefully because of OOM */
109 _dbus_string_free (&parser->bus_type);
111 _dbus_string_free (&parser->user);
119 bus_config_parser_unref (BusConfigParser *parser)
121 _dbus_string_free (&parser->user);
122 _dbus_string_free (&parser->service_helper);
123 _dbus_string_free (&parser->bus_type);
125 _dbus_list_foreach (&parser->service_dirs,
126 (DBusForeachFunction) dbus_free,
129 _dbus_list_clear (&parser->service_dirs);
135 bus_config_parser_start_element (BusConfigParser *parser,
136 const char *element_name,
137 const char **attribute_names,
138 const char **attribute_values,
141 /* we don't do processing of attribute names, we don't need to */
142 parser->type = bus_config_parser_element_name_to_type (element_name);
144 switch (parser->type)
146 case ELEMENT_SERVICEHELPER:
149 /* content about to be handled */
152 case ELEMENT_STANDARD_SYSTEM_SERVICEDIRS:
158 if (!_dbus_get_standard_system_servicedirs (&dirs))
164 while ((link = _dbus_list_pop_first_link (&dirs)))
165 service_dirs_append_link_unique_or_free (&parser->service_dirs, link);
171 /* we really don't care about the others... */
172 _dbus_verbose (" START We dont care about '%s' type '%i'\n", element_name, parser->type);
180 bus_config_parser_end_element (BusConfigParser *parser,
181 const char *element_name,
189 bus_config_parser_content (BusConfigParser *parser,
190 const DBusString *content,
193 DBusString content_sane;
198 if (!_dbus_string_init (&content_sane))
203 if (!_dbus_string_copy (content, 0, &content_sane, 0))
209 /* rip out white space */
210 _dbus_string_chop_white (&content_sane);
211 if (_dbus_string_get_length (&content_sane) == 0)
213 /* optimise, there is no content */
218 switch (parser->type)
220 case ELEMENT_SERVICEDIR:
224 /* copy the sane data into a char array */
225 if (!_dbus_string_copy_data(&content_sane, &cpath))
231 /* append the dynamic char string to service dirs */
232 if (!_dbus_list_append (&parser->service_dirs, cpath))
241 case ELEMENT_SERVICEHELPER:
243 if (!_dbus_string_copy (&content_sane, 0, &parser->service_helper, 0))
253 if (!_dbus_string_copy (&content_sane, 0, &parser->user, 0))
263 if (!_dbus_string_copy (&content_sane, 0, &parser->bus_type, 0))
272 /* we don't care about the others... really */
273 _dbus_verbose (" CONTENTS We dont care '%s' type '%i'\n", _dbus_string_get_const_data (&content_sane), parser->type);
282 _dbus_string_free (&content_sane);
288 bus_config_parser_finished (BusConfigParser *parser,
291 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
292 _dbus_verbose ("finished scanning!\n");
297 bus_config_parser_get_user (BusConfigParser *parser)
299 return _dbus_string_get_const_data (&parser->user);
303 bus_config_parser_get_type (BusConfigParser *parser)
305 return _dbus_string_get_const_data (&parser->bus_type);
309 bus_config_parser_get_service_dirs (BusConfigParser *parser)
311 return &parser->service_dirs;
314 #ifdef DBUS_BUILD_TESTS
326 check_return_values (const DBusString *full_path)
328 BusConfigParser *parser;
335 dbus_error_init (&error);
338 printf ("Testing values from: %s\n", _dbus_string_get_const_data (full_path));
340 parser = bus_config_load (full_path, TRUE, NULL, &error);
343 _DBUS_ASSERT_ERROR_IS_SET (&error);
344 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
345 _dbus_verbose ("Failed to load valid file due to OOM\n");
348 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
350 /* check user return value is okay */
351 user = bus_config_parser_get_user (parser);
354 _dbus_warn ("User was NULL!\n");
358 /* the username can be configured in configure.in so this test doesn't work */
359 if (strcmp (user, "dbus") != 0)
361 _dbus_warn ("User was invalid; '%s'!\n", user);
364 printf (" <user>dbus</user> OKAY!\n");
367 /* check type return value is okay */
368 type = bus_config_parser_get_type (parser);
371 _dbus_warn ("Type was NULL!\n");
374 if (strcmp (type, "system") != 0)
376 _dbus_warn ("Type was invalid; '%s'!\n", user);
379 printf (" <type>system</type> OKAY!\n");
381 /* check dirs return value is okay */
382 dirs = bus_config_parser_get_service_dirs (parser);
385 _dbus_warn ("Service dirs are NULL!\n");
388 printf (" <standard_system_service_dirs/> OKAY!\n");
389 /* NOTE: We tested the specific return values in the config-parser tests */
395 bus_config_parser_unref (parser);
396 dbus_error_free (&error);
401 do_load (const DBusString *full_path,
403 dbus_bool_t oom_possible)
405 BusConfigParser *parser;
408 dbus_error_init (&error);
410 parser = bus_config_load (full_path, TRUE, NULL, &error);
413 _DBUS_ASSERT_ERROR_IS_SET (&error);
416 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
418 _dbus_verbose ("Failed to load valid file due to OOM\n");
419 dbus_error_free (&error);
422 else if (validity == VALID)
424 _dbus_warn ("Failed to load valid file but still had memory: %s\n",
427 dbus_error_free (&error);
432 dbus_error_free (&error);
438 _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
440 bus_config_parser_unref (parser);
442 if (validity == INVALID)
444 _dbus_warn ("Accepted invalid file\n");
454 const DBusString *full_path;
459 check_loader_oom_func (void *data)
461 LoaderOomData *d = data;
463 return do_load (d->full_path, d->validity, TRUE);
467 process_test_valid_subdir (const DBusString *test_base_dir,
471 DBusString test_directory;
480 if (!_dbus_string_init (&test_directory))
481 _dbus_assert_not_reached ("didn't allocate test_directory\n");
483 _dbus_string_init_const (&filename, subdir);
485 if (!_dbus_string_copy (test_base_dir, 0,
487 _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
489 if (!_dbus_concat_dir_and_file (&test_directory, &filename))
490 _dbus_assert_not_reached ("couldn't allocate full path");
492 _dbus_string_free (&filename);
493 if (!_dbus_string_init (&filename))
494 _dbus_assert_not_reached ("didn't allocate filename string\n");
496 dbus_error_init (&error);
497 dir = _dbus_directory_open (&test_directory, &error);
500 _dbus_warn ("Could not open %s: %s\n",
501 _dbus_string_get_const_data (&test_directory),
503 dbus_error_free (&error);
507 if (validity == VALID)
508 printf ("Testing valid files:\n");
509 else if (validity == INVALID)
510 printf ("Testing invalid files:\n");
512 printf ("Testing unknown files:\n");
515 while (_dbus_directory_get_next_file (dir, &filename, &error))
517 DBusString full_path;
520 if (!_dbus_string_init (&full_path))
521 _dbus_assert_not_reached ("couldn't init string");
523 if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
524 _dbus_assert_not_reached ("couldn't copy dir to full_path");
526 if (!_dbus_concat_dir_and_file (&full_path, &filename))
527 _dbus_assert_not_reached ("couldn't concat file to dir");
529 if (!_dbus_string_ends_with_c_str (&full_path, ".conf"))
531 _dbus_verbose ("Skipping non-.conf file %s\n",
532 _dbus_string_get_const_data (&filename));
533 _dbus_string_free (&full_path);
537 printf (" %s\n", _dbus_string_get_const_data (&filename));
539 _dbus_verbose (" expecting %s\n",
540 validity == VALID ? "valid" :
541 (validity == INVALID ? "invalid" :
542 (validity == UNKNOWN ? "unknown" : "???")));
544 d.full_path = &full_path;
545 d.validity = validity;
547 /* FIXME hackaround for an expat problem, see
548 * https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=124747
549 * http://freedesktop.org/pipermail/dbus/2004-May/001153.html
551 /* if (!_dbus_test_oom_handling ("config-loader", check_loader_oom_func, &d)) */
552 if (!check_loader_oom_func (&d))
553 _dbus_assert_not_reached ("test failed");
555 _dbus_string_free (&full_path);
558 if (dbus_error_is_set (&error))
560 _dbus_warn ("Could not get next file in %s: %s\n",
561 _dbus_string_get_const_data (&test_directory),
563 dbus_error_free (&error);
572 _dbus_directory_close (dir);
573 _dbus_string_free (&test_directory);
574 _dbus_string_free (&filename);
579 /* convenience function, do not reuse outside of TEST */
581 make_full_path (const DBusString *test_data_dir,
584 DBusString *full_path)
591 if (!_dbus_string_init (full_path))
593 _dbus_warn ("couldn't allocate full path");
597 if (!_dbus_string_copy (test_data_dir, 0, full_path, 0))
599 _dbus_warn ("couldn't allocate full path");
603 _dbus_string_init_const (&filename, subdir);
604 if (!_dbus_concat_dir_and_file (full_path, &filename))
606 _dbus_warn ("couldn't allocate full path");
609 _dbus_string_free (&filename);
611 _dbus_string_init_const (&filename, file);
612 if (!_dbus_concat_dir_and_file (full_path, &filename))
614 _dbus_warn ("couldn't allocate full path");
622 _dbus_string_free (&filename);
627 check_file_valid (DBusString *full_path,
632 if (validity == VALID)
633 printf ("Testing valid file:\n");
634 else if (validity == INVALID)
635 printf ("Testing invalid file:\n");
637 printf ("Testing unknown file:\n");
639 /* print the filename, just so we match the other output */
640 printf (" %s\n", _dbus_string_get_const_data (full_path));
642 /* only test one file */
643 retval = do_load (full_path, validity, TRUE);
649 bus_config_parser_trivial_test (const DBusString *test_data_dir)
651 DBusString full_path;
656 if (test_data_dir == NULL ||
657 _dbus_string_get_length (test_data_dir) == 0)
659 printf ("No test data\n");
663 /* We already test default_session_servicedirs and default_system_servicedirs
664 * in bus_config_parser_test() */
665 if (!process_test_valid_subdir (test_data_dir, "valid-config-files", VALID))
668 /* we don't process all the invalid files, as the trivial parser can't hope
669 * to validate them all for all different syntaxes. We just check one broken
670 * file to see if junk is received */
671 if (!make_full_path (test_data_dir, "invalid-config-files", "not-well-formed.conf", &full_path))
673 if (!check_file_valid (&full_path, INVALID))
675 _dbus_string_free (&full_path);
677 /* just test if the check_file_valid works okay and we got sane values */
678 if (!make_full_path (test_data_dir, "valid-config-files", "system.conf", &full_path))
680 if (!check_file_valid (&full_path, VALID))
682 /* check to see if we got the correct values from the parser */
683 if (!check_return_values (&full_path))
690 _dbus_string_free (&full_path);
692 /* we don't process equiv-config-files as we don't handle <include> */
696 #endif /* DBUS_BUILD_TESTS */