From 2d544a99b39543325c7d78d8bbc790b61f74d011 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 17 Feb 2017 15:40:34 +0000 Subject: [PATCH 16/16] activation: Add support for enforcing strict naming on .service files This is done on a per-directory basis. The use of the BusContext here means we have to make the activation test a little more realistic, by providing a non-NULL BusContext. Signed-off-by: Simon McVittie --- bus/activation.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++---- bus/config-parser.h | 1 + 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/bus/activation.c b/bus/activation.c index 8e477067..f35cb02e 100644 --- a/bus/activation.c +++ b/bus/activation.c @@ -402,6 +402,58 @@ update_desktop_file_entry (BusActivation *activation, if (entry == NULL) /* New file */ { + DBusString expected_name; + + if (!_dbus_string_init (&expected_name)) + { + BUS_SET_OOM (error); + goto out; + } + + if (!_dbus_string_append (&expected_name, name) || + !_dbus_string_append (&expected_name, ".service")) + { + _dbus_string_free (&expected_name); + BUS_SET_OOM (error); + goto out; + } + + if (_dbus_string_equal (&expected_name, filename)) + { + _dbus_verbose ("Name of \"%s\" is as expected\n", + _dbus_string_get_const_data (&file_path)); + } + else if (s_dir->flags & BUS_SERVICE_DIR_FLAGS_STRICT_NAMING) + { + bus_context_log_and_set_error (activation->context, + DBUS_SYSTEM_LOG_WARNING, error, + DBUS_ERROR_FAILED, + "Service file \"%s\" should have " + "been named \"%s\": not loading it", + _dbus_string_get_const_data (&file_path), + _dbus_string_get_const_data (&expected_name)); + _dbus_string_free (&expected_name); + goto out; + } + else if (bus_context_get_servicehelper (activation->context) != NULL) + { + bus_context_log (activation->context, DBUS_SYSTEM_LOG_WARNING, + "Service file \"%s\" should have been named \"%s\"" + "and will not work with system bus activation", + _dbus_string_get_const_data (&file_path), + _dbus_string_get_const_data (&expected_name)); + /* We don't actually error out here, because *technically* it could + * still work on systemd systems, where we tell systemd to start the + * SystemdService instead of launching dbus-daemon-launch-helper + * ourselves. */ + } + /* We could maybe log mismatched names for session services too, + * but don't do that until + * https://lintian.debian.org/tags/dbus-session-service-wrong-name.html + * is a bit shorter */ + + _dbus_string_free (&expected_name); + /* FIXME we need a better-defined algorithm for which service file to * pick than "whichever one is first in the directory listing" */ @@ -2522,10 +2574,13 @@ do_test (const char *description, dbus_bool_t oom_test, CheckData *data) } static dbus_bool_t -do_service_reload_test (DBusString *dir, dbus_bool_t oom_test) +do_service_reload_test (const DBusString *test_data_dir, + DBusString *dir, + dbus_bool_t oom_test) { BusActivation *activation; BusConfigServiceDir config; + BusContext *context; DBusString address; DBusList *directories; CheckData d; @@ -2539,7 +2594,12 @@ do_service_reload_test (DBusString *dir, dbus_bool_t oom_test) if (!_dbus_list_append (&directories, &config)) return FALSE; - activation = bus_activation_new (NULL, &address, &directories, NULL); + context = bus_context_new_test (test_data_dir, + "valid-config-files/debug-allow-all.conf"); + if (context == NULL) + return FALSE; + + activation = bus_activation_new (context, &address, &directories, NULL); if (!activation) return FALSE; @@ -2600,6 +2660,7 @@ do_service_reload_test (DBusString *dir, dbus_bool_t oom_test) bus_activation_unref (activation); _dbus_list_clear (&directories); + bus_context_unref (context); return TRUE; } @@ -2630,7 +2691,7 @@ bus_activation_service_reload_test (const DBusString *test_data_dir) if (!init_service_reload_test (&directory)) _dbus_assert_not_reached ("could not initiate service reload test"); - if (!do_service_reload_test (&directory, FALSE)) + if (!do_service_reload_test (test_data_dir, &directory, FALSE)) { /* Do nothing? */ } @@ -2642,7 +2703,7 @@ bus_activation_service_reload_test (const DBusString *test_data_dir) if (!init_service_reload_test (&directory)) _dbus_assert_not_reached ("could not initiate service reload test"); - if (!do_service_reload_test (&directory, TRUE)) + if (!do_service_reload_test (test_data_dir, &directory, TRUE)) { /* Do nothing? */ } diff --git a/bus/config-parser.h b/bus/config-parser.h index b3dfa91e..a24e74b2 100644 --- a/bus/config-parser.h +++ b/bus/config-parser.h @@ -94,6 +94,7 @@ BusConfigParser* bus_config_load (const DBusString *file, typedef enum { BUS_SERVICE_DIR_FLAGS_NO_WATCH = (1 << 0), + BUS_SERVICE_DIR_FLAGS_STRICT_NAMING = (1 << 1), /* Keep this one at the end to reduce diffs when adding new entries */ BUS_SERVICE_DIR_FLAGS_NONE = 0 } BusServiceDirFlags; -- 2.11.0