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, message))
263 switch (bus_dispatch_matches (transaction, NULL, NULL, message, NULL, error))
265 case BUS_RESULT_TRUE:
268 case BUS_RESULT_FALSE:
272 /* should never happen */
273 _dbus_assert_not_reached ("bus_dispatch_matches returned BUS_RESULT_LATER unexpectedly");
277 dbus_message_unref (message);
282 dbus_message_unref (message);
288 bus_driver_send_service_lost (DBusConnection *connection,
289 const char *service_name,
290 BusTransaction *transaction,
293 DBusMessage *message;
295 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
297 message = dbus_message_new_signal (DBUS_PATH_DBUS,
307 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
308 !dbus_message_append_args (message,
309 DBUS_TYPE_STRING, &service_name,
312 dbus_message_unref (message);
317 if (!bus_transaction_send_from_driver (transaction, connection, message))
319 dbus_message_unref (message);
325 dbus_message_unref (message);
331 bus_driver_send_service_acquired (DBusConnection *connection,
332 const char *service_name,
333 BusTransaction *transaction,
336 DBusMessage *message;
338 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
340 message = dbus_message_new_signal (DBUS_PATH_DBUS,
350 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
351 !dbus_message_append_args (message,
352 DBUS_TYPE_STRING, &service_name,
355 dbus_message_unref (message);
360 if (!bus_transaction_send_from_driver (transaction, connection, message))
362 dbus_message_unref (message);
368 dbus_message_unref (message);
374 create_unique_client_name (BusRegistry *registry,
377 /* We never want to use the same unique client name twice, because
378 * we want to guarantee that if you send a message to a given unique
379 * name, you always get the same application. So we use two numbers
380 * for INT_MAX * INT_MAX combinations, should be pretty safe against
383 /* FIXME these should be in BusRegistry rather than static vars */
384 static int next_major_number = 0;
385 static int next_minor_number = 0;
388 len = _dbus_string_get_length (str);
392 /* start out with 1-0, go to 1-1, 1-2, 1-3,
393 * up to 1-MAXINT, then 2-0, 2-1, etc.
395 if (next_minor_number <= 0)
397 next_major_number += 1;
398 next_minor_number = 0;
399 if (next_major_number <= 0)
400 _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
403 _dbus_assert (next_major_number > 0);
404 _dbus_assert (next_minor_number >= 0);
406 /* appname:MAJOR-MINOR */
408 if (!_dbus_string_append (str, ":"))
411 if (!_dbus_string_append_int (str, next_major_number))
414 if (!_dbus_string_append (str, "."))
417 if (!_dbus_string_append_int (str, next_minor_number))
420 next_minor_number += 1;
422 /* Check if a client with the name exists */
423 if (bus_registry_lookup (registry, str) == NULL)
426 /* drop the number again, try the next one. */
427 _dbus_string_set_length (str, len);
434 bus_driver_handle_hello (DBusConnection *connection,
435 BusTransaction *transaction,
436 DBusMessage *message,
439 DBusString unique_name;
442 BusRegistry *registry;
443 BusConnections *connections;
445 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
447 if (bus_connection_is_active (connection))
449 /* We already handled an Hello message for this connection. */
450 dbus_set_error (error, DBUS_ERROR_FAILED,
451 "Already handled an Hello message");
452 return BUS_RESULT_FALSE;
455 /* Note that when these limits are exceeded we don't disconnect the
456 * connection; we just sort of leave it hanging there until it times
457 * out or disconnects itself or is dropped due to the max number of
458 * incomplete connections. It's even OK if the connection wants to
459 * retry the hello message, we support that.
461 connections = bus_connection_get_connections (connection);
462 if (!bus_connections_check_limits (connections, connection,
465 _DBUS_ASSERT_ERROR_IS_SET (error);
466 return BUS_RESULT_FALSE;
469 if (!_dbus_string_init (&unique_name))
472 return BUS_RESULT_FALSE;
475 retval = BUS_RESULT_FALSE;
477 registry = bus_connection_get_registry (connection);
479 if (!create_unique_client_name (registry, &unique_name))
485 if (!bus_connection_complete (connection, &unique_name, error))
487 _DBUS_ASSERT_ERROR_IS_SET (error);
491 if (!dbus_message_set_sender (message,
492 bus_connection_get_name (connection)))
498 if (!bus_driver_send_welcome_message (connection, message, transaction, error))
501 /* Create the service */
502 service = bus_registry_ensure (registry,
503 &unique_name, connection, 0, transaction, error);
507 _dbus_assert (bus_connection_is_active (connection));
508 retval = BUS_RESULT_TRUE;
511 _dbus_string_free (&unique_name);
516 bus_driver_send_welcome_message (DBusConnection *connection,
517 DBusMessage *hello_message,
518 BusTransaction *transaction,
521 DBusMessage *welcome;
524 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
526 name = bus_connection_get_name (connection);
527 _dbus_assert (name != NULL);
529 welcome = dbus_message_new_method_return (hello_message);
536 if (!dbus_message_append_args (welcome,
537 DBUS_TYPE_STRING, &name,
540 dbus_message_unref (welcome);
545 _dbus_assert (dbus_message_has_signature (welcome, DBUS_TYPE_STRING_AS_STRING));
547 if (!bus_transaction_send_from_driver (transaction, connection, welcome))
549 dbus_message_unref (welcome);
555 dbus_message_unref (welcome);
561 bus_driver_handle_list_services (DBusConnection *connection,
562 BusTransaction *transaction,
563 DBusMessage *message,
569 BusRegistry *registry;
571 DBusMessageIter iter;
574 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
576 registry = bus_connection_get_registry (connection);
578 reply = dbus_message_new_method_return (message);
582 return BUS_RESULT_FALSE;
585 if (!bus_registry_list_services (registry, &services, &len))
587 dbus_message_unref (reply);
589 return BUS_RESULT_FALSE;
592 dbus_message_iter_init_append (reply, &iter);
594 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
595 DBUS_TYPE_STRING_AS_STRING,
598 dbus_free_string_array (services);
599 dbus_message_unref (reply);
601 return BUS_RESULT_FALSE;
605 /* Include the bus driver in the list */
606 const char *v_STRING = DBUS_SERVICE_DBUS;
607 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
610 dbus_free_string_array (services);
611 dbus_message_unref (reply);
613 return BUS_RESULT_FALSE;
620 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
623 dbus_free_string_array (services);
624 dbus_message_unref (reply);
626 return BUS_RESULT_FALSE;
631 dbus_free_string_array (services);
633 if (!dbus_message_iter_close_container (&iter, &sub))
635 dbus_message_unref (reply);
637 return BUS_RESULT_FALSE;
640 if (!bus_transaction_send_from_driver (transaction, connection, reply))
642 dbus_message_unref (reply);
644 return BUS_RESULT_FALSE;
648 dbus_message_unref (reply);
649 return BUS_RESULT_TRUE;
654 bus_driver_handle_list_activatable_services (DBusConnection *connection,
655 BusTransaction *transaction,
656 DBusMessage *message,
662 BusActivation *activation;
664 DBusMessageIter iter;
667 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
669 activation = bus_connection_get_activation (connection);
671 reply = dbus_message_new_method_return (message);
675 return BUS_RESULT_FALSE;
678 if (!bus_activation_list_services (activation, &services, &len))
680 dbus_message_unref (reply);
682 return BUS_RESULT_FALSE;
685 dbus_message_iter_init_append (reply, &iter);
687 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
688 DBUS_TYPE_STRING_AS_STRING,
691 dbus_free_string_array (services);
692 dbus_message_unref (reply);
694 return BUS_RESULT_FALSE;
698 /* Include the bus driver in the list */
699 const char *v_STRING = DBUS_SERVICE_DBUS;
700 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
703 dbus_free_string_array (services);
704 dbus_message_unref (reply);
706 return BUS_RESULT_FALSE;
713 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
716 dbus_free_string_array (services);
717 dbus_message_unref (reply);
719 return BUS_RESULT_FALSE;
724 dbus_free_string_array (services);
726 if (!dbus_message_iter_close_container (&iter, &sub))
728 dbus_message_unref (reply);
730 return BUS_RESULT_FALSE;
733 if (!bus_transaction_send_from_driver (transaction, connection, reply))
735 dbus_message_unref (reply);
737 return BUS_RESULT_FALSE;
741 dbus_message_unref (reply);
742 return BUS_RESULT_TRUE;
747 bus_driver_handle_acquire_service (DBusConnection *connection,
748 BusTransaction *transaction,
749 DBusMessage *message,
753 DBusString service_name;
755 dbus_uint32_t service_reply;
758 BusRegistry *registry;
760 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
762 registry = bus_connection_get_registry (connection);
764 if (!dbus_message_get_args (message, error,
765 DBUS_TYPE_STRING, &name,
766 DBUS_TYPE_UINT32, &flags,
768 return BUS_RESULT_FALSE;
770 _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
772 retval = BUS_RESULT_FALSE;
775 _dbus_string_init_const (&service_name, name);
777 switch (bus_registry_acquire_service (registry, connection, message,
778 &service_name, flags,
779 &service_reply, transaction,
782 case BUS_RESULT_TRUE:
784 case BUS_RESULT_FALSE:
786 case BUS_RESULT_LATER:
787 retval = BUS_RESULT_LATER;
791 reply = dbus_message_new_method_return (message);
798 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
804 if (!bus_transaction_send_from_driver (transaction, connection, reply))
810 retval = BUS_RESULT_TRUE;
814 dbus_message_unref (reply);
819 bus_driver_handle_release_service (DBusConnection *connection,
820 BusTransaction *transaction,
821 DBusMessage *message,
825 DBusString service_name;
827 dbus_uint32_t service_reply;
829 BusRegistry *registry;
831 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
833 registry = bus_connection_get_registry (connection);
835 if (!dbus_message_get_args (message, error,
836 DBUS_TYPE_STRING, &name,
838 return BUS_RESULT_FALSE;
840 _dbus_verbose ("Trying to release name %s\n", name);
842 retval = BUS_RESULT_FALSE;
845 _dbus_string_init_const (&service_name, name);
847 if (!bus_registry_release_service (registry, connection,
848 &service_name, &service_reply,
852 reply = dbus_message_new_method_return (message);
859 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
865 if (!bus_transaction_send_from_driver (transaction, connection, reply))
871 retval = BUS_RESULT_TRUE;
875 dbus_message_unref (reply);
880 bus_driver_handle_service_exists (DBusConnection *connection,
881 BusTransaction *transaction,
882 DBusMessage *message,
886 DBusString service_name;
888 dbus_bool_t service_exists;
891 BusRegistry *registry;
893 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
895 registry = bus_connection_get_registry (connection);
897 if (!dbus_message_get_args (message, error,
898 DBUS_TYPE_STRING, &name,
900 return BUS_RESULT_FALSE;
902 retval = BUS_RESULT_FALSE;
904 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
906 service_exists = TRUE;
910 _dbus_string_init_const (&service_name, name);
911 service = bus_registry_lookup (registry, &service_name);
912 service_exists = service != NULL;
915 reply = dbus_message_new_method_return (message);
922 if (!dbus_message_append_args (reply,
923 DBUS_TYPE_BOOLEAN, &service_exists,
930 if (!bus_transaction_send_from_driver (transaction, connection, reply))
936 retval = BUS_RESULT_TRUE;
940 dbus_message_unref (reply);
946 bus_driver_handle_activate_service (DBusConnection *connection,
947 BusTransaction *transaction,
948 DBusMessage *message,
954 BusActivation *activation;
956 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
958 activation = bus_connection_get_activation (connection);
960 if (!dbus_message_get_args (message, error,
961 DBUS_TYPE_STRING, &name,
962 DBUS_TYPE_UINT32, &flags,
965 _DBUS_ASSERT_ERROR_IS_SET (error);
966 _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
967 return BUS_RESULT_FALSE;
970 retval = BUS_RESULT_FALSE;
972 if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
973 message, name, error))
975 _DBUS_ASSERT_ERROR_IS_SET (error);
976 _dbus_verbose ("bus_activation_activate_service() failed\n");
980 retval = BUS_RESULT_TRUE;
987 send_ack_reply (DBusConnection *connection,
988 BusTransaction *transaction,
989 DBusMessage *message,
994 if (dbus_message_get_no_reply (message))
997 reply = dbus_message_new_method_return (message);
1000 BUS_SET_OOM (error);
1004 if (!bus_transaction_send_from_driver (transaction, connection, reply))
1006 BUS_SET_OOM (error);
1007 dbus_message_unref (reply);
1011 dbus_message_unref (reply);
1017 * Send a message from the driver, activating the destination if necessary.
1018 * The message must already have a destination set.
1021 bus_driver_send_or_activate (BusTransaction *transaction,
1022 DBusMessage *message,
1025 BusContext *context;
1026 BusService *service;
1027 const char *service_name;
1028 DBusString service_string;
1030 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1032 service_name = dbus_message_get_destination (message);
1034 _dbus_assert (service_name != NULL);
1036 _dbus_string_init_const (&service_string, service_name);
1038 context = bus_transaction_get_context (transaction);
1040 service = bus_registry_lookup (bus_context_get_registry (context),
1043 if (service == NULL)
1045 /* destination isn't connected yet; pass the message to activation */
1046 BusActivation *activation;
1048 activation = bus_context_get_activation (context);
1050 if (!bus_transaction_capture (transaction, NULL, message))
1052 BUS_SET_OOM (error);
1053 _dbus_verbose ("No memory for bus_transaction_capture()");
1057 if (!bus_activation_activate_service (activation, NULL, transaction, TRUE,
1058 message, service_name, error))
1060 _DBUS_ASSERT_ERROR_IS_SET (error);
1061 _dbus_verbose ("bus_activation_activate_service() failed");
1067 DBusConnection *service_conn;
1069 service_conn = bus_service_get_primary_owners_connection (service);
1071 if (!bus_transaction_send_from_driver (transaction, service_conn, message))
1073 BUS_SET_OOM (error);
1074 _dbus_verbose ("No memory for bus_transaction_send_from_driver()");
1083 bus_driver_handle_update_activation_environment (DBusConnection *connection,
1084 BusTransaction *transaction,
1085 DBusMessage *message,
1089 BusActivation *activation;
1090 BusContext *context;
1091 DBusMessageIter iter;
1092 DBusMessageIter dict_iter;
1093 DBusMessageIter dict_entry_iter;
1096 DBusList *keys, *key_link;
1097 DBusList *values, *value_link;
1098 DBusMessage *systemd_message;
1099 DBusMessageIter systemd_iter;
1101 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1103 if (!bus_driver_check_message_is_for_us (message, error))
1108 /* UpdateActivationEnvironment is basically a recipe for privilege
1109 * escalation so let's be extra-careful: do not allow the sysadmin
1110 * to shoot themselves in the foot.
1112 if (!bus_driver_check_caller_is_privileged (connection, transaction,
1118 context = bus_connection_get_context (connection);
1120 if (bus_context_get_servicehelper (context) != NULL)
1122 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1123 "Cannot change activation environment "
1124 "on a system bus.");
1128 activation = bus_connection_get_activation (connection);
1130 dbus_message_iter_init (message, &iter);
1132 /* The message signature has already been checked for us,
1133 * so let's just assert it's right.
1135 _dbus_assert (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY);
1137 dbus_message_iter_recurse (&iter, &dict_iter);
1139 retval = BUS_RESULT_FALSE;
1140 systemd_message = NULL;
1142 /* Then loop through the sent dictionary, add the location of
1143 * the environment keys and values to lists. The result will
1144 * be in reverse order, so we don't have to constantly search
1145 * for the end of the list in a loop.
1149 while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
1151 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
1153 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
1159 dbus_message_iter_get_basic (&dict_entry_iter, &key);
1160 dbus_message_iter_next (&dict_entry_iter);
1162 value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
1164 if (value_type != DBUS_TYPE_STRING)
1167 dbus_message_iter_get_basic (&dict_entry_iter, &value);
1169 if (!_dbus_list_append (&keys, key))
1171 BUS_SET_OOM (error);
1175 if (!_dbus_list_append (&values, value))
1177 BUS_SET_OOM (error);
1181 dbus_message_iter_next (&dict_entry_iter);
1184 if (key_type != DBUS_TYPE_INVALID)
1187 dbus_message_iter_next (&dict_iter);
1190 if (array_type != DBUS_TYPE_INVALID)
1193 _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
1195 if (bus_context_get_systemd_activation (bus_connection_get_context (connection)))
1197 /* Prepare a call to forward environment updates to systemd */
1198 systemd_message = dbus_message_new_method_call ("org.freedesktop.systemd1",
1199 "/org/freedesktop/systemd1",
1200 "org.freedesktop.systemd1.Manager",
1202 if (systemd_message == NULL ||
1203 !dbus_message_set_sender (systemd_message, DBUS_SERVICE_DBUS))
1205 BUS_SET_OOM (error);
1206 _dbus_verbose ("No memory to create systemd message\n");
1210 dbus_message_set_no_reply (systemd_message, TRUE);
1211 dbus_message_iter_init_append (systemd_message, &iter);
1213 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "s",
1216 BUS_SET_OOM (error);
1217 _dbus_verbose ("No memory to open systemd message container\n");
1223 value_link = values;
1224 while (key_link != NULL)
1229 key = key_link->data;
1230 value = value_link->data;
1232 if (!bus_activation_set_environment_variable (activation,
1235 _DBUS_ASSERT_ERROR_IS_SET (error);
1236 _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
1240 if (systemd_message != NULL)
1245 /* SetEnvironment wants an array of KEY=VALUE strings */
1246 if (!_dbus_string_init (&envline) ||
1247 !_dbus_string_append_printf (&envline, "%s=%s", key, value))
1249 BUS_SET_OOM (error);
1250 _dbus_verbose ("No memory to format systemd environment line\n");
1251 _dbus_string_free (&envline);
1255 s = _dbus_string_get_data (&envline);
1257 if (!dbus_message_iter_append_basic (&systemd_iter,
1258 DBUS_TYPE_STRING, &s))
1260 BUS_SET_OOM (error);
1261 _dbus_verbose ("No memory to append systemd environment line\n");
1262 _dbus_string_free (&envline);
1266 _dbus_string_free (&envline);
1269 key_link = _dbus_list_get_next_link (&keys, key_link);
1270 value_link = _dbus_list_get_next_link (&values, value_link);
1273 /* FIXME: We can fail early having set only some of the environment variables,
1274 * (because of OOM failure). It's sort of hard to fix and it doesn't really
1275 * matter, so we're punting for now.
1277 if (key_link != NULL)
1279 if (systemd_message != NULL)
1280 dbus_message_iter_abandon_container (&iter, &systemd_iter);
1284 if (systemd_message != NULL)
1286 if (!dbus_message_iter_close_container (&iter, &systemd_iter))
1288 BUS_SET_OOM (error);
1289 _dbus_verbose ("No memory to close systemd message container\n");
1293 if (!bus_driver_send_or_activate (transaction, systemd_message, error))
1295 _DBUS_ASSERT_ERROR_IS_SET (error);
1296 _dbus_verbose ("bus_driver_send_or_activate() failed\n");
1301 if (!send_ack_reply (connection, transaction,
1305 retval = BUS_RESULT_TRUE;
1308 if (systemd_message != NULL)
1309 dbus_message_unref (systemd_message);
1310 _dbus_list_clear (&keys);
1311 _dbus_list_clear (&values);
1316 bus_driver_handle_add_match (DBusConnection *connection,
1317 BusTransaction *transaction,
1318 DBusMessage *message,
1322 const char *text, *bustype;
1324 BusMatchmaker *matchmaker;
1325 BusContext *context;
1327 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1332 if (bus_connection_get_n_match_rules (connection) >=
1333 bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
1335 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1336 "Connection \"%s\" is not allowed to add more match rules "
1337 "(increase limits in configuration file if required)",
1338 bus_connection_is_active (connection) ?
1339 bus_connection_get_name (connection) :
1344 if (!dbus_message_get_args (message, error,
1345 DBUS_TYPE_STRING, &text,
1348 _dbus_verbose ("No memory to get arguments to AddMatch\n");
1352 _dbus_string_init_const (&str, text);
1354 rule = bus_match_rule_parse (connection, &str, error);
1358 context = bus_transaction_get_context (transaction);
1359 bustype = context ? bus_context_get_type (context) : NULL;
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);
1381 return BUS_RESULT_TRUE;
1384 _DBUS_ASSERT_ERROR_IS_SET (error);
1386 bus_match_rule_unref (rule);
1387 return BUS_RESULT_FALSE;
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);
1434 return BUS_RESULT_TRUE;
1437 _DBUS_ASSERT_ERROR_IS_SET (error);
1439 bus_match_rule_unref (rule);
1440 return BUS_RESULT_FALSE;
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);
1513 return BUS_RESULT_TRUE;
1516 BUS_SET_OOM (error);
1519 _DBUS_ASSERT_ERROR_IS_SET (error);
1521 dbus_message_unref (reply);
1522 return BUS_RESULT_FALSE;
1526 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1527 BusTransaction *transaction,
1528 DBusMessage *message,
1532 DBusList *base_names;
1535 BusRegistry *registry;
1536 BusService *service;
1538 DBusMessageIter iter, array_iter;
1539 char *dbus_service_name = DBUS_SERVICE_DBUS;
1541 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1543 registry = bus_connection_get_registry (connection);
1549 if (! dbus_message_get_args (message, error,
1550 DBUS_TYPE_STRING, &text,
1554 _dbus_string_init_const (&str, text);
1555 service = bus_registry_lookup (registry, &str);
1556 if (service == NULL &&
1557 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1559 /* ORG_FREEDESKTOP_DBUS owns itself */
1560 if (! _dbus_list_append (&base_names, dbus_service_name))
1563 else if (service == NULL)
1565 dbus_set_error (error,
1566 DBUS_ERROR_NAME_HAS_NO_OWNER,
1567 "Could not get owners of name '%s': no such name", text);
1572 if (!bus_service_list_queued_owners (service,
1578 _dbus_assert (base_names != NULL);
1580 reply = dbus_message_new_method_return (message);
1584 dbus_message_iter_init_append (reply, &iter);
1585 if (!dbus_message_iter_open_container (&iter,
1587 DBUS_TYPE_STRING_AS_STRING,
1591 link = _dbus_list_get_first_link (&base_names);
1592 while (link != NULL)
1596 _dbus_assert (link->data != NULL);
1597 uname = (char *)link->data;
1599 if (!dbus_message_iter_append_basic (&array_iter,
1604 link = _dbus_list_get_next_link (&base_names, link);
1607 if (! dbus_message_iter_close_container (&iter, &array_iter))
1611 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1614 dbus_message_unref (reply);
1616 return BUS_RESULT_TRUE;
1619 BUS_SET_OOM (error);
1622 _DBUS_ASSERT_ERROR_IS_SET (error);
1624 dbus_message_unref (reply);
1627 _dbus_list_clear (&base_names);
1629 return BUS_RESULT_FALSE;
1633 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1634 BusTransaction *transaction,
1635 DBusMessage *message,
1638 DBusConnection *conn;
1641 dbus_uint32_t uid32;
1642 const char *service;
1643 BusDriverFound found;
1645 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1649 found = bus_driver_get_conn_helper (connection, message, "UID", &service,
1653 case BUS_DRIVER_FOUND_SELF:
1654 uid = _dbus_getuid ();
1656 case BUS_DRIVER_FOUND_PEER:
1657 if (!dbus_connection_get_unix_user (conn, &uid))
1658 uid = DBUS_UID_UNSET;
1660 case BUS_DRIVER_FOUND_ERROR:
1664 if (uid == DBUS_UID_UNSET)
1666 dbus_set_error (error,
1668 "Could not determine UID for '%s'", service);
1672 reply = dbus_message_new_method_return (message);
1677 if (! dbus_message_append_args (reply,
1678 DBUS_TYPE_UINT32, &uid32,
1682 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1685 dbus_message_unref (reply);
1687 return BUS_RESULT_TRUE;
1690 BUS_SET_OOM (error);
1693 _DBUS_ASSERT_ERROR_IS_SET (error);
1695 dbus_message_unref (reply);
1696 return BUS_RESULT_FALSE;
1700 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1701 BusTransaction *transaction,
1702 DBusMessage *message,
1705 DBusConnection *conn;
1708 dbus_uint32_t pid32;
1709 const char *service;
1710 BusDriverFound found;
1712 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1716 found = bus_driver_get_conn_helper (connection, message, "PID", &service,
1720 case BUS_DRIVER_FOUND_SELF:
1721 pid = _dbus_getpid ();
1723 case BUS_DRIVER_FOUND_PEER:
1724 if (!dbus_connection_get_unix_process_id (conn, &pid))
1725 pid = DBUS_PID_UNSET;
1727 case BUS_DRIVER_FOUND_ERROR:
1731 if (pid == DBUS_PID_UNSET)
1733 dbus_set_error (error,
1734 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1735 "Could not determine PID for '%s'", service);
1739 reply = dbus_message_new_method_return (message);
1744 if (! dbus_message_append_args (reply,
1745 DBUS_TYPE_UINT32, &pid32,
1749 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1752 dbus_message_unref (reply);
1754 return BUS_RESULT_TRUE;
1757 BUS_SET_OOM (error);
1760 _DBUS_ASSERT_ERROR_IS_SET (error);
1762 dbus_message_unref (reply);
1763 return BUS_RESULT_FALSE;
1767 bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
1768 BusTransaction *transaction,
1769 DBusMessage *message,
1772 DBusConnection *conn;
1775 dbus_uint32_t data_size;
1776 const char *service;
1777 BusDriverFound found;
1779 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1783 found = bus_driver_get_conn_helper (connection, message, "audit session data",
1784 &service, &conn, error);
1786 if (found == BUS_DRIVER_FOUND_ERROR)
1789 reply = dbus_message_new_method_return (message);
1793 /* We don't know how to find "ADT audit session data" for the bus daemon
1794 * itself. Is that even meaningful?
1795 * FIXME: Implement this or briefly note it makes no sense.
1797 if (found != BUS_DRIVER_FOUND_PEER ||
1798 !dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) ||
1801 dbus_set_error (error,
1802 DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
1803 "Could not determine audit session data for '%s'", service);
1807 if (! dbus_message_append_args (reply,
1808 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
1812 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1815 dbus_message_unref (reply);
1817 return BUS_RESULT_TRUE;
1820 BUS_SET_OOM (error);
1823 _DBUS_ASSERT_ERROR_IS_SET (error);
1825 dbus_message_unref (reply);
1826 return BUS_RESULT_FALSE;
1830 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1831 BusTransaction *transaction,
1832 DBusMessage *message,
1835 DBusConnection *conn;
1837 BusSELinuxID *context;
1838 const char *service;
1839 BusDriverFound found;
1841 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1845 found = bus_driver_get_conn_helper (connection, message, "security context",
1846 &service, &conn, error);
1848 if (found == BUS_DRIVER_FOUND_ERROR)
1851 reply = dbus_message_new_method_return (message);
1855 /* FIXME: Obtain the SELinux security context for the bus daemon itself */
1856 if (found == BUS_DRIVER_FOUND_PEER)
1857 context = bus_connection_get_selinux_id (conn);
1863 dbus_set_error (error,
1864 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1865 "Could not determine security context for '%s'", service);
1869 if (! bus_selinux_append_context (reply, context, error))
1872 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1875 dbus_message_unref (reply);
1877 return BUS_RESULT_TRUE;
1880 BUS_SET_OOM (error);
1883 _DBUS_ASSERT_ERROR_IS_SET (error);
1885 dbus_message_unref (reply);
1886 return BUS_RESULT_FALSE;
1890 bus_driver_handle_get_connection_credentials (DBusConnection *connection,
1891 BusTransaction *transaction,
1892 DBusMessage *message,
1895 DBusConnection *conn;
1897 DBusMessageIter reply_iter;
1898 DBusMessageIter array_iter;
1899 unsigned long ulong_uid, ulong_pid;
1901 const char *service;
1902 BusDriverFound found;
1904 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1908 found = bus_driver_get_conn_helper (connection, message, "credentials",
1909 &service, &conn, error);
1913 case BUS_DRIVER_FOUND_SELF:
1914 ulong_pid = _dbus_getpid ();
1915 ulong_uid = _dbus_getuid ();
1918 case BUS_DRIVER_FOUND_PEER:
1919 if (!dbus_connection_get_unix_process_id (conn, &ulong_pid))
1920 ulong_pid = DBUS_PID_UNSET;
1921 if (!dbus_connection_get_unix_user (conn, &ulong_uid))
1922 ulong_uid = DBUS_UID_UNSET;
1924 case BUS_DRIVER_FOUND_ERROR:
1928 reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
1932 /* we can't represent > 32-bit pids; if your system needs them, please
1933 * add ProcessID64 to the spec or something */
1934 if (ulong_pid <= _DBUS_UINT32_MAX && ulong_pid != DBUS_PID_UNSET &&
1935 !_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_pid))
1938 /* we can't represent > 32-bit uids; if your system needs them, please
1939 * add UnixUserID64 to the spec or something */
1940 if (ulong_uid <= _DBUS_UINT32_MAX && ulong_uid != DBUS_UID_UNSET &&
1941 !_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_uid))
1944 /* FIXME: Obtain the Windows user of the bus daemon itself */
1945 if (found == BUS_DRIVER_FOUND_PEER &&
1946 dbus_connection_get_windows_user (conn, &s))
1954 _dbus_string_init_const (&str, s);
1955 result = _dbus_validate_utf8 (&str, 0, _dbus_string_get_length (&str));
1956 _dbus_string_free (&str);
1959 if (!_dbus_asv_add_string (&array_iter, "WindowsSID", s))
1968 /* FIXME: Obtain the security label for the bus daemon itself */
1969 if (found == BUS_DRIVER_FOUND_PEER &&
1970 _dbus_connection_get_linux_security_label (conn, &s))
1975 /* use the GVariant bytestring convention for strings of unknown
1976 * encoding: include the \0 in the payload, for zero-copy reading */
1977 if (!_dbus_asv_add_byte_array (&array_iter, "LinuxSecurityLabel",
1987 if (!_dbus_asv_close (&reply_iter, &array_iter))
1990 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1992 /* this time we don't want to close the iterator again, so just
1993 * get rid of the message */
1994 dbus_message_unref (reply);
1999 dbus_message_unref (reply);
2001 return BUS_RESULT_TRUE;
2004 BUS_SET_OOM (error);
2007 _DBUS_ASSERT_ERROR_IS_SET (error);
2011 _dbus_asv_abandon (&reply_iter, &array_iter);
2012 dbus_message_unref (reply);
2015 return BUS_RESULT_FALSE;
2019 bus_driver_handle_reload_config (DBusConnection *connection,
2020 BusTransaction *transaction,
2021 DBusMessage *message,
2024 BusContext *context;
2027 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2031 context = bus_connection_get_context (connection);
2032 if (!bus_context_reload_config (context, error))
2035 reply = dbus_message_new_method_return (message);
2039 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2042 dbus_message_unref (reply);
2043 return BUS_RESULT_TRUE;
2046 BUS_SET_OOM (error);
2049 _DBUS_ASSERT_ERROR_IS_SET (error);
2051 dbus_message_unref (reply);
2052 return BUS_RESULT_FALSE;
2055 #ifdef DBUS_ENABLE_VERBOSE_MODE
2057 bus_driver_handle_enable_verbose (DBusConnection *connection,
2058 BusTransaction *transaction,
2059 DBusMessage *message,
2062 DBusMessage *reply = NULL;
2064 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2066 reply = dbus_message_new_method_return (message);
2070 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2073 _dbus_set_verbose(TRUE);
2075 dbus_message_unref (reply);
2079 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2081 BUS_SET_OOM (error);
2084 dbus_message_unref (reply);
2089 bus_driver_handle_disable_verbose (DBusConnection *connection,
2090 BusTransaction *transaction,
2091 DBusMessage *message,
2094 DBusMessage *reply = NULL;
2096 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2098 reply = dbus_message_new_method_return (message);
2102 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2105 _dbus_set_verbose(FALSE);
2107 dbus_message_unref (reply);
2111 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2113 BUS_SET_OOM (error);
2116 dbus_message_unref (reply);
2122 bus_driver_handle_get_id (DBusConnection *connection,
2123 BusTransaction *transaction,
2124 DBusMessage *message,
2127 BusContext *context;
2130 const char *v_STRING;
2132 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2134 if (!_dbus_string_init (&uuid))
2136 BUS_SET_OOM (error);
2137 return BUS_RESULT_FALSE;
2142 context = bus_connection_get_context (connection);
2143 if (!bus_context_get_id (context, &uuid))
2146 reply = dbus_message_new_method_return (message);
2150 v_STRING = _dbus_string_get_const_data (&uuid);
2151 if (!dbus_message_append_args (reply,
2152 DBUS_TYPE_STRING, &v_STRING,
2156 _dbus_assert (dbus_message_has_signature (reply, "s"));
2158 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2161 _dbus_string_free (&uuid);
2162 dbus_message_unref (reply);
2163 return BUS_RESULT_TRUE;
2166 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2168 BUS_SET_OOM (error);
2171 dbus_message_unref (reply);
2172 _dbus_string_free (&uuid);
2173 return BUS_RESULT_FALSE;
2177 bus_driver_handle_become_monitor (DBusConnection *connection,
2178 BusTransaction *transaction,
2179 DBusMessage *message,
2182 char **match_rules = NULL;
2183 const char *bustype;
2184 BusContext *context;
2186 DBusList *rules = NULL;
2191 dbus_uint32_t flags;
2192 dbus_bool_t ret = FALSE;
2194 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2196 if (!bus_driver_check_message_is_for_us (message, error))
2199 context = bus_transaction_get_context (transaction);
2200 bustype = context ? bus_context_get_type (context) : NULL;
2201 if (!bus_apparmor_allows_eavesdropping (connection, bustype, error))
2204 if (!bus_driver_check_caller_is_privileged (connection, transaction,
2208 if (!dbus_message_get_args (message, error,
2209 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &match_rules, &n_match_rules,
2210 DBUS_TYPE_UINT32, &flags,
2216 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2217 "BecomeMonitor does not support any flags yet");
2221 /* Special case: a zero-length array becomes [""] */
2222 if (n_match_rules == 0)
2224 match_rules = dbus_malloc (2 * sizeof (char *));
2226 if (match_rules == NULL)
2228 BUS_SET_OOM (error);
2232 match_rules[0] = _dbus_strdup ("");
2234 if (match_rules[0] == NULL)
2236 BUS_SET_OOM (error);
2240 match_rules[1] = NULL;
2244 for (i = 0; i < n_match_rules; i++)
2246 _dbus_string_init_const (&str, match_rules[i]);
2247 rule = bus_match_rule_parse (connection, &str, error);
2252 /* monitors always eavesdrop */
2253 bus_match_rule_set_client_is_eavesdropping (rule, TRUE);
2255 if (!_dbus_list_append (&rules, rule))
2257 BUS_SET_OOM (error);
2258 bus_match_rule_unref (rule);
2263 /* Send the ack before we remove the rule, since the ack is undone
2264 * on transaction cancel, but becoming a monitor isn't.
2266 if (!send_ack_reply (connection, transaction, message, error))
2269 if (!bus_connection_be_monitor (connection, transaction, &rules, error))
2276 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2278 _DBUS_ASSERT_ERROR_IS_SET (error);
2280 for (iter = _dbus_list_get_first_link (&rules);
2282 iter = _dbus_list_get_next_link (&rules, iter))
2283 bus_match_rule_unref (iter->data);
2285 _dbus_list_clear (&rules);
2287 dbus_free_string_array (match_rules);
2294 const char *in_args;
2295 const char *out_args;
2296 BusResult (* handler) (DBusConnection *connection,
2297 BusTransaction *transaction,
2298 DBusMessage *message,
2302 /* For speed it might be useful to sort this in order of
2303 * frequency of use (but doesn't matter with only a few items
2306 static const MessageHandler dbus_message_handlers[] = {
2309 DBUS_TYPE_STRING_AS_STRING,
2310 bus_driver_handle_hello },
2312 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
2313 DBUS_TYPE_UINT32_AS_STRING,
2314 bus_driver_handle_acquire_service },
2316 DBUS_TYPE_STRING_AS_STRING,
2317 DBUS_TYPE_UINT32_AS_STRING,
2318 bus_driver_handle_release_service },
2319 { "StartServiceByName",
2320 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
2321 DBUS_TYPE_UINT32_AS_STRING,
2322 bus_driver_handle_activate_service },
2323 { "UpdateActivationEnvironment",
2324 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,
2326 bus_driver_handle_update_activation_environment },
2328 DBUS_TYPE_STRING_AS_STRING,
2329 DBUS_TYPE_BOOLEAN_AS_STRING,
2330 bus_driver_handle_service_exists },
2333 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
2334 bus_driver_handle_list_services },
2335 { "ListActivatableNames",
2337 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
2338 bus_driver_handle_list_activatable_services },
2340 DBUS_TYPE_STRING_AS_STRING,
2342 bus_driver_handle_add_match },
2344 DBUS_TYPE_STRING_AS_STRING,
2346 bus_driver_handle_remove_match },
2348 DBUS_TYPE_STRING_AS_STRING,
2349 DBUS_TYPE_STRING_AS_STRING,
2350 bus_driver_handle_get_service_owner },
2351 { "ListQueuedOwners",
2352 DBUS_TYPE_STRING_AS_STRING,
2353 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
2354 bus_driver_handle_list_queued_owners },
2355 { "GetConnectionUnixUser",
2356 DBUS_TYPE_STRING_AS_STRING,
2357 DBUS_TYPE_UINT32_AS_STRING,
2358 bus_driver_handle_get_connection_unix_user },
2359 { "GetConnectionUnixProcessID",
2360 DBUS_TYPE_STRING_AS_STRING,
2361 DBUS_TYPE_UINT32_AS_STRING,
2362 bus_driver_handle_get_connection_unix_process_id },
2363 { "GetAdtAuditSessionData",
2364 DBUS_TYPE_STRING_AS_STRING,
2365 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
2366 bus_driver_handle_get_adt_audit_session_data },
2367 { "GetConnectionSELinuxSecurityContext",
2368 DBUS_TYPE_STRING_AS_STRING,
2369 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
2370 bus_driver_handle_get_connection_selinux_security_context },
2374 bus_driver_handle_reload_config },
2377 DBUS_TYPE_STRING_AS_STRING,
2378 bus_driver_handle_get_id },
2379 { "GetConnectionCredentials", "s", "a{sv}",
2380 bus_driver_handle_get_connection_credentials },
2381 { NULL, NULL, NULL, NULL }
2384 static BusResult bus_driver_handle_introspect (DBusConnection *,
2385 BusTransaction *, DBusMessage *, DBusError *);
2387 static const MessageHandler introspectable_message_handlers[] = {
2388 { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
2389 { NULL, NULL, NULL, NULL }
2392 static const MessageHandler monitoring_message_handlers[] = {
2393 { "BecomeMonitor", "asu", "", bus_driver_handle_become_monitor },
2394 { NULL, NULL, NULL, NULL }
2397 #ifdef DBUS_ENABLE_VERBOSE_MODE
2398 static const MessageHandler verbose_message_handlers[] = {
2399 { "EnableVerbose", "", "", bus_driver_handle_enable_verbose},
2400 { "DisableVerbose", "", "", bus_driver_handle_disable_verbose},
2401 { NULL, NULL, NULL, NULL }
2405 #ifdef DBUS_ENABLE_STATS
2406 static const MessageHandler stats_message_handlers[] = {
2407 { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
2408 { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
2409 { "GetAllMatchRules", "", "a{sas}", bus_stats_handle_get_all_match_rules },
2410 { NULL, NULL, NULL, NULL }
2416 const MessageHandler *message_handlers;
2417 const char *extra_introspection;
2420 /* These should ideally be sorted by frequency of use, although it
2421 * probably doesn't matter with this few items */
2422 static InterfaceHandler interface_handlers[] = {
2423 { DBUS_INTERFACE_DBUS, dbus_message_handlers,
2424 " <signal name=\"NameOwnerChanged\">\n"
2425 " <arg type=\"s\"/>\n"
2426 " <arg type=\"s\"/>\n"
2427 " <arg type=\"s\"/>\n"
2429 " <signal name=\"NameLost\">\n"
2430 " <arg type=\"s\"/>\n"
2432 " <signal name=\"NameAcquired\">\n"
2433 " <arg type=\"s\"/>\n"
2435 { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
2436 { DBUS_INTERFACE_MONITORING, monitoring_message_handlers, NULL },
2437 #ifdef DBUS_ENABLE_VERBOSE_MODE
2438 { DBUS_INTERFACE_VERBOSE, verbose_message_handlers, NULL },
2440 #ifdef DBUS_ENABLE_STATS
2441 { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
2443 { NULL, NULL, NULL }
2447 write_args_for_direction (DBusString *xml,
2448 const char *signature,
2451 DBusTypeReader typereader;
2455 _dbus_string_init_const (&sigstr, signature);
2456 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
2458 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
2460 const DBusString *subsig;
2463 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
2464 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
2467 if (!_dbus_string_append_len (xml,
2468 _dbus_string_get_const_data (subsig) + start,
2471 if (!_dbus_string_append (xml, "\"/>\n"))
2474 _dbus_type_reader_next (&typereader);
2482 bus_driver_generate_introspect_string (DBusString *xml)
2484 const InterfaceHandler *ih;
2485 const MessageHandler *mh;
2487 if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
2489 if (!_dbus_string_append (xml, "<node>\n"))
2492 for (ih = interface_handlers; ih->name != NULL; ih++)
2494 if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n",
2498 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2500 if (!_dbus_string_append_printf (xml, " <method name=\"%s\">\n",
2504 if (!write_args_for_direction (xml, mh->in_args, TRUE))
2507 if (!write_args_for_direction (xml, mh->out_args, FALSE))
2510 if (!_dbus_string_append (xml, " </method>\n"))
2514 if (ih->extra_introspection != NULL &&
2515 !_dbus_string_append (xml, ih->extra_introspection))
2518 if (!_dbus_string_append (xml, " </interface>\n"))
2522 if (!_dbus_string_append (xml, "</node>\n"))
2529 bus_driver_handle_introspect (DBusConnection *connection,
2530 BusTransaction *transaction,
2531 DBusMessage *message,
2536 const char *v_STRING;
2538 _dbus_verbose ("Introspect() on bus driver\n");
2540 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2544 if (! dbus_message_get_args (message, error,
2547 _DBUS_ASSERT_ERROR_IS_SET (error);
2548 return BUS_RESULT_FALSE;
2551 if (!_dbus_string_init (&xml))
2553 BUS_SET_OOM (error);
2554 return BUS_RESULT_FALSE;
2557 if (!bus_driver_generate_introspect_string (&xml))
2560 v_STRING = _dbus_string_get_const_data (&xml);
2562 reply = dbus_message_new_method_return (message);
2566 if (! dbus_message_append_args (reply,
2567 DBUS_TYPE_STRING, &v_STRING,
2571 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2574 dbus_message_unref (reply);
2575 _dbus_string_free (&xml);
2577 return BUS_RESULT_TRUE;
2580 BUS_SET_OOM (error);
2583 dbus_message_unref (reply);
2585 _dbus_string_free (&xml);
2587 return BUS_RESULT_FALSE;
2591 * Set @error and return FALSE if the message is not directed to the
2592 * dbus-daemon by its canonical object path. This is hardening against
2593 * system services with poorly-written security policy files, which
2594 * might allow sending dangerously broad equivalence classes of messages
2595 * such as "anything with this assumed-to-be-safe object path".
2597 * dbus-daemon is unusual in that it normally ignores the object path
2598 * of incoming messages; we need to keep that behaviour for the "read"
2599 * read-only method calls like GetConnectionUnixUser for backwards
2600 * compatibility, but it seems safer to be more restrictive for things
2601 * intended to be root-only or privileged-developers-only.
2603 * It is possible that there are other system services with the same
2604 * quirk as dbus-daemon.
2607 bus_driver_check_message_is_for_us (DBusMessage *message,
2610 if (!dbus_message_has_path (message, DBUS_PATH_DBUS))
2612 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
2613 "Method '%s' is only available at the canonical object path '%s'",
2614 dbus_message_get_member (message), DBUS_PATH_DBUS);
2623 bus_driver_handle_message (DBusConnection *connection,
2624 BusTransaction *transaction,
2625 DBusMessage *message,
2628 const char *name, *interface;
2629 const InterfaceHandler *ih;
2630 const MessageHandler *mh;
2631 dbus_bool_t found_interface = FALSE;
2633 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2635 if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
2637 BusContext *context;
2638 DBusConnection *systemd;
2640 context = bus_connection_get_context (connection);
2641 systemd = bus_driver_get_owner_of_name (connection,
2642 "org.freedesktop.systemd1");
2644 if (systemd != connection)
2646 const char *attacker;
2648 attacker = bus_connection_get_name (connection);
2649 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY,
2650 "Ignoring forged ActivationFailure message from "
2651 "connection %s (%s)",
2652 attacker ? attacker : "(unauthenticated)",
2653 bus_connection_get_loginfo (connection));
2655 return BUS_RESULT_TRUE;
2658 return dbus_activation_systemd_failure(bus_context_get_activation(context), message) == TRUE ? BUS_RESULT_TRUE : BUS_RESULT_FALSE;
2661 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
2663 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
2664 return BUS_RESULT_TRUE; /* we just ignore this */
2667 /* may be NULL, which means "any interface will do" */
2668 interface = dbus_message_get_interface (message);
2670 _dbus_assert (dbus_message_get_member (message) != NULL);
2672 name = dbus_message_get_member (message);
2674 _dbus_verbose ("Driver got a method call: %s\n", name);
2676 /* security checks should have kept this from getting here */
2677 _dbus_assert (dbus_message_get_sender (message) != NULL ||
2678 strcmp (name, "Hello") == 0);
2680 for (ih = interface_handlers; ih->name != NULL; ih++)
2682 if (interface != NULL && strcmp (interface, ih->name) != 0)
2685 found_interface = TRUE;
2687 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2689 if (strcmp (mh->name, name) != 0)
2692 _dbus_verbose ("Found driver handler for %s\n", name);
2694 if (!dbus_message_has_signature (message, mh->in_args))
2696 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2697 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
2698 name, dbus_message_get_signature (message),
2701 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2702 "Call to %s has wrong args (%s, expected %s)\n",
2703 name, dbus_message_get_signature (message),
2705 _DBUS_ASSERT_ERROR_IS_SET (error);
2706 return BUS_RESULT_FALSE;
2709 switch ((* mh->handler) (connection, transaction, message, error))
2711 case BUS_RESULT_TRUE:
2712 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2713 _dbus_verbose ("Driver handler succeeded\n");
2714 return BUS_RESULT_TRUE;
2715 case BUS_RESULT_FALSE:
2716 _DBUS_ASSERT_ERROR_IS_SET (error);
2717 _dbus_verbose ("Driver handler returned failure\n");
2718 return BUS_RESULT_FALSE;
2719 case BUS_RESULT_LATER:
2720 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2721 _dbus_verbose ("Driver handler delayed message processing due to policy check\n");
2722 return BUS_RESULT_LATER;
2727 _dbus_verbose ("No driver handler for message \"%s\"\n",
2730 dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
2731 "%s does not understand message %s",
2732 DBUS_SERVICE_DBUS, name);
2734 return BUS_RESULT_FALSE;
2738 bus_driver_remove_connection (DBusConnection *connection)
2740 /* FIXME 1.0 Does nothing for now, should unregister the connection
2741 * with the bus driver.