1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* driver.c Bus client (driver)
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 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 "activation.h"
28 #include "connection.h"
37 #include <dbus/dbus-asv-util.h>
38 #include <dbus/dbus-connection-internal.h>
39 #include <dbus/dbus-string.h>
40 #include <dbus/dbus-internals.h>
41 #include <dbus/dbus-message.h>
42 #include <dbus/dbus-marshal-recursive.h>
43 #include <dbus/dbus-marshal-validate.h>
48 BUS_DRIVER_FOUND_SELF,
49 BUS_DRIVER_FOUND_PEER,
50 BUS_DRIVER_FOUND_ERROR,
53 static inline const char *
54 nonnull (const char *maybe_null,
57 return (maybe_null ? maybe_null : if_null);
60 static DBusConnection *
61 bus_driver_get_owner_of_name (DBusConnection *connection,
64 BusRegistry *registry;
68 registry = bus_connection_get_registry (connection);
69 _dbus_string_init_const (&str, name);
70 serv = bus_registry_lookup (registry, &str);
75 return bus_service_get_primary_owners_connection (serv);
79 bus_driver_get_conn_helper (DBusConnection *connection,
81 const char *what_we_want,
83 DBusConnection **peer_conn_p,
89 if (!dbus_message_get_args (message, error,
90 DBUS_TYPE_STRING, &name,
92 return BUS_DRIVER_FOUND_ERROR;
94 _dbus_assert (name != NULL);
95 _dbus_verbose ("asked for %s of connection %s\n", what_we_want, name);
100 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
101 return BUS_DRIVER_FOUND_SELF;
103 conn = bus_driver_get_owner_of_name (connection, name);
107 dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
108 "Could not get %s of name '%s': no such name",
110 return BUS_DRIVER_FOUND_ERROR;
113 if (peer_conn_p != NULL)
116 return BUS_DRIVER_FOUND_PEER;
120 * Log a security warning and set error unless the uid of the connection
121 * is either the uid of this process, or on Unix, uid 0 (root).
123 * This is intended to be a second line of defence after <deny> rules,
124 * to mitigate incorrect system bus security policy configuration files
125 * like the ones in CVE-2014-8148 and CVE-2014-8156, and (if present)
126 * LSM rules; so it doesn't need to be perfect, but as long as we have
127 * potentially dangerous functionality in the system bus, it does need
131 bus_driver_check_caller_is_privileged (DBusConnection *connection,
132 BusTransaction *transaction,
133 DBusMessage *message,
139 if (!dbus_connection_get_unix_user (connection, &uid))
141 const char *method = dbus_message_get_member (message);
143 bus_context_log_and_set_error (bus_transaction_get_context (transaction),
144 DBUS_SYSTEM_LOG_SECURITY, error, DBUS_ERROR_ACCESS_DENIED,
145 "rejected attempt to call %s by connection %s (%s) with "
146 "unknown uid", method,
147 nonnull (bus_connection_get_name (connection), "(inactive)"),
148 bus_connection_get_loginfo (connection));
152 /* I'm writing it in this slightly strange form so that it's more
153 * obvious that this security-sensitive code is correct.
155 if (_dbus_unix_user_is_process_owner (uid))
165 const char *method = dbus_message_get_member (message);
167 bus_context_log_and_set_error (bus_transaction_get_context (transaction),
168 DBUS_SYSTEM_LOG_SECURITY, error, DBUS_ERROR_ACCESS_DENIED,
169 "rejected attempt to call %s by connection %s (%s) with "
171 nonnull (bus_connection_get_name (connection), "(inactive)"),
172 bus_connection_get_loginfo (connection), uid);
177 #elif defined(DBUS_WIN)
178 char *windows_sid = NULL;
179 dbus_bool_t ret = FALSE;
181 if (!dbus_connection_get_windows_user (connection, &windows_sid))
183 const char *method = dbus_message_get_member (message);
185 bus_context_log_and_set_error (bus_transaction_get_context (transaction),
186 DBUS_SYSTEM_LOG_SECURITY, error, DBUS_ERROR_ACCESS_DENIED,
187 "rejected attempt to call %s by unknown uid", method);
191 if (!_dbus_windows_user_is_process_owner (windows_sid))
193 const char *method = dbus_message_get_member (message);
195 bus_context_log_and_set_error (bus_transaction_get_context (transaction),
196 DBUS_SYSTEM_LOG_SECURITY, error, DBUS_ERROR_ACCESS_DENIED,
197 "rejected attempt to call %s by uid %s", method, windows_sid);
203 dbus_free (windows_sid);
206 /* make sure we fail closed in the hypothetical case that we are neither
207 * Unix nor Windows */
208 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
209 "please teach bus/driver.c how uids work on this platform");
214 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
215 DBusMessage *hello_message,
216 BusTransaction *transaction,
220 bus_driver_send_service_owner_changed (const char *service_name,
221 const char *old_owner,
222 const char *new_owner,
223 BusTransaction *transaction,
226 DBusMessage *message;
228 const char *null_service;
230 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
233 _dbus_verbose ("sending name owner changed: %s [%s -> %s]\n",
235 old_owner ? old_owner : null_service,
236 new_owner ? new_owner : null_service);
238 message = dbus_message_new_signal (DBUS_PATH_DBUS,
248 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
251 if (!dbus_message_append_args (message,
252 DBUS_TYPE_STRING, &service_name,
253 DBUS_TYPE_STRING, old_owner ? &old_owner : &null_service,
254 DBUS_TYPE_STRING, new_owner ? &new_owner : &null_service,
258 _dbus_assert (dbus_message_has_signature (message, "sss"));
260 if (!bus_transaction_capture (transaction, NULL, NULL, message))
263 retval = bus_dispatch_matches (transaction, NULL, NULL, message, error);
264 dbus_message_unref (message);
269 dbus_message_unref (message);
275 bus_driver_send_service_lost (DBusConnection *connection,
276 const char *service_name,
277 BusTransaction *transaction,
280 DBusMessage *message;
282 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
284 message = dbus_message_new_signal (DBUS_PATH_DBUS,
294 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
295 !dbus_message_append_args (message,
296 DBUS_TYPE_STRING, &service_name,
299 dbus_message_unref (message);
304 if (!bus_transaction_send_from_driver (transaction, connection, message))
306 dbus_message_unref (message);
312 dbus_message_unref (message);
318 bus_driver_send_service_acquired (DBusConnection *connection,
319 const char *service_name,
320 BusTransaction *transaction,
323 DBusMessage *message;
325 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
327 message = dbus_message_new_signal (DBUS_PATH_DBUS,
337 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
338 !dbus_message_append_args (message,
339 DBUS_TYPE_STRING, &service_name,
342 dbus_message_unref (message);
347 if (!bus_transaction_send_from_driver (transaction, connection, message))
349 dbus_message_unref (message);
355 dbus_message_unref (message);
361 create_unique_client_name (BusRegistry *registry,
364 /* We never want to use the same unique client name twice, because
365 * we want to guarantee that if you send a message to a given unique
366 * name, you always get the same application. So we use two numbers
367 * for INT_MAX * INT_MAX combinations, should be pretty safe against
370 /* FIXME these should be in BusRegistry rather than static vars */
371 static int next_major_number = 0;
372 static int next_minor_number = 0;
375 len = _dbus_string_get_length (str);
379 /* start out with 1-0, go to 1-1, 1-2, 1-3,
380 * up to 1-MAXINT, then 2-0, 2-1, etc.
382 if (next_minor_number <= 0)
384 next_major_number += 1;
385 next_minor_number = 0;
386 if (next_major_number <= 0)
387 _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
390 _dbus_assert (next_major_number > 0);
391 _dbus_assert (next_minor_number >= 0);
393 /* appname:MAJOR-MINOR */
395 if (!_dbus_string_append (str, ":"))
398 if (!_dbus_string_append_int (str, next_major_number))
401 if (!_dbus_string_append (str, "."))
404 if (!_dbus_string_append_int (str, next_minor_number))
407 next_minor_number += 1;
409 /* Check if a client with the name exists */
410 if (bus_registry_lookup (registry, str) == NULL)
413 /* drop the number again, try the next one. */
414 _dbus_string_set_length (str, len);
421 bus_driver_handle_hello (DBusConnection *connection,
422 BusTransaction *transaction,
423 DBusMessage *message,
426 DBusString unique_name;
429 BusRegistry *registry;
430 BusConnections *connections;
433 const char *limit_name;
435 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
437 if (bus_connection_is_active (connection))
439 /* We already handled an Hello message for this connection. */
440 dbus_set_error (error, DBUS_ERROR_FAILED,
441 "Already handled an Hello message");
445 /* Note that when these limits are exceeded we don't disconnect the
446 * connection; we just sort of leave it hanging there until it times
447 * out or disconnects itself or is dropped due to the max number of
448 * incomplete connections. It's even OK if the connection wants to
449 * retry the hello message, we support that.
451 dbus_error_init (&tmp_error);
452 connections = bus_connection_get_connections (connection);
453 if (!bus_connections_check_limits (connections, connection,
459 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
460 context = bus_connection_get_context (connection);
461 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING, "%s (%s=%d)",
462 tmp_error.message, limit_name, limit);
463 dbus_move_error (&tmp_error, error);
467 if (!_dbus_string_init (&unique_name))
475 registry = bus_connection_get_registry (connection);
477 if (!create_unique_client_name (registry, &unique_name))
483 if (!bus_connection_complete (connection, &unique_name, error))
485 _DBUS_ASSERT_ERROR_IS_SET (error);
489 if (!dbus_message_set_sender (message,
490 bus_connection_get_name (connection)))
496 if (!bus_driver_send_welcome_message (connection, message, transaction, error))
499 /* Create the service */
500 service = bus_registry_ensure (registry,
501 &unique_name, connection, 0, transaction, error);
505 _dbus_assert (bus_connection_is_active (connection));
509 _dbus_string_free (&unique_name);
514 bus_driver_send_welcome_message (DBusConnection *connection,
515 DBusMessage *hello_message,
516 BusTransaction *transaction,
519 DBusMessage *welcome;
522 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
524 name = bus_connection_get_name (connection);
525 _dbus_assert (name != NULL);
527 welcome = dbus_message_new_method_return (hello_message);
534 if (!dbus_message_append_args (welcome,
535 DBUS_TYPE_STRING, &name,
538 dbus_message_unref (welcome);
543 _dbus_assert (dbus_message_has_signature (welcome, DBUS_TYPE_STRING_AS_STRING));
545 if (!bus_transaction_send_from_driver (transaction, connection, welcome))
547 dbus_message_unref (welcome);
553 dbus_message_unref (welcome);
559 bus_driver_handle_list_services (DBusConnection *connection,
560 BusTransaction *transaction,
561 DBusMessage *message,
567 BusRegistry *registry;
569 DBusMessageIter iter;
572 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
574 registry = bus_connection_get_registry (connection);
576 reply = dbus_message_new_method_return (message);
583 if (!bus_registry_list_services (registry, &services, &len))
585 dbus_message_unref (reply);
590 dbus_message_iter_init_append (reply, &iter);
592 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
593 DBUS_TYPE_STRING_AS_STRING,
596 dbus_free_string_array (services);
597 dbus_message_unref (reply);
603 /* Include the bus driver in the list */
604 const char *v_STRING = DBUS_SERVICE_DBUS;
605 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
608 dbus_free_string_array (services);
609 dbus_message_unref (reply);
618 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
621 dbus_free_string_array (services);
622 dbus_message_unref (reply);
629 dbus_free_string_array (services);
631 if (!dbus_message_iter_close_container (&iter, &sub))
633 dbus_message_unref (reply);
638 if (!bus_transaction_send_from_driver (transaction, connection, reply))
640 dbus_message_unref (reply);
646 dbus_message_unref (reply);
652 bus_driver_handle_list_activatable_services (DBusConnection *connection,
653 BusTransaction *transaction,
654 DBusMessage *message,
660 BusActivation *activation;
662 DBusMessageIter iter;
665 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
667 activation = bus_connection_get_activation (connection);
669 reply = dbus_message_new_method_return (message);
676 if (!bus_activation_list_services (activation, &services, &len))
678 dbus_message_unref (reply);
683 dbus_message_iter_init_append (reply, &iter);
685 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
686 DBUS_TYPE_STRING_AS_STRING,
689 dbus_free_string_array (services);
690 dbus_message_unref (reply);
696 /* Include the bus driver in the list */
697 const char *v_STRING = DBUS_SERVICE_DBUS;
698 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
701 dbus_free_string_array (services);
702 dbus_message_unref (reply);
711 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
714 dbus_free_string_array (services);
715 dbus_message_unref (reply);
722 dbus_free_string_array (services);
724 if (!dbus_message_iter_close_container (&iter, &sub))
726 dbus_message_unref (reply);
731 if (!bus_transaction_send_from_driver (transaction, connection, reply))
733 dbus_message_unref (reply);
739 dbus_message_unref (reply);
745 bus_driver_handle_acquire_service (DBusConnection *connection,
746 BusTransaction *transaction,
747 DBusMessage *message,
751 DBusString service_name;
753 dbus_uint32_t service_reply;
756 BusRegistry *registry;
758 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
760 registry = bus_connection_get_registry (connection);
762 if (!dbus_message_get_args (message, error,
763 DBUS_TYPE_STRING, &name,
764 DBUS_TYPE_UINT32, &flags,
768 _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
773 _dbus_string_init_const (&service_name, name);
775 if (!bus_registry_acquire_service (registry, connection,
776 &service_name, flags,
777 &service_reply, transaction,
781 reply = dbus_message_new_method_return (message);
788 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
794 if (!bus_transaction_send_from_driver (transaction, connection, reply))
804 dbus_message_unref (reply);
809 bus_driver_handle_release_service (DBusConnection *connection,
810 BusTransaction *transaction,
811 DBusMessage *message,
815 DBusString service_name;
817 dbus_uint32_t service_reply;
819 BusRegistry *registry;
821 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
823 registry = bus_connection_get_registry (connection);
825 if (!dbus_message_get_args (message, error,
826 DBUS_TYPE_STRING, &name,
830 _dbus_verbose ("Trying to release name %s\n", name);
835 _dbus_string_init_const (&service_name, name);
837 if (!bus_registry_release_service (registry, connection,
838 &service_name, &service_reply,
842 reply = dbus_message_new_method_return (message);
849 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
855 if (!bus_transaction_send_from_driver (transaction, connection, reply))
865 dbus_message_unref (reply);
870 bus_driver_handle_service_exists (DBusConnection *connection,
871 BusTransaction *transaction,
872 DBusMessage *message,
876 DBusString service_name;
878 dbus_bool_t service_exists;
881 BusRegistry *registry;
883 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
885 registry = bus_connection_get_registry (connection);
887 if (!dbus_message_get_args (message, error,
888 DBUS_TYPE_STRING, &name,
894 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
896 service_exists = TRUE;
900 _dbus_string_init_const (&service_name, name);
901 service = bus_registry_lookup (registry, &service_name);
902 service_exists = service != NULL;
905 reply = dbus_message_new_method_return (message);
912 if (!dbus_message_append_args (reply,
913 DBUS_TYPE_BOOLEAN, &service_exists,
920 if (!bus_transaction_send_from_driver (transaction, connection, reply))
930 dbus_message_unref (reply);
936 bus_driver_handle_activate_service (DBusConnection *connection,
937 BusTransaction *transaction,
938 DBusMessage *message,
944 BusActivation *activation;
946 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
948 activation = bus_connection_get_activation (connection);
950 if (!dbus_message_get_args (message, error,
951 DBUS_TYPE_STRING, &name,
952 DBUS_TYPE_UINT32, &flags,
955 _DBUS_ASSERT_ERROR_IS_SET (error);
956 _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
962 if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
963 message, name, error))
965 _DBUS_ASSERT_ERROR_IS_SET (error);
966 _dbus_verbose ("bus_activation_activate_service() failed\n");
977 send_ack_reply (DBusConnection *connection,
978 BusTransaction *transaction,
979 DBusMessage *message,
984 if (dbus_message_get_no_reply (message))
987 reply = dbus_message_new_method_return (message);
994 if (!bus_transaction_send_from_driver (transaction, connection, reply))
997 dbus_message_unref (reply);
1001 dbus_message_unref (reply);
1007 * Send a message from the driver, activating the destination if necessary.
1008 * The message must already have a destination set.
1011 bus_driver_send_or_activate (BusTransaction *transaction,
1012 DBusMessage *message,
1015 BusContext *context;
1016 BusService *service;
1017 const char *service_name;
1018 DBusString service_string;
1020 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1022 service_name = dbus_message_get_destination (message);
1024 _dbus_assert (service_name != NULL);
1026 _dbus_string_init_const (&service_string, service_name);
1028 context = bus_transaction_get_context (transaction);
1030 service = bus_registry_lookup (bus_context_get_registry (context),
1033 if (service == NULL)
1035 /* destination isn't connected yet; pass the message to activation */
1036 BusActivation *activation;
1038 activation = bus_context_get_activation (context);
1040 if (!bus_transaction_capture (transaction, NULL, NULL, message))
1042 BUS_SET_OOM (error);
1043 _dbus_verbose ("No memory for bus_transaction_capture()");
1047 if (!bus_activation_activate_service (activation, NULL, transaction, TRUE,
1048 message, service_name, error))
1050 _DBUS_ASSERT_ERROR_IS_SET (error);
1051 _dbus_verbose ("bus_activation_activate_service() failed");
1057 DBusConnection *service_conn;
1059 service_conn = bus_service_get_primary_owners_connection (service);
1061 if (!bus_transaction_send_from_driver (transaction, service_conn, message))
1063 BUS_SET_OOM (error);
1064 _dbus_verbose ("No memory for bus_transaction_send_from_driver()");
1073 bus_driver_handle_update_activation_environment (DBusConnection *connection,
1074 BusTransaction *transaction,
1075 DBusMessage *message,
1079 BusActivation *activation;
1080 BusContext *context;
1081 DBusMessageIter iter;
1082 DBusMessageIter dict_iter;
1083 DBusMessageIter dict_entry_iter;
1086 DBusList *keys, *key_link;
1087 DBusList *values, *value_link;
1088 DBusMessage *systemd_message;
1089 DBusMessageIter systemd_iter;
1091 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1093 if (!bus_driver_check_message_is_for_us (message, error))
1098 /* UpdateActivationEnvironment is basically a recipe for privilege
1099 * escalation so let's be extra-careful: do not allow the sysadmin
1100 * to shoot themselves in the foot.
1102 if (!bus_driver_check_caller_is_privileged (connection, transaction,
1108 context = bus_connection_get_context (connection);
1110 if (bus_context_get_servicehelper (context) != NULL)
1112 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1113 "Cannot change activation environment "
1114 "on a system bus.");
1118 activation = bus_connection_get_activation (connection);
1120 dbus_message_iter_init (message, &iter);
1122 /* The message signature has already been checked for us,
1123 * so let's just assert it's right.
1125 _dbus_assert (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY);
1127 dbus_message_iter_recurse (&iter, &dict_iter);
1130 systemd_message = NULL;
1132 /* Then loop through the sent dictionary, add the location of
1133 * the environment keys and values to lists. The result will
1134 * be in reverse order, so we don't have to constantly search
1135 * for the end of the list in a loop.
1139 while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
1141 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
1143 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
1149 dbus_message_iter_get_basic (&dict_entry_iter, &key);
1150 dbus_message_iter_next (&dict_entry_iter);
1152 value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
1154 if (value_type != DBUS_TYPE_STRING)
1157 dbus_message_iter_get_basic (&dict_entry_iter, &value);
1159 if (!_dbus_list_append (&keys, key))
1161 BUS_SET_OOM (error);
1165 if (!_dbus_list_append (&values, value))
1167 BUS_SET_OOM (error);
1171 dbus_message_iter_next (&dict_entry_iter);
1174 if (key_type != DBUS_TYPE_INVALID)
1177 dbus_message_iter_next (&dict_iter);
1180 if (array_type != DBUS_TYPE_INVALID)
1183 _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
1185 if (bus_context_get_systemd_activation (bus_connection_get_context (connection)))
1187 /* Prepare a call to forward environment updates to systemd */
1188 systemd_message = dbus_message_new_method_call ("org.freedesktop.systemd1",
1189 "/org/freedesktop/systemd1",
1190 "org.freedesktop.systemd1.Manager",
1192 if (systemd_message == NULL ||
1193 !dbus_message_set_sender (systemd_message, DBUS_SERVICE_DBUS))
1195 BUS_SET_OOM (error);
1196 _dbus_verbose ("No memory to create systemd message\n");
1200 dbus_message_set_no_reply (systemd_message, TRUE);
1201 dbus_message_iter_init_append (systemd_message, &iter);
1203 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "s",
1206 BUS_SET_OOM (error);
1207 _dbus_verbose ("No memory to open systemd message container\n");
1213 value_link = values;
1214 while (key_link != NULL)
1219 key = key_link->data;
1220 value = value_link->data;
1222 if (!bus_activation_set_environment_variable (activation,
1225 _DBUS_ASSERT_ERROR_IS_SET (error);
1226 _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
1230 if (systemd_message != NULL)
1235 /* SetEnvironment wants an array of KEY=VALUE strings */
1236 if (!_dbus_string_init (&envline) ||
1237 !_dbus_string_append_printf (&envline, "%s=%s", key, value))
1239 BUS_SET_OOM (error);
1240 _dbus_verbose ("No memory to format systemd environment line\n");
1241 _dbus_string_free (&envline);
1245 s = _dbus_string_get_data (&envline);
1247 if (!dbus_message_iter_append_basic (&systemd_iter,
1248 DBUS_TYPE_STRING, &s))
1250 BUS_SET_OOM (error);
1251 _dbus_verbose ("No memory to append systemd environment line\n");
1252 _dbus_string_free (&envline);
1256 _dbus_string_free (&envline);
1259 key_link = _dbus_list_get_next_link (&keys, key_link);
1260 value_link = _dbus_list_get_next_link (&values, value_link);
1263 /* FIXME: We can fail early having set only some of the environment variables,
1264 * (because of OOM failure). It's sort of hard to fix and it doesn't really
1265 * matter, so we're punting for now.
1267 if (key_link != NULL)
1269 if (systemd_message != NULL)
1270 dbus_message_iter_abandon_container (&iter, &systemd_iter);
1274 if (systemd_message != NULL)
1276 if (!dbus_message_iter_close_container (&iter, &systemd_iter))
1278 BUS_SET_OOM (error);
1279 _dbus_verbose ("No memory to close systemd message container\n");
1283 if (!bus_driver_send_or_activate (transaction, systemd_message, error))
1285 _DBUS_ASSERT_ERROR_IS_SET (error);
1286 _dbus_verbose ("bus_driver_send_or_activate() failed\n");
1291 if (!send_ack_reply (connection, transaction,
1298 if (systemd_message != NULL)
1299 dbus_message_unref (systemd_message);
1300 _dbus_list_clear (&keys);
1301 _dbus_list_clear (&values);
1306 bus_driver_handle_add_match (DBusConnection *connection,
1307 BusTransaction *transaction,
1308 DBusMessage *message,
1312 const char *text, *bustype;
1314 BusMatchmaker *matchmaker;
1316 BusContext *context;
1318 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1323 context = bus_transaction_get_context (transaction);
1324 limit = bus_context_get_max_match_rules_per_connection (context);
1326 if (bus_connection_get_n_match_rules (connection) >= limit)
1328 DBusError tmp_error;
1330 dbus_error_init (&tmp_error);
1331 dbus_set_error (&tmp_error, DBUS_ERROR_LIMITS_EXCEEDED,
1332 "Connection \"%s\" is not allowed to add more match rules "
1333 "(increase limits in configuration file if required; "
1334 "max_match_rules_per_connection=%d)",
1335 bus_connection_is_active (connection) ?
1336 bus_connection_get_name (connection) :
1339 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING, "%s",
1341 dbus_move_error (&tmp_error, error);
1345 if (!dbus_message_get_args (message, error,
1346 DBUS_TYPE_STRING, &text,
1349 _dbus_verbose ("No memory to get arguments to AddMatch\n");
1353 _dbus_string_init_const (&str, text);
1355 rule = bus_match_rule_parse (connection, &str, error);
1359 bustype = bus_context_get_type (context);
1360 if (bus_match_rule_get_client_is_eavesdropping (rule) &&
1361 !bus_apparmor_allows_eavesdropping (connection, bustype, error))
1364 matchmaker = bus_connection_get_matchmaker (connection);
1366 if (!bus_matchmaker_add_rule (matchmaker, rule))
1368 BUS_SET_OOM (error);
1372 if (!send_ack_reply (connection, transaction,
1375 bus_matchmaker_remove_rule (matchmaker, rule);
1379 bus_match_rule_unref (rule);
1384 _DBUS_ASSERT_ERROR_IS_SET (error);
1386 bus_match_rule_unref (rule);
1391 bus_driver_handle_remove_match (DBusConnection *connection,
1392 BusTransaction *transaction,
1393 DBusMessage *message,
1399 BusMatchmaker *matchmaker;
1401 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1406 if (!dbus_message_get_args (message, error,
1407 DBUS_TYPE_STRING, &text,
1410 _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
1414 _dbus_string_init_const (&str, text);
1416 rule = bus_match_rule_parse (connection, &str, error);
1420 /* Send the ack before we remove the rule, since the ack is undone
1421 * on transaction cancel, but rule removal isn't.
1423 if (!send_ack_reply (connection, transaction,
1427 matchmaker = bus_connection_get_matchmaker (connection);
1429 if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
1432 bus_match_rule_unref (rule);
1437 _DBUS_ASSERT_ERROR_IS_SET (error);
1439 bus_match_rule_unref (rule);
1444 bus_driver_handle_get_service_owner (DBusConnection *connection,
1445 BusTransaction *transaction,
1446 DBusMessage *message,
1450 const char *base_name;
1452 BusRegistry *registry;
1453 BusService *service;
1456 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1458 registry = bus_connection_get_registry (connection);
1463 if (! dbus_message_get_args (message, error,
1464 DBUS_TYPE_STRING, &text,
1468 _dbus_string_init_const (&str, text);
1469 service = bus_registry_lookup (registry, &str);
1470 if (service == NULL &&
1471 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1473 /* ORG_FREEDESKTOP_DBUS owns itself */
1474 base_name = DBUS_SERVICE_DBUS;
1476 else if (service == NULL)
1478 dbus_set_error (error,
1479 DBUS_ERROR_NAME_HAS_NO_OWNER,
1480 "Could not get owner of name '%s': no such name", text);
1485 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1486 if (base_name == NULL)
1488 /* FIXME - how is this error possible? */
1489 dbus_set_error (error,
1491 "Could not determine unique name for '%s'", text);
1494 _dbus_assert (*base_name == ':');
1497 _dbus_assert (base_name != NULL);
1499 reply = dbus_message_new_method_return (message);
1503 if (! dbus_message_append_args (reply,
1504 DBUS_TYPE_STRING, &base_name,
1508 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1511 dbus_message_unref (reply);
1516 BUS_SET_OOM (error);
1519 _DBUS_ASSERT_ERROR_IS_SET (error);
1521 dbus_message_unref (reply);
1526 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1527 BusTransaction *transaction,
1528 DBusMessage *message,
1531 static const char dbus_service_name[] = DBUS_SERVICE_DBUS;
1534 DBusList *base_names;
1537 BusRegistry *registry;
1538 BusService *service;
1540 DBusMessageIter iter, array_iter;
1542 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1544 registry = bus_connection_get_registry (connection);
1550 if (! dbus_message_get_args (message, error,
1551 DBUS_TYPE_STRING, &text,
1555 _dbus_string_init_const (&str, text);
1556 service = bus_registry_lookup (registry, &str);
1557 if (service == NULL &&
1558 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1560 /* ORG_FREEDESKTOP_DBUS owns itself */
1561 if (! _dbus_list_append (&base_names, (char *) dbus_service_name))
1564 else if (service == NULL)
1566 dbus_set_error (error,
1567 DBUS_ERROR_NAME_HAS_NO_OWNER,
1568 "Could not get owners of name '%s': no such name", text);
1573 if (!bus_service_list_queued_owners (service,
1579 _dbus_assert (base_names != NULL);
1581 reply = dbus_message_new_method_return (message);
1585 dbus_message_iter_init_append (reply, &iter);
1586 if (!dbus_message_iter_open_container (&iter,
1588 DBUS_TYPE_STRING_AS_STRING,
1592 link = _dbus_list_get_first_link (&base_names);
1593 while (link != NULL)
1597 _dbus_assert (link->data != NULL);
1598 uname = (char *)link->data;
1600 if (!dbus_message_iter_append_basic (&array_iter,
1605 link = _dbus_list_get_next_link (&base_names, link);
1608 if (! dbus_message_iter_close_container (&iter, &array_iter))
1612 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1615 dbus_message_unref (reply);
1620 BUS_SET_OOM (error);
1623 _DBUS_ASSERT_ERROR_IS_SET (error);
1625 dbus_message_unref (reply);
1628 _dbus_list_clear (&base_names);
1634 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1635 BusTransaction *transaction,
1636 DBusMessage *message,
1639 DBusConnection *conn;
1642 dbus_uint32_t uid32;
1643 const char *service;
1644 BusDriverFound found;
1646 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1650 found = bus_driver_get_conn_helper (connection, message, "UID", &service,
1654 case BUS_DRIVER_FOUND_SELF:
1655 uid = _dbus_getuid ();
1657 case BUS_DRIVER_FOUND_PEER:
1658 if (!dbus_connection_get_unix_user (conn, &uid))
1659 uid = DBUS_UID_UNSET;
1661 case BUS_DRIVER_FOUND_ERROR:
1667 if (uid == DBUS_UID_UNSET)
1669 dbus_set_error (error,
1671 "Could not determine UID for '%s'", service);
1675 reply = dbus_message_new_method_return (message);
1680 if (! dbus_message_append_args (reply,
1681 DBUS_TYPE_UINT32, &uid32,
1685 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1688 dbus_message_unref (reply);
1693 BUS_SET_OOM (error);
1696 _DBUS_ASSERT_ERROR_IS_SET (error);
1698 dbus_message_unref (reply);
1703 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1704 BusTransaction *transaction,
1705 DBusMessage *message,
1708 DBusConnection *conn;
1711 dbus_uint32_t pid32;
1712 const char *service;
1713 BusDriverFound found;
1715 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1719 found = bus_driver_get_conn_helper (connection, message, "PID", &service,
1723 case BUS_DRIVER_FOUND_SELF:
1724 pid = _dbus_getpid ();
1726 case BUS_DRIVER_FOUND_PEER:
1727 if (!dbus_connection_get_unix_process_id (conn, &pid))
1728 pid = DBUS_PID_UNSET;
1730 case BUS_DRIVER_FOUND_ERROR:
1736 if (pid == DBUS_PID_UNSET)
1738 dbus_set_error (error,
1739 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1740 "Could not determine PID for '%s'", service);
1744 reply = dbus_message_new_method_return (message);
1749 if (! dbus_message_append_args (reply,
1750 DBUS_TYPE_UINT32, &pid32,
1754 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1757 dbus_message_unref (reply);
1762 BUS_SET_OOM (error);
1765 _DBUS_ASSERT_ERROR_IS_SET (error);
1767 dbus_message_unref (reply);
1772 bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
1773 BusTransaction *transaction,
1774 DBusMessage *message,
1777 DBusConnection *conn;
1780 dbus_int32_t data_size;
1781 const char *service;
1782 BusDriverFound found;
1784 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1788 found = bus_driver_get_conn_helper (connection, message, "audit session data",
1789 &service, &conn, error);
1791 if (found == BUS_DRIVER_FOUND_ERROR)
1794 reply = dbus_message_new_method_return (message);
1798 /* We don't know how to find "ADT audit session data" for the bus daemon
1799 * itself. Is that even meaningful?
1800 * FIXME: Implement this or briefly note it makes no sense.
1802 if (found != BUS_DRIVER_FOUND_PEER ||
1803 !dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) ||
1806 dbus_set_error (error,
1807 DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
1808 "Could not determine audit session data for '%s'", service);
1812 if (! dbus_message_append_args (reply,
1813 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
1817 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1820 dbus_message_unref (reply);
1825 BUS_SET_OOM (error);
1828 _DBUS_ASSERT_ERROR_IS_SET (error);
1830 dbus_message_unref (reply);
1835 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1836 BusTransaction *transaction,
1837 DBusMessage *message,
1840 DBusConnection *conn;
1842 BusSELinuxID *context;
1843 const char *service;
1844 BusDriverFound found;
1846 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1850 found = bus_driver_get_conn_helper (connection, message, "security context",
1851 &service, &conn, error);
1853 if (found == BUS_DRIVER_FOUND_ERROR)
1856 reply = dbus_message_new_method_return (message);
1860 /* FIXME: Obtain the SELinux security context for the bus daemon itself */
1861 if (found == BUS_DRIVER_FOUND_PEER)
1862 context = bus_connection_get_selinux_id (conn);
1868 dbus_set_error (error,
1869 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1870 "Could not determine security context for '%s'", service);
1874 if (! bus_selinux_append_context (reply, context, error))
1877 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1880 dbus_message_unref (reply);
1885 BUS_SET_OOM (error);
1888 _DBUS_ASSERT_ERROR_IS_SET (error);
1890 dbus_message_unref (reply);
1895 bus_driver_handle_get_connection_credentials (DBusConnection *connection,
1896 BusTransaction *transaction,
1897 DBusMessage *message,
1900 DBusConnection *conn;
1902 DBusMessageIter reply_iter;
1903 DBusMessageIter array_iter;
1904 unsigned long ulong_uid, ulong_pid;
1906 const char *service;
1907 BusDriverFound found;
1909 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1913 found = bus_driver_get_conn_helper (connection, message, "credentials",
1914 &service, &conn, error);
1918 case BUS_DRIVER_FOUND_SELF:
1919 ulong_pid = _dbus_getpid ();
1920 ulong_uid = _dbus_getuid ();
1923 case BUS_DRIVER_FOUND_PEER:
1924 if (!dbus_connection_get_unix_process_id (conn, &ulong_pid))
1925 ulong_pid = DBUS_PID_UNSET;
1926 if (!dbus_connection_get_unix_user (conn, &ulong_uid))
1927 ulong_uid = DBUS_UID_UNSET;
1929 case BUS_DRIVER_FOUND_ERROR:
1935 reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
1939 /* we can't represent > 32-bit pids; if your system needs them, please
1940 * add ProcessID64 to the spec or something */
1941 if (ulong_pid <= _DBUS_UINT32_MAX && ulong_pid != DBUS_PID_UNSET &&
1942 !_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_pid))
1945 /* we can't represent > 32-bit uids; if your system needs them, please
1946 * add UnixUserID64 to the spec or something */
1947 if (ulong_uid <= _DBUS_UINT32_MAX && ulong_uid != DBUS_UID_UNSET &&
1948 !_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_uid))
1951 /* FIXME: Obtain the Windows user of the bus daemon itself */
1952 if (found == BUS_DRIVER_FOUND_PEER &&
1953 dbus_connection_get_windows_user (conn, &s))
1961 _dbus_string_init_const (&str, s);
1962 result = _dbus_validate_utf8 (&str, 0, _dbus_string_get_length (&str));
1963 _dbus_string_free (&str);
1966 if (!_dbus_asv_add_string (&array_iter, "WindowsSID", s))
1975 /* FIXME: Obtain the security label for the bus daemon itself */
1976 if (found == BUS_DRIVER_FOUND_PEER &&
1977 _dbus_connection_get_linux_security_label (conn, &s))
1982 /* use the GVariant bytestring convention for strings of unknown
1983 * encoding: include the \0 in the payload, for zero-copy reading */
1984 if (!_dbus_asv_add_byte_array (&array_iter, "LinuxSecurityLabel",
1994 if (!_dbus_asv_close (&reply_iter, &array_iter))
1997 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1999 /* this time we don't want to close the iterator again, so just
2000 * get rid of the message */
2001 dbus_message_unref (reply);
2006 dbus_message_unref (reply);
2011 BUS_SET_OOM (error);
2014 _DBUS_ASSERT_ERROR_IS_SET (error);
2018 _dbus_asv_abandon (&reply_iter, &array_iter);
2019 dbus_message_unref (reply);
2026 bus_driver_handle_reload_config (DBusConnection *connection,
2027 BusTransaction *transaction,
2028 DBusMessage *message,
2031 BusContext *context;
2034 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2038 context = bus_connection_get_context (connection);
2039 if (!bus_context_reload_config (context, error))
2042 reply = dbus_message_new_method_return (message);
2046 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2049 dbus_message_unref (reply);
2053 BUS_SET_OOM (error);
2056 _DBUS_ASSERT_ERROR_IS_SET (error);
2058 dbus_message_unref (reply);
2062 #ifdef DBUS_ENABLE_VERBOSE_MODE
2064 bus_driver_handle_enable_verbose (DBusConnection *connection,
2065 BusTransaction *transaction,
2066 DBusMessage *message,
2069 DBusMessage *reply = NULL;
2071 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2073 reply = dbus_message_new_method_return (message);
2077 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2080 _dbus_set_verbose(TRUE);
2082 dbus_message_unref (reply);
2086 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2088 BUS_SET_OOM (error);
2091 dbus_message_unref (reply);
2096 bus_driver_handle_disable_verbose (DBusConnection *connection,
2097 BusTransaction *transaction,
2098 DBusMessage *message,
2101 DBusMessage *reply = NULL;
2103 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2105 reply = dbus_message_new_method_return (message);
2109 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2112 _dbus_set_verbose(FALSE);
2114 dbus_message_unref (reply);
2118 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2120 BUS_SET_OOM (error);
2123 dbus_message_unref (reply);
2129 bus_driver_handle_get_id (DBusConnection *connection,
2130 BusTransaction *transaction,
2131 DBusMessage *message,
2134 BusContext *context;
2137 const char *v_STRING;
2139 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2141 if (!_dbus_string_init (&uuid))
2143 BUS_SET_OOM (error);
2149 context = bus_connection_get_context (connection);
2150 if (!bus_context_get_id (context, &uuid))
2153 reply = dbus_message_new_method_return (message);
2157 v_STRING = _dbus_string_get_const_data (&uuid);
2158 if (!dbus_message_append_args (reply,
2159 DBUS_TYPE_STRING, &v_STRING,
2163 _dbus_assert (dbus_message_has_signature (reply, "s"));
2165 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2168 _dbus_string_free (&uuid);
2169 dbus_message_unref (reply);
2173 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2175 BUS_SET_OOM (error);
2178 dbus_message_unref (reply);
2179 _dbus_string_free (&uuid);
2184 bus_driver_handle_become_monitor (DBusConnection *connection,
2185 BusTransaction *transaction,
2186 DBusMessage *message,
2189 char **match_rules = NULL;
2190 const char *bustype;
2191 BusContext *context;
2193 DBusList *rules = NULL;
2198 dbus_uint32_t flags;
2199 dbus_bool_t ret = FALSE;
2201 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2203 if (!bus_driver_check_message_is_for_us (message, error))
2206 context = bus_transaction_get_context (transaction);
2207 bustype = context ? bus_context_get_type (context) : NULL;
2208 if (!bus_apparmor_allows_eavesdropping (connection, bustype, error))
2211 if (!bus_driver_check_caller_is_privileged (connection, transaction,
2215 if (!dbus_message_get_args (message, error,
2216 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &match_rules, &n_match_rules,
2217 DBUS_TYPE_UINT32, &flags,
2223 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2224 "BecomeMonitor does not support any flags yet");
2228 /* Special case: a zero-length array becomes [""] */
2229 if (n_match_rules == 0)
2231 match_rules = dbus_malloc (2 * sizeof (char *));
2233 if (match_rules == NULL)
2235 BUS_SET_OOM (error);
2239 match_rules[0] = _dbus_strdup ("");
2241 if (match_rules[0] == NULL)
2243 BUS_SET_OOM (error);
2247 match_rules[1] = NULL;
2251 for (i = 0; i < n_match_rules; i++)
2253 _dbus_string_init_const (&str, match_rules[i]);
2254 rule = bus_match_rule_parse (connection, &str, error);
2259 /* monitors always eavesdrop */
2260 bus_match_rule_set_client_is_eavesdropping (rule, TRUE);
2262 if (!_dbus_list_append (&rules, rule))
2264 BUS_SET_OOM (error);
2265 bus_match_rule_unref (rule);
2270 /* Send the ack before we remove the rule, since the ack is undone
2271 * on transaction cancel, but becoming a monitor isn't.
2273 if (!send_ack_reply (connection, transaction, message, error))
2276 if (!bus_connection_be_monitor (connection, transaction, &rules, error))
2283 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2285 _DBUS_ASSERT_ERROR_IS_SET (error);
2287 for (iter = _dbus_list_get_first_link (&rules);
2289 iter = _dbus_list_get_next_link (&rules, iter))
2290 bus_match_rule_unref (iter->data);
2292 _dbus_list_clear (&rules);
2294 dbus_free_string_array (match_rules);
2301 const char *in_args;
2302 const char *out_args;
2303 dbus_bool_t (* handler) (DBusConnection *connection,
2304 BusTransaction *transaction,
2305 DBusMessage *message,
2309 /* For speed it might be useful to sort this in order of
2310 * frequency of use (but doesn't matter with only a few items
2313 static const MessageHandler dbus_message_handlers[] = {
2316 DBUS_TYPE_STRING_AS_STRING,
2317 bus_driver_handle_hello },
2319 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
2320 DBUS_TYPE_UINT32_AS_STRING,
2321 bus_driver_handle_acquire_service },
2323 DBUS_TYPE_STRING_AS_STRING,
2324 DBUS_TYPE_UINT32_AS_STRING,
2325 bus_driver_handle_release_service },
2326 { "StartServiceByName",
2327 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
2328 DBUS_TYPE_UINT32_AS_STRING,
2329 bus_driver_handle_activate_service },
2330 { "UpdateActivationEnvironment",
2331 DBUS_TYPE_ARRAY_AS_STRING DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
2333 bus_driver_handle_update_activation_environment },
2335 DBUS_TYPE_STRING_AS_STRING,
2336 DBUS_TYPE_BOOLEAN_AS_STRING,
2337 bus_driver_handle_service_exists },
2340 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
2341 bus_driver_handle_list_services },
2342 { "ListActivatableNames",
2344 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
2345 bus_driver_handle_list_activatable_services },
2347 DBUS_TYPE_STRING_AS_STRING,
2349 bus_driver_handle_add_match },
2351 DBUS_TYPE_STRING_AS_STRING,
2353 bus_driver_handle_remove_match },
2355 DBUS_TYPE_STRING_AS_STRING,
2356 DBUS_TYPE_STRING_AS_STRING,
2357 bus_driver_handle_get_service_owner },
2358 { "ListQueuedOwners",
2359 DBUS_TYPE_STRING_AS_STRING,
2360 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
2361 bus_driver_handle_list_queued_owners },
2362 { "GetConnectionUnixUser",
2363 DBUS_TYPE_STRING_AS_STRING,
2364 DBUS_TYPE_UINT32_AS_STRING,
2365 bus_driver_handle_get_connection_unix_user },
2366 { "GetConnectionUnixProcessID",
2367 DBUS_TYPE_STRING_AS_STRING,
2368 DBUS_TYPE_UINT32_AS_STRING,
2369 bus_driver_handle_get_connection_unix_process_id },
2370 { "GetAdtAuditSessionData",
2371 DBUS_TYPE_STRING_AS_STRING,
2372 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
2373 bus_driver_handle_get_adt_audit_session_data },
2374 { "GetConnectionSELinuxSecurityContext",
2375 DBUS_TYPE_STRING_AS_STRING,
2376 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
2377 bus_driver_handle_get_connection_selinux_security_context },
2381 bus_driver_handle_reload_config },
2384 DBUS_TYPE_STRING_AS_STRING,
2385 bus_driver_handle_get_id },
2386 { "GetConnectionCredentials", "s", "a{sv}",
2387 bus_driver_handle_get_connection_credentials },
2388 { NULL, NULL, NULL, NULL }
2391 static dbus_bool_t bus_driver_handle_introspect (DBusConnection *,
2392 BusTransaction *, DBusMessage *, DBusError *);
2394 static const MessageHandler introspectable_message_handlers[] = {
2395 { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
2396 { NULL, NULL, NULL, NULL }
2399 static const MessageHandler monitoring_message_handlers[] = {
2400 { "BecomeMonitor", "asu", "", bus_driver_handle_become_monitor },
2401 { NULL, NULL, NULL, NULL }
2404 #ifdef DBUS_ENABLE_VERBOSE_MODE
2405 static const MessageHandler verbose_message_handlers[] = {
2406 { "EnableVerbose", "", "", bus_driver_handle_enable_verbose},
2407 { "DisableVerbose", "", "", bus_driver_handle_disable_verbose},
2408 { NULL, NULL, NULL, NULL }
2412 #ifdef DBUS_ENABLE_STATS
2413 static const MessageHandler stats_message_handlers[] = {
2414 { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
2415 { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
2416 { "GetAllMatchRules", "", "a{sas}", bus_stats_handle_get_all_match_rules },
2417 { NULL, NULL, NULL, NULL }
2423 const MessageHandler *message_handlers;
2424 const char *extra_introspection;
2427 /* These should ideally be sorted by frequency of use, although it
2428 * probably doesn't matter with this few items */
2429 static InterfaceHandler interface_handlers[] = {
2430 { DBUS_INTERFACE_DBUS, dbus_message_handlers,
2431 " <signal name=\"NameOwnerChanged\">\n"
2432 " <arg type=\"s\"/>\n"
2433 " <arg type=\"s\"/>\n"
2434 " <arg type=\"s\"/>\n"
2436 " <signal name=\"NameLost\">\n"
2437 " <arg type=\"s\"/>\n"
2439 " <signal name=\"NameAcquired\">\n"
2440 " <arg type=\"s\"/>\n"
2442 { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
2443 { DBUS_INTERFACE_MONITORING, monitoring_message_handlers, NULL },
2444 #ifdef DBUS_ENABLE_VERBOSE_MODE
2445 { DBUS_INTERFACE_VERBOSE, verbose_message_handlers, NULL },
2447 #ifdef DBUS_ENABLE_STATS
2448 { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
2450 { NULL, NULL, NULL }
2454 write_args_for_direction (DBusString *xml,
2455 const char *signature,
2458 DBusTypeReader typereader;
2462 _dbus_string_init_const (&sigstr, signature);
2463 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
2465 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
2467 const DBusString *subsig;
2470 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
2471 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
2474 if (!_dbus_string_append_len (xml,
2475 _dbus_string_get_const_data (subsig) + start,
2478 if (!_dbus_string_append (xml, "\"/>\n"))
2481 _dbus_type_reader_next (&typereader);
2489 bus_driver_generate_introspect_string (DBusString *xml)
2491 const InterfaceHandler *ih;
2492 const MessageHandler *mh;
2494 if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
2496 if (!_dbus_string_append (xml, "<node>\n"))
2499 for (ih = interface_handlers; ih->name != NULL; ih++)
2501 if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n",
2505 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2507 if (!_dbus_string_append_printf (xml, " <method name=\"%s\">\n",
2511 if (!write_args_for_direction (xml, mh->in_args, TRUE))
2514 if (!write_args_for_direction (xml, mh->out_args, FALSE))
2517 if (!_dbus_string_append (xml, " </method>\n"))
2521 if (ih->extra_introspection != NULL &&
2522 !_dbus_string_append (xml, ih->extra_introspection))
2525 if (!_dbus_string_append (xml, " </interface>\n"))
2529 if (!_dbus_string_append (xml, "</node>\n"))
2536 bus_driver_handle_introspect (DBusConnection *connection,
2537 BusTransaction *transaction,
2538 DBusMessage *message,
2543 const char *v_STRING;
2545 _dbus_verbose ("Introspect() on bus driver\n");
2547 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2551 if (! dbus_message_get_args (message, error,
2554 _DBUS_ASSERT_ERROR_IS_SET (error);
2558 if (!_dbus_string_init (&xml))
2560 BUS_SET_OOM (error);
2564 if (!bus_driver_generate_introspect_string (&xml))
2567 v_STRING = _dbus_string_get_const_data (&xml);
2569 reply = dbus_message_new_method_return (message);
2573 if (! dbus_message_append_args (reply,
2574 DBUS_TYPE_STRING, &v_STRING,
2578 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2581 dbus_message_unref (reply);
2582 _dbus_string_free (&xml);
2587 BUS_SET_OOM (error);
2590 dbus_message_unref (reply);
2592 _dbus_string_free (&xml);
2598 * Set @error and return FALSE if the message is not directed to the
2599 * dbus-daemon by its canonical object path. This is hardening against
2600 * system services with poorly-written security policy files, which
2601 * might allow sending dangerously broad equivalence classes of messages
2602 * such as "anything with this assumed-to-be-safe object path".
2604 * dbus-daemon is unusual in that it normally ignores the object path
2605 * of incoming messages; we need to keep that behaviour for the "read"
2606 * read-only method calls like GetConnectionUnixUser for backwards
2607 * compatibility, but it seems safer to be more restrictive for things
2608 * intended to be root-only or privileged-developers-only.
2610 * It is possible that there are other system services with the same
2611 * quirk as dbus-daemon.
2614 bus_driver_check_message_is_for_us (DBusMessage *message,
2617 if (!dbus_message_has_path (message, DBUS_PATH_DBUS))
2619 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
2620 "Method '%s' is only available at the canonical object path '%s'",
2621 dbus_message_get_member (message), DBUS_PATH_DBUS);
2630 bus_driver_handle_message (DBusConnection *connection,
2631 BusTransaction *transaction,
2632 DBusMessage *message,
2635 const char *name, *interface;
2636 const InterfaceHandler *ih;
2637 const MessageHandler *mh;
2638 dbus_bool_t found_interface = FALSE;
2640 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2642 if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
2644 BusContext *context;
2645 DBusConnection *systemd;
2647 /* This is a directed signal, not a method call, so the log message
2648 * is a little weird (it talks about "calling" ActivationFailure),
2649 * but it's close enough */
2650 if (!bus_driver_check_caller_is_privileged (connection,
2656 context = bus_connection_get_context (connection);
2657 systemd = bus_driver_get_owner_of_name (connection,
2658 "org.freedesktop.systemd1");
2660 if (systemd != connection)
2662 const char *attacker;
2664 attacker = bus_connection_get_name (connection);
2665 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY,
2666 "Ignoring forged ActivationFailure message from "
2667 "connection %s (%s)",
2668 attacker ? attacker : "(unauthenticated)",
2669 bus_connection_get_loginfo (connection));
2674 if (!bus_context_get_systemd_activation (context))
2676 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
2677 "Ignoring unexpected ActivationFailure message "
2678 "while not using systemd activation");
2682 return dbus_activation_systemd_failure(bus_context_get_activation(context), message);
2685 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
2687 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
2688 return TRUE; /* we just ignore this */
2691 /* may be NULL, which means "any interface will do" */
2692 interface = dbus_message_get_interface (message);
2694 _dbus_assert (dbus_message_get_member (message) != NULL);
2696 name = dbus_message_get_member (message);
2698 _dbus_verbose ("Driver got a method call: %s\n", name);
2700 /* security checks should have kept this from getting here */
2701 _dbus_assert (dbus_message_get_sender (message) != NULL ||
2702 strcmp (name, "Hello") == 0);
2704 for (ih = interface_handlers; ih->name != NULL; ih++)
2706 if (interface != NULL && strcmp (interface, ih->name) != 0)
2709 found_interface = TRUE;
2711 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2713 if (strcmp (mh->name, name) != 0)
2716 _dbus_verbose ("Found driver handler for %s\n", name);
2718 if (!dbus_message_has_signature (message, mh->in_args))
2720 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2721 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
2722 name, dbus_message_get_signature (message),
2725 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2726 "Call to %s has wrong args (%s, expected %s)\n",
2727 name, dbus_message_get_signature (message),
2729 _DBUS_ASSERT_ERROR_IS_SET (error);
2733 if ((* mh->handler) (connection, transaction, message, error))
2735 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2736 _dbus_verbose ("Driver handler succeeded\n");
2741 _DBUS_ASSERT_ERROR_IS_SET (error);
2742 _dbus_verbose ("Driver handler returned failure\n");
2748 _dbus_verbose ("No driver handler for message \"%s\"\n",
2751 dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
2752 "%s does not understand message %s",
2753 DBUS_SERVICE_DBUS, name);
2759 bus_driver_remove_connection (DBusConnection *connection)
2761 /* FIXME 1.0 Does nothing for now, should unregister the connection
2762 * with the bus driver.