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"
38 #include <dbus/dbus-asv-util.h>
39 #include <dbus/dbus-connection-internal.h>
40 #include <dbus/dbus-string.h>
41 #include <dbus/dbus-internals.h>
42 #include <dbus/dbus-message.h>
43 #include <dbus/dbus-marshal-recursive.h>
44 #include <dbus/dbus-marshal-validate.h>
49 BUS_DRIVER_FOUND_SELF,
50 BUS_DRIVER_FOUND_PEER,
51 BUS_DRIVER_FOUND_ERROR,
54 static inline const char *
55 nonnull (const char *maybe_null,
58 return (maybe_null ? maybe_null : if_null);
61 static DBusConnection *
62 bus_driver_get_owner_of_name (DBusConnection *connection,
65 BusRegistry *registry;
69 registry = bus_connection_get_registry (connection);
70 _dbus_string_init_const (&str, name);
71 serv = bus_registry_lookup (registry, &str);
76 return bus_service_get_primary_owners_connection (serv);
80 bus_driver_get_conn_helper (DBusConnection *connection,
82 const char *what_we_want,
84 DBusConnection **peer_conn_p,
90 if (!dbus_message_get_args (message, error,
91 DBUS_TYPE_STRING, &name,
93 return BUS_DRIVER_FOUND_ERROR;
95 _dbus_assert (name != NULL);
96 _dbus_verbose ("asked for %s of connection %s\n", what_we_want, name);
101 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
102 return BUS_DRIVER_FOUND_SELF;
104 conn = bus_driver_get_owner_of_name (connection, name);
108 dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
109 "Could not get %s of name '%s': no such name",
111 return BUS_DRIVER_FOUND_ERROR;
114 if (peer_conn_p != NULL)
117 return BUS_DRIVER_FOUND_PEER;
121 * Log a security warning and set error unless the uid of the connection
122 * is either the uid of this process, or on Unix, uid 0 (root).
124 * This is intended to be a second line of defence after <deny> rules,
125 * to mitigate incorrect system bus security policy configuration files
126 * like the ones in CVE-2014-8148 and CVE-2014-8156, and (if present)
127 * LSM rules; so it doesn't need to be perfect, but as long as we have
128 * potentially dangerous functionality in the system bus, it does need
132 bus_driver_check_caller_is_privileged (DBusConnection *connection,
133 BusTransaction *transaction,
134 DBusMessage *message,
140 if (!dbus_connection_get_unix_user (connection, &uid))
142 const char *method = dbus_message_get_member (message);
144 bus_context_log_and_set_error (bus_transaction_get_context (transaction),
145 DBUS_SYSTEM_LOG_SECURITY, error, DBUS_ERROR_ACCESS_DENIED,
146 "rejected attempt to call %s by connection %s (%s) with "
147 "unknown uid", method,
148 nonnull (bus_connection_get_name (connection), "(inactive)"),
149 bus_connection_get_loginfo (connection));
153 /* I'm writing it in this slightly strange form so that it's more
154 * obvious that this security-sensitive code is correct.
156 if (_dbus_unix_user_is_process_owner (uid))
166 const char *method = dbus_message_get_member (message);
168 bus_context_log_and_set_error (bus_transaction_get_context (transaction),
169 DBUS_SYSTEM_LOG_SECURITY, error, DBUS_ERROR_ACCESS_DENIED,
170 "rejected attempt to call %s by connection %s (%s) with "
172 nonnull (bus_connection_get_name (connection), "(inactive)"),
173 bus_connection_get_loginfo (connection), uid);
178 #elif defined(DBUS_WIN)
179 char *windows_sid = NULL;
180 dbus_bool_t ret = FALSE;
182 if (!dbus_connection_get_windows_user (connection, &windows_sid))
184 const char *method = dbus_message_get_member (message);
186 bus_context_log_and_set_error (bus_transaction_get_context (transaction),
187 DBUS_SYSTEM_LOG_SECURITY, error, DBUS_ERROR_ACCESS_DENIED,
188 "rejected attempt to call %s by unknown uid", method);
192 if (!_dbus_windows_user_is_process_owner (windows_sid))
194 const char *method = dbus_message_get_member (message);
196 bus_context_log_and_set_error (bus_transaction_get_context (transaction),
197 DBUS_SYSTEM_LOG_SECURITY, error, DBUS_ERROR_ACCESS_DENIED,
198 "rejected attempt to call %s by uid %s", method, windows_sid);
204 dbus_free (windows_sid);
207 /* make sure we fail closed in the hypothetical case that we are neither
208 * Unix nor Windows */
209 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
210 "please teach bus/driver.c how uids work on this platform");
215 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
216 DBusMessage *hello_message,
217 BusTransaction *transaction,
221 bus_driver_send_service_owner_changed (const char *service_name,
222 const char *old_owner,
223 const char *new_owner,
224 BusTransaction *transaction,
227 DBusMessage *message;
229 const char *null_service;
231 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
234 _dbus_verbose ("sending name owner changed: %s [%s -> %s]\n",
236 old_owner ? old_owner : null_service,
237 new_owner ? new_owner : null_service);
239 message = dbus_message_new_signal (DBUS_PATH_DBUS,
249 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
252 if (!dbus_message_append_args (message,
253 DBUS_TYPE_STRING, &service_name,
254 DBUS_TYPE_STRING, old_owner ? &old_owner : &null_service,
255 DBUS_TYPE_STRING, new_owner ? &new_owner : &null_service,
259 _dbus_assert (dbus_message_has_signature (message, "sss"));
261 if (!bus_transaction_capture (transaction, NULL, message))
264 switch (bus_dispatch_matches (transaction, NULL, NULL, message, NULL, error))
266 case BUS_RESULT_TRUE:
269 case BUS_RESULT_FALSE:
273 /* should never happen */
274 _dbus_assert_not_reached ("bus_dispatch_matches returned BUS_RESULT_LATER unexpectedly");
278 dbus_message_unref (message);
283 dbus_message_unref (message);
289 bus_driver_send_service_lost (DBusConnection *connection,
290 const char *service_name,
291 BusTransaction *transaction,
294 DBusMessage *message;
296 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
298 message = dbus_message_new_signal (DBUS_PATH_DBUS,
308 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
309 !dbus_message_append_args (message,
310 DBUS_TYPE_STRING, &service_name,
313 dbus_message_unref (message);
318 if (!bus_transaction_send_from_driver (transaction, connection, message))
320 dbus_message_unref (message);
326 dbus_message_unref (message);
332 bus_driver_send_service_acquired (DBusConnection *connection,
333 const char *service_name,
334 BusTransaction *transaction,
337 DBusMessage *message;
339 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
341 message = dbus_message_new_signal (DBUS_PATH_DBUS,
351 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
352 !dbus_message_append_args (message,
353 DBUS_TYPE_STRING, &service_name,
356 dbus_message_unref (message);
361 if (!bus_transaction_send_from_driver (transaction, connection, message))
363 dbus_message_unref (message);
369 dbus_message_unref (message);
375 create_unique_client_name (BusRegistry *registry,
378 /* We never want to use the same unique client name twice, because
379 * we want to guarantee that if you send a message to a given unique
380 * name, you always get the same application. So we use two numbers
381 * for INT_MAX * INT_MAX combinations, should be pretty safe against
384 /* FIXME these should be in BusRegistry rather than static vars */
385 static int next_major_number = 0;
386 static int next_minor_number = 0;
389 len = _dbus_string_get_length (str);
393 /* start out with 1-0, go to 1-1, 1-2, 1-3,
394 * up to 1-MAXINT, then 2-0, 2-1, etc.
396 if (next_minor_number <= 0)
398 next_major_number += 1;
399 next_minor_number = 0;
400 if (next_major_number <= 0)
401 _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
404 _dbus_assert (next_major_number > 0);
405 _dbus_assert (next_minor_number >= 0);
407 /* appname:MAJOR-MINOR */
409 if (!_dbus_string_append (str, ":"))
412 if (!_dbus_string_append_int (str, next_major_number))
415 if (!_dbus_string_append (str, "."))
418 if (!_dbus_string_append_int (str, next_minor_number))
421 next_minor_number += 1;
423 /* Check if a client with the name exists */
424 if (bus_registry_lookup (registry, str) == NULL)
427 /* drop the number again, try the next one. */
428 _dbus_string_set_length (str, len);
435 bus_driver_handle_hello (DBusConnection *connection,
436 BusTransaction *transaction,
437 DBusMessage *message,
440 DBusString unique_name;
443 BusRegistry *registry;
444 BusConnections *connections;
446 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
448 if (bus_connection_is_active (connection))
450 /* We already handled an Hello message for this connection. */
451 dbus_set_error (error, DBUS_ERROR_FAILED,
452 "Already handled an Hello message");
453 return BUS_RESULT_FALSE;
456 /* Note that when these limits are exceeded we don't disconnect the
457 * connection; we just sort of leave it hanging there until it times
458 * out or disconnects itself or is dropped due to the max number of
459 * incomplete connections. It's even OK if the connection wants to
460 * retry the hello message, we support that.
462 connections = bus_connection_get_connections (connection);
463 if (!bus_connections_check_limits (connections, connection,
466 _DBUS_ASSERT_ERROR_IS_SET (error);
467 return BUS_RESULT_FALSE;
470 if (!_dbus_string_init (&unique_name))
473 return BUS_RESULT_FALSE;
476 retval = BUS_RESULT_FALSE;
478 registry = bus_connection_get_registry (connection);
480 if (!create_unique_client_name (registry, &unique_name))
486 if (!bus_connection_complete (connection, &unique_name, error))
488 _DBUS_ASSERT_ERROR_IS_SET (error);
492 if (!dbus_message_set_sender (message,
493 bus_connection_get_name (connection)))
499 if (!bus_driver_send_welcome_message (connection, message, transaction, error))
502 /* Create the service */
503 service = bus_registry_ensure (registry,
504 &unique_name, connection, 0, transaction, error);
508 _dbus_assert (bus_connection_is_active (connection));
509 retval = BUS_RESULT_TRUE;
512 _dbus_string_free (&unique_name);
517 bus_driver_send_welcome_message (DBusConnection *connection,
518 DBusMessage *hello_message,
519 BusTransaction *transaction,
522 DBusMessage *welcome;
525 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
527 name = bus_connection_get_name (connection);
528 _dbus_assert (name != NULL);
530 welcome = dbus_message_new_method_return (hello_message);
537 if (!dbus_message_append_args (welcome,
538 DBUS_TYPE_STRING, &name,
541 dbus_message_unref (welcome);
546 _dbus_assert (dbus_message_has_signature (welcome, DBUS_TYPE_STRING_AS_STRING));
548 if (!bus_transaction_send_from_driver (transaction, connection, welcome))
550 dbus_message_unref (welcome);
556 dbus_message_unref (welcome);
562 bus_driver_handle_list_services (DBusConnection *connection,
563 BusTransaction *transaction,
564 DBusMessage *message,
570 BusRegistry *registry;
572 DBusMessageIter iter;
575 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
577 registry = bus_connection_get_registry (connection);
579 reply = dbus_message_new_method_return (message);
583 return BUS_RESULT_FALSE;
586 if (!bus_registry_list_services (registry, &services, &len))
588 dbus_message_unref (reply);
590 return BUS_RESULT_FALSE;
593 dbus_message_iter_init_append (reply, &iter);
595 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
596 DBUS_TYPE_STRING_AS_STRING,
599 dbus_free_string_array (services);
600 dbus_message_unref (reply);
602 return BUS_RESULT_FALSE;
606 /* Include the bus driver in the list */
607 const char *v_STRING = DBUS_SERVICE_DBUS;
608 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
611 dbus_free_string_array (services);
612 dbus_message_unref (reply);
614 return BUS_RESULT_FALSE;
621 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
624 dbus_free_string_array (services);
625 dbus_message_unref (reply);
627 return BUS_RESULT_FALSE;
632 dbus_free_string_array (services);
634 if (!dbus_message_iter_close_container (&iter, &sub))
636 dbus_message_unref (reply);
638 return BUS_RESULT_FALSE;
641 if (!bus_transaction_send_from_driver (transaction, connection, reply))
643 dbus_message_unref (reply);
645 return BUS_RESULT_FALSE;
649 dbus_message_unref (reply);
650 return BUS_RESULT_TRUE;
655 bus_driver_handle_list_activatable_services (DBusConnection *connection,
656 BusTransaction *transaction,
657 DBusMessage *message,
663 BusActivation *activation;
665 DBusMessageIter iter;
668 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
670 activation = bus_connection_get_activation (connection);
672 reply = dbus_message_new_method_return (message);
676 return BUS_RESULT_FALSE;
679 if (!bus_activation_list_services (activation, &services, &len))
681 dbus_message_unref (reply);
683 return BUS_RESULT_FALSE;
686 dbus_message_iter_init_append (reply, &iter);
688 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
689 DBUS_TYPE_STRING_AS_STRING,
692 dbus_free_string_array (services);
693 dbus_message_unref (reply);
695 return BUS_RESULT_FALSE;
699 /* Include the bus driver in the list */
700 const char *v_STRING = DBUS_SERVICE_DBUS;
701 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
704 dbus_free_string_array (services);
705 dbus_message_unref (reply);
707 return BUS_RESULT_FALSE;
714 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
717 dbus_free_string_array (services);
718 dbus_message_unref (reply);
720 return BUS_RESULT_FALSE;
725 dbus_free_string_array (services);
727 if (!dbus_message_iter_close_container (&iter, &sub))
729 dbus_message_unref (reply);
731 return BUS_RESULT_FALSE;
734 if (!bus_transaction_send_from_driver (transaction, connection, reply))
736 dbus_message_unref (reply);
738 return BUS_RESULT_FALSE;
742 dbus_message_unref (reply);
743 return BUS_RESULT_TRUE;
748 bus_driver_handle_acquire_service (DBusConnection *connection,
749 BusTransaction *transaction,
750 DBusMessage *message,
754 DBusString service_name;
756 dbus_uint32_t service_reply;
759 BusRegistry *registry;
761 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
763 registry = bus_connection_get_registry (connection);
765 if (!dbus_message_get_args (message, error,
766 DBUS_TYPE_STRING, &name,
767 DBUS_TYPE_UINT32, &flags,
769 return BUS_RESULT_FALSE;
771 _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
773 retval = BUS_RESULT_FALSE;
776 _dbus_string_init_const (&service_name, name);
778 switch (bus_registry_acquire_service (registry, connection, message,
779 &service_name, flags,
780 &service_reply, transaction,
783 case BUS_RESULT_TRUE:
785 case BUS_RESULT_FALSE:
787 case BUS_RESULT_LATER:
788 retval = BUS_RESULT_LATER;
792 reply = dbus_message_new_method_return (message);
799 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
805 if (!bus_transaction_send_from_driver (transaction, connection, reply))
811 retval = BUS_RESULT_TRUE;
815 dbus_message_unref (reply);
820 bus_driver_handle_release_service (DBusConnection *connection,
821 BusTransaction *transaction,
822 DBusMessage *message,
826 DBusString service_name;
828 dbus_uint32_t service_reply;
830 BusRegistry *registry;
832 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
834 registry = bus_connection_get_registry (connection);
836 if (!dbus_message_get_args (message, error,
837 DBUS_TYPE_STRING, &name,
839 return BUS_RESULT_FALSE;
841 _dbus_verbose ("Trying to release name %s\n", name);
843 retval = BUS_RESULT_FALSE;
846 _dbus_string_init_const (&service_name, name);
848 if (!bus_registry_release_service (registry, connection,
849 &service_name, &service_reply,
853 reply = dbus_message_new_method_return (message);
860 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
866 if (!bus_transaction_send_from_driver (transaction, connection, reply))
872 retval = BUS_RESULT_TRUE;
876 dbus_message_unref (reply);
881 bus_driver_handle_service_exists (DBusConnection *connection,
882 BusTransaction *transaction,
883 DBusMessage *message,
887 DBusString service_name;
889 dbus_bool_t service_exists;
892 BusRegistry *registry;
894 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
896 registry = bus_connection_get_registry (connection);
898 if (!dbus_message_get_args (message, error,
899 DBUS_TYPE_STRING, &name,
901 return BUS_RESULT_FALSE;
903 retval = BUS_RESULT_FALSE;
905 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
907 service_exists = TRUE;
911 _dbus_string_init_const (&service_name, name);
912 service = bus_registry_lookup (registry, &service_name);
913 service_exists = service != NULL;
916 reply = dbus_message_new_method_return (message);
923 if (!dbus_message_append_args (reply,
924 DBUS_TYPE_BOOLEAN, &service_exists,
931 if (!bus_transaction_send_from_driver (transaction, connection, reply))
937 retval = BUS_RESULT_TRUE;
941 dbus_message_unref (reply);
947 bus_driver_handle_activate_service (DBusConnection *connection,
948 BusTransaction *transaction,
949 DBusMessage *message,
955 BusActivation *activation;
957 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
959 activation = bus_connection_get_activation (connection);
961 if (!dbus_message_get_args (message, error,
962 DBUS_TYPE_STRING, &name,
963 DBUS_TYPE_UINT32, &flags,
966 _DBUS_ASSERT_ERROR_IS_SET (error);
967 _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
968 return BUS_RESULT_FALSE;
971 retval = BUS_RESULT_FALSE;
973 if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
974 message, name, error))
976 _DBUS_ASSERT_ERROR_IS_SET (error);
977 _dbus_verbose ("bus_activation_activate_service() failed\n");
981 retval = BUS_RESULT_TRUE;
988 send_ack_reply (DBusConnection *connection,
989 BusTransaction *transaction,
990 DBusMessage *message,
995 if (dbus_message_get_no_reply (message))
998 reply = dbus_message_new_method_return (message);
1001 BUS_SET_OOM (error);
1005 if (!bus_transaction_send_from_driver (transaction, connection, reply))
1007 BUS_SET_OOM (error);
1008 dbus_message_unref (reply);
1012 dbus_message_unref (reply);
1018 * Send a message from the driver, activating the destination if necessary.
1019 * The message must already have a destination set.
1022 bus_driver_send_or_activate (BusTransaction *transaction,
1023 DBusMessage *message,
1026 BusContext *context;
1027 BusService *service;
1028 const char *service_name;
1029 DBusString service_string;
1031 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1033 service_name = dbus_message_get_destination (message);
1035 _dbus_assert (service_name != NULL);
1037 _dbus_string_init_const (&service_string, service_name);
1039 context = bus_transaction_get_context (transaction);
1041 service = bus_registry_lookup (bus_context_get_registry (context),
1044 if (service == NULL)
1046 /* destination isn't connected yet; pass the message to activation */
1047 BusActivation *activation;
1049 activation = bus_context_get_activation (context);
1051 if (!bus_transaction_capture (transaction, NULL, message))
1053 BUS_SET_OOM (error);
1054 _dbus_verbose ("No memory for bus_transaction_capture()");
1058 if (!bus_activation_activate_service (activation, NULL, transaction, TRUE,
1059 message, service_name, error))
1061 _DBUS_ASSERT_ERROR_IS_SET (error);
1062 _dbus_verbose ("bus_activation_activate_service() failed");
1068 DBusConnection *service_conn;
1070 service_conn = bus_service_get_primary_owners_connection (service);
1072 if (!bus_transaction_send_from_driver (transaction, service_conn, message))
1074 BUS_SET_OOM (error);
1075 _dbus_verbose ("No memory for bus_transaction_send_from_driver()");
1084 bus_driver_handle_update_activation_environment (DBusConnection *connection,
1085 BusTransaction *transaction,
1086 DBusMessage *message,
1090 BusActivation *activation;
1091 BusContext *context;
1092 DBusMessageIter iter;
1093 DBusMessageIter dict_iter;
1094 DBusMessageIter dict_entry_iter;
1097 DBusList *keys, *key_link;
1098 DBusList *values, *value_link;
1099 DBusMessage *systemd_message;
1100 DBusMessageIter systemd_iter;
1102 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1104 if (!bus_driver_check_message_is_for_us (message, error))
1109 /* UpdateActivationEnvironment is basically a recipe for privilege
1110 * escalation so let's be extra-careful: do not allow the sysadmin
1111 * to shoot themselves in the foot.
1113 if (!bus_driver_check_caller_is_privileged (connection, transaction,
1119 context = bus_connection_get_context (connection);
1121 if (bus_context_get_servicehelper (context) != NULL)
1123 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1124 "Cannot change activation environment "
1125 "on a system bus.");
1129 activation = bus_connection_get_activation (connection);
1131 dbus_message_iter_init (message, &iter);
1133 /* The message signature has already been checked for us,
1134 * so let's just assert it's right.
1136 _dbus_assert (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY);
1138 dbus_message_iter_recurse (&iter, &dict_iter);
1140 retval = BUS_RESULT_FALSE;
1141 systemd_message = NULL;
1143 /* Then loop through the sent dictionary, add the location of
1144 * the environment keys and values to lists. The result will
1145 * be in reverse order, so we don't have to constantly search
1146 * for the end of the list in a loop.
1150 while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
1152 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
1154 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
1160 dbus_message_iter_get_basic (&dict_entry_iter, &key);
1161 dbus_message_iter_next (&dict_entry_iter);
1163 value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
1165 if (value_type != DBUS_TYPE_STRING)
1168 dbus_message_iter_get_basic (&dict_entry_iter, &value);
1170 if (!_dbus_list_append (&keys, key))
1172 BUS_SET_OOM (error);
1176 if (!_dbus_list_append (&values, value))
1178 BUS_SET_OOM (error);
1182 dbus_message_iter_next (&dict_entry_iter);
1185 if (key_type != DBUS_TYPE_INVALID)
1188 dbus_message_iter_next (&dict_iter);
1191 if (array_type != DBUS_TYPE_INVALID)
1194 _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
1196 if (bus_context_get_systemd_activation (bus_connection_get_context (connection)))
1198 /* Prepare a call to forward environment updates to systemd */
1199 systemd_message = dbus_message_new_method_call ("org.freedesktop.systemd1",
1200 "/org/freedesktop/systemd1",
1201 "org.freedesktop.systemd1.Manager",
1203 if (systemd_message == NULL ||
1204 !dbus_message_set_sender (systemd_message, DBUS_SERVICE_DBUS))
1206 BUS_SET_OOM (error);
1207 _dbus_verbose ("No memory to create systemd message\n");
1211 dbus_message_set_no_reply (systemd_message, TRUE);
1212 dbus_message_iter_init_append (systemd_message, &iter);
1214 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY, "s",
1217 BUS_SET_OOM (error);
1218 _dbus_verbose ("No memory to open systemd message container\n");
1224 value_link = values;
1225 while (key_link != NULL)
1230 key = key_link->data;
1231 value = value_link->data;
1233 if (!bus_activation_set_environment_variable (activation,
1236 _DBUS_ASSERT_ERROR_IS_SET (error);
1237 _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
1241 if (systemd_message != NULL)
1246 /* SetEnvironment wants an array of KEY=VALUE strings */
1247 if (!_dbus_string_init (&envline) ||
1248 !_dbus_string_append_printf (&envline, "%s=%s", key, value))
1250 BUS_SET_OOM (error);
1251 _dbus_verbose ("No memory to format systemd environment line\n");
1252 _dbus_string_free (&envline);
1256 s = _dbus_string_get_data (&envline);
1258 if (!dbus_message_iter_append_basic (&systemd_iter,
1259 DBUS_TYPE_STRING, &s))
1261 BUS_SET_OOM (error);
1262 _dbus_verbose ("No memory to append systemd environment line\n");
1263 _dbus_string_free (&envline);
1267 _dbus_string_free (&envline);
1270 key_link = _dbus_list_get_next_link (&keys, key_link);
1271 value_link = _dbus_list_get_next_link (&values, value_link);
1274 /* FIXME: We can fail early having set only some of the environment variables,
1275 * (because of OOM failure). It's sort of hard to fix and it doesn't really
1276 * matter, so we're punting for now.
1278 if (key_link != NULL)
1280 if (systemd_message != NULL)
1281 dbus_message_iter_abandon_container (&iter, &systemd_iter);
1285 if (systemd_message != NULL)
1287 if (!dbus_message_iter_close_container (&iter, &systemd_iter))
1289 BUS_SET_OOM (error);
1290 _dbus_verbose ("No memory to close systemd message container\n");
1294 if (!bus_driver_send_or_activate (transaction, systemd_message, error))
1296 _DBUS_ASSERT_ERROR_IS_SET (error);
1297 _dbus_verbose ("bus_driver_send_or_activate() failed\n");
1302 if (!send_ack_reply (connection, transaction,
1306 retval = BUS_RESULT_TRUE;
1309 if (systemd_message != NULL)
1310 dbus_message_unref (systemd_message);
1311 _dbus_list_clear (&keys);
1312 _dbus_list_clear (&values);
1317 bus_driver_handle_add_match (DBusConnection *connection,
1318 BusTransaction *transaction,
1319 DBusMessage *message,
1323 const char *text, *bustype;
1325 BusMatchmaker *matchmaker;
1326 BusContext *context;
1328 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1333 if (bus_connection_get_n_match_rules (connection) >=
1334 bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
1336 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1337 "Connection \"%s\" is not allowed to add more match rules "
1338 "(increase limits in configuration file if required)",
1339 bus_connection_is_active (connection) ?
1340 bus_connection_get_name (connection) :
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 context = bus_transaction_get_context (transaction);
1360 bustype = context ? bus_context_get_type (context) : NULL;
1361 if (bus_match_rule_get_client_is_eavesdropping (rule) &&
1362 !bus_apparmor_allows_eavesdropping (connection, bustype, error))
1365 matchmaker = bus_connection_get_matchmaker (connection);
1367 if (!bus_matchmaker_add_rule (matchmaker, rule))
1369 BUS_SET_OOM (error);
1373 if (!send_ack_reply (connection, transaction,
1376 bus_matchmaker_remove_rule (matchmaker, rule);
1380 bus_match_rule_unref (rule);
1382 return BUS_RESULT_TRUE;
1385 _DBUS_ASSERT_ERROR_IS_SET (error);
1387 bus_match_rule_unref (rule);
1388 return BUS_RESULT_FALSE;
1392 bus_driver_handle_remove_match (DBusConnection *connection,
1393 BusTransaction *transaction,
1394 DBusMessage *message,
1400 BusMatchmaker *matchmaker;
1402 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1407 if (!dbus_message_get_args (message, error,
1408 DBUS_TYPE_STRING, &text,
1411 _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
1415 _dbus_string_init_const (&str, text);
1417 rule = bus_match_rule_parse (connection, &str, error);
1421 /* Send the ack before we remove the rule, since the ack is undone
1422 * on transaction cancel, but rule removal isn't.
1424 if (!send_ack_reply (connection, transaction,
1428 matchmaker = bus_connection_get_matchmaker (connection);
1430 if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
1433 bus_match_rule_unref (rule);
1435 return BUS_RESULT_TRUE;
1438 _DBUS_ASSERT_ERROR_IS_SET (error);
1440 bus_match_rule_unref (rule);
1441 return BUS_RESULT_FALSE;
1445 bus_driver_handle_get_service_owner (DBusConnection *connection,
1446 BusTransaction *transaction,
1447 DBusMessage *message,
1451 const char *base_name;
1453 BusRegistry *registry;
1454 BusService *service;
1457 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1459 registry = bus_connection_get_registry (connection);
1464 if (! dbus_message_get_args (message, error,
1465 DBUS_TYPE_STRING, &text,
1469 _dbus_string_init_const (&str, text);
1470 service = bus_registry_lookup (registry, &str);
1471 if (service == NULL &&
1472 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1474 /* ORG_FREEDESKTOP_DBUS owns itself */
1475 base_name = DBUS_SERVICE_DBUS;
1477 else if (service == NULL)
1479 dbus_set_error (error,
1480 DBUS_ERROR_NAME_HAS_NO_OWNER,
1481 "Could not get owner of name '%s': no such name", text);
1486 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1487 if (base_name == NULL)
1489 /* FIXME - how is this error possible? */
1490 dbus_set_error (error,
1492 "Could not determine unique name for '%s'", text);
1495 _dbus_assert (*base_name == ':');
1498 _dbus_assert (base_name != NULL);
1500 reply = dbus_message_new_method_return (message);
1504 if (! dbus_message_append_args (reply,
1505 DBUS_TYPE_STRING, &base_name,
1509 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1512 dbus_message_unref (reply);
1514 return BUS_RESULT_TRUE;
1517 BUS_SET_OOM (error);
1520 _DBUS_ASSERT_ERROR_IS_SET (error);
1522 dbus_message_unref (reply);
1523 return BUS_RESULT_FALSE;
1527 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1528 BusTransaction *transaction,
1529 DBusMessage *message,
1533 DBusList *base_names;
1536 BusRegistry *registry;
1537 BusService *service;
1539 DBusMessageIter iter, array_iter;
1540 char *dbus_service_name = DBUS_SERVICE_DBUS;
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, 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);
1617 return BUS_RESULT_TRUE;
1620 BUS_SET_OOM (error);
1623 _DBUS_ASSERT_ERROR_IS_SET (error);
1625 dbus_message_unref (reply);
1628 _dbus_list_clear (&base_names);
1630 return BUS_RESULT_FALSE;
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:
1665 if (uid == DBUS_UID_UNSET)
1667 dbus_set_error (error,
1669 "Could not determine UID for '%s'", service);
1673 reply = dbus_message_new_method_return (message);
1678 if (! dbus_message_append_args (reply,
1679 DBUS_TYPE_UINT32, &uid32,
1683 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1686 dbus_message_unref (reply);
1688 return BUS_RESULT_TRUE;
1691 BUS_SET_OOM (error);
1694 _DBUS_ASSERT_ERROR_IS_SET (error);
1696 dbus_message_unref (reply);
1697 return BUS_RESULT_FALSE;
1701 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1702 BusTransaction *transaction,
1703 DBusMessage *message,
1706 DBusConnection *conn;
1709 dbus_uint32_t pid32;
1710 const char *service;
1711 BusDriverFound found;
1713 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1717 found = bus_driver_get_conn_helper (connection, message, "PID", &service,
1721 case BUS_DRIVER_FOUND_SELF:
1722 pid = _dbus_getpid ();
1724 case BUS_DRIVER_FOUND_PEER:
1725 if (!dbus_connection_get_unix_process_id (conn, &pid))
1726 pid = DBUS_PID_UNSET;
1728 case BUS_DRIVER_FOUND_ERROR:
1732 if (pid == DBUS_PID_UNSET)
1734 dbus_set_error (error,
1735 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1736 "Could not determine PID for '%s'", service);
1740 reply = dbus_message_new_method_return (message);
1745 if (! dbus_message_append_args (reply,
1746 DBUS_TYPE_UINT32, &pid32,
1750 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1753 dbus_message_unref (reply);
1755 return BUS_RESULT_TRUE;
1758 BUS_SET_OOM (error);
1761 _DBUS_ASSERT_ERROR_IS_SET (error);
1763 dbus_message_unref (reply);
1764 return BUS_RESULT_FALSE;
1768 bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
1769 BusTransaction *transaction,
1770 DBusMessage *message,
1773 DBusConnection *conn;
1776 dbus_uint32_t data_size;
1777 const char *service;
1778 BusDriverFound found;
1780 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1784 found = bus_driver_get_conn_helper (connection, message, "audit session data",
1785 &service, &conn, error);
1787 if (found == BUS_DRIVER_FOUND_ERROR)
1790 reply = dbus_message_new_method_return (message);
1794 /* We don't know how to find "ADT audit session data" for the bus daemon
1795 * itself. Is that even meaningful?
1796 * FIXME: Implement this or briefly note it makes no sense.
1798 if (found != BUS_DRIVER_FOUND_PEER ||
1799 !dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) ||
1802 dbus_set_error (error,
1803 DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
1804 "Could not determine audit session data for '%s'", service);
1808 if (! dbus_message_append_args (reply,
1809 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
1813 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1816 dbus_message_unref (reply);
1818 return BUS_RESULT_TRUE;
1821 BUS_SET_OOM (error);
1824 _DBUS_ASSERT_ERROR_IS_SET (error);
1826 dbus_message_unref (reply);
1827 return BUS_RESULT_FALSE;
1831 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1832 BusTransaction *transaction,
1833 DBusMessage *message,
1836 DBusConnection *conn;
1838 BusSELinuxID *context;
1839 const char *service;
1840 BusDriverFound found;
1842 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1846 found = bus_driver_get_conn_helper (connection, message, "security context",
1847 &service, &conn, error);
1849 if (found == BUS_DRIVER_FOUND_ERROR)
1852 reply = dbus_message_new_method_return (message);
1856 /* FIXME: Obtain the SELinux security context for the bus daemon itself */
1857 if (found == BUS_DRIVER_FOUND_PEER)
1858 context = bus_connection_get_selinux_id (conn);
1864 dbus_set_error (error,
1865 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1866 "Could not determine security context for '%s'", service);
1870 if (! bus_selinux_append_context (reply, context, error))
1873 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1876 dbus_message_unref (reply);
1878 return BUS_RESULT_TRUE;
1881 BUS_SET_OOM (error);
1884 _DBUS_ASSERT_ERROR_IS_SET (error);
1886 dbus_message_unref (reply);
1887 return BUS_RESULT_FALSE;
1891 bus_driver_handle_get_connection_credentials (DBusConnection *connection,
1892 BusTransaction *transaction,
1893 DBusMessage *message,
1896 DBusConnection *conn;
1898 DBusMessageIter reply_iter;
1899 DBusMessageIter array_iter;
1900 unsigned long ulong_uid, ulong_pid;
1902 const char *service;
1903 BusDriverFound found;
1905 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1909 found = bus_driver_get_conn_helper (connection, message, "credentials",
1910 &service, &conn, error);
1914 case BUS_DRIVER_FOUND_SELF:
1915 ulong_pid = _dbus_getpid ();
1916 ulong_uid = _dbus_getuid ();
1919 case BUS_DRIVER_FOUND_PEER:
1920 if (!dbus_connection_get_unix_process_id (conn, &ulong_pid))
1921 ulong_pid = DBUS_PID_UNSET;
1922 if (!dbus_connection_get_unix_user (conn, &ulong_uid))
1923 ulong_uid = DBUS_UID_UNSET;
1925 case BUS_DRIVER_FOUND_ERROR:
1929 reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
1933 /* we can't represent > 32-bit pids; if your system needs them, please
1934 * add ProcessID64 to the spec or something */
1935 if (ulong_pid <= _DBUS_UINT32_MAX && ulong_pid != DBUS_PID_UNSET &&
1936 !_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_pid))
1939 /* we can't represent > 32-bit uids; if your system needs them, please
1940 * add UnixUserID64 to the spec or something */
1941 if (ulong_uid <= _DBUS_UINT32_MAX && ulong_uid != DBUS_UID_UNSET &&
1942 !_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_uid))
1945 /* FIXME: Obtain the Windows user of the bus daemon itself */
1946 if (found == BUS_DRIVER_FOUND_PEER &&
1947 dbus_connection_get_windows_user (conn, &s))
1955 _dbus_string_init_const (&str, s);
1956 result = _dbus_validate_utf8 (&str, 0, _dbus_string_get_length (&str));
1957 _dbus_string_free (&str);
1960 if (!_dbus_asv_add_string (&array_iter, "WindowsSID", s))
1969 /* FIXME: Obtain the security label for the bus daemon itself */
1970 if (found == BUS_DRIVER_FOUND_PEER &&
1971 _dbus_connection_get_linux_security_label (conn, &s))
1976 /* use the GVariant bytestring convention for strings of unknown
1977 * encoding: include the \0 in the payload, for zero-copy reading */
1978 if (!_dbus_asv_add_byte_array (&array_iter, "LinuxSecurityLabel",
1988 #ifdef DBUS_ENABLE_SMACK
1990 const char *smack_label;
1991 if (dbus_connection_get_smack_label (conn, &smack_label)) {
1992 if (!_dbus_asv_add_string (&array_iter, "SmackLabel", smack_label))
1998 if (!_dbus_asv_close (&reply_iter, &array_iter))
2001 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2003 /* this time we don't want to close the iterator again, so just
2004 * get rid of the message */
2005 dbus_message_unref (reply);
2010 dbus_message_unref (reply);
2012 return BUS_RESULT_TRUE;
2015 BUS_SET_OOM (error);
2018 _DBUS_ASSERT_ERROR_IS_SET (error);
2022 _dbus_asv_abandon (&reply_iter, &array_iter);
2023 dbus_message_unref (reply);
2026 return BUS_RESULT_FALSE;
2030 bus_driver_handle_reload_config (DBusConnection *connection,
2031 BusTransaction *transaction,
2032 DBusMessage *message,
2035 BusContext *context;
2038 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2042 context = bus_connection_get_context (connection);
2043 if (!bus_context_reload_config (context, error))
2046 reply = dbus_message_new_method_return (message);
2050 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2053 dbus_message_unref (reply);
2054 return BUS_RESULT_TRUE;
2057 BUS_SET_OOM (error);
2060 _DBUS_ASSERT_ERROR_IS_SET (error);
2062 dbus_message_unref (reply);
2063 return BUS_RESULT_FALSE;
2066 #ifdef DBUS_ENABLE_VERBOSE_MODE
2068 bus_driver_handle_enable_verbose (DBusConnection *connection,
2069 BusTransaction *transaction,
2070 DBusMessage *message,
2073 DBusMessage *reply = NULL;
2075 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2077 reply = dbus_message_new_method_return (message);
2081 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2084 _dbus_set_verbose(TRUE);
2086 dbus_message_unref (reply);
2090 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2092 BUS_SET_OOM (error);
2095 dbus_message_unref (reply);
2100 bus_driver_handle_disable_verbose (DBusConnection *connection,
2101 BusTransaction *transaction,
2102 DBusMessage *message,
2105 DBusMessage *reply = NULL;
2107 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2109 reply = dbus_message_new_method_return (message);
2113 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2116 _dbus_set_verbose(FALSE);
2118 dbus_message_unref (reply);
2122 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2124 BUS_SET_OOM (error);
2127 dbus_message_unref (reply);
2133 bus_driver_handle_get_id (DBusConnection *connection,
2134 BusTransaction *transaction,
2135 DBusMessage *message,
2138 BusContext *context;
2141 const char *v_STRING;
2143 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2145 if (!_dbus_string_init (&uuid))
2147 BUS_SET_OOM (error);
2148 return BUS_RESULT_FALSE;
2153 context = bus_connection_get_context (connection);
2154 if (!bus_context_get_id (context, &uuid))
2157 reply = dbus_message_new_method_return (message);
2161 v_STRING = _dbus_string_get_const_data (&uuid);
2162 if (!dbus_message_append_args (reply,
2163 DBUS_TYPE_STRING, &v_STRING,
2167 _dbus_assert (dbus_message_has_signature (reply, "s"));
2169 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2172 _dbus_string_free (&uuid);
2173 dbus_message_unref (reply);
2174 return BUS_RESULT_TRUE;
2177 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2179 BUS_SET_OOM (error);
2182 dbus_message_unref (reply);
2183 _dbus_string_free (&uuid);
2184 return BUS_RESULT_FALSE;
2188 bus_driver_handle_become_monitor (DBusConnection *connection,
2189 BusTransaction *transaction,
2190 DBusMessage *message,
2193 char **match_rules = NULL;
2194 const char *bustype;
2195 BusContext *context;
2197 DBusList *rules = NULL;
2202 dbus_uint32_t flags;
2203 dbus_bool_t ret = FALSE;
2205 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2207 if (!bus_driver_check_message_is_for_us (message, error))
2210 context = bus_transaction_get_context (transaction);
2211 bustype = context ? bus_context_get_type (context) : NULL;
2212 if (!bus_apparmor_allows_eavesdropping (connection, bustype, error))
2215 if (!bus_driver_check_caller_is_privileged (connection, transaction,
2219 if (!dbus_message_get_args (message, error,
2220 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &match_rules, &n_match_rules,
2221 DBUS_TYPE_UINT32, &flags,
2227 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2228 "BecomeMonitor does not support any flags yet");
2232 /* Special case: a zero-length array becomes [""] */
2233 if (n_match_rules == 0)
2235 match_rules = dbus_malloc (2 * sizeof (char *));
2237 if (match_rules == NULL)
2239 BUS_SET_OOM (error);
2243 match_rules[0] = _dbus_strdup ("");
2245 if (match_rules[0] == NULL)
2247 BUS_SET_OOM (error);
2251 match_rules[1] = NULL;
2255 for (i = 0; i < n_match_rules; i++)
2257 _dbus_string_init_const (&str, match_rules[i]);
2258 rule = bus_match_rule_parse (connection, &str, error);
2263 /* monitors always eavesdrop */
2264 bus_match_rule_set_client_is_eavesdropping (rule, TRUE);
2266 if (!_dbus_list_append (&rules, rule))
2268 BUS_SET_OOM (error);
2269 bus_match_rule_unref (rule);
2274 /* Send the ack before we remove the rule, since the ack is undone
2275 * on transaction cancel, but becoming a monitor isn't.
2277 if (!send_ack_reply (connection, transaction, message, error))
2280 if (!bus_connection_be_monitor (connection, transaction, &rules, error))
2287 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2289 _DBUS_ASSERT_ERROR_IS_SET (error);
2291 for (iter = _dbus_list_get_first_link (&rules);
2293 iter = _dbus_list_get_next_link (&rules, iter))
2294 bus_match_rule_unref (iter->data);
2296 _dbus_list_clear (&rules);
2298 dbus_free_string_array (match_rules);
2305 const char *in_args;
2306 const char *out_args;
2307 BusResult (* handler) (DBusConnection *connection,
2308 BusTransaction *transaction,
2309 DBusMessage *message,
2313 /* For speed it might be useful to sort this in order of
2314 * frequency of use (but doesn't matter with only a few items
2317 static const MessageHandler dbus_message_handlers[] = {
2320 DBUS_TYPE_STRING_AS_STRING,
2321 bus_driver_handle_hello },
2323 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
2324 DBUS_TYPE_UINT32_AS_STRING,
2325 bus_driver_handle_acquire_service },
2327 DBUS_TYPE_STRING_AS_STRING,
2328 DBUS_TYPE_UINT32_AS_STRING,
2329 bus_driver_handle_release_service },
2330 { "StartServiceByName",
2331 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
2332 DBUS_TYPE_UINT32_AS_STRING,
2333 bus_driver_handle_activate_service },
2334 { "UpdateActivationEnvironment",
2335 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,
2337 bus_driver_handle_update_activation_environment },
2339 DBUS_TYPE_STRING_AS_STRING,
2340 DBUS_TYPE_BOOLEAN_AS_STRING,
2341 bus_driver_handle_service_exists },
2344 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
2345 bus_driver_handle_list_services },
2346 { "ListActivatableNames",
2348 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
2349 bus_driver_handle_list_activatable_services },
2351 DBUS_TYPE_STRING_AS_STRING,
2353 bus_driver_handle_add_match },
2355 DBUS_TYPE_STRING_AS_STRING,
2357 bus_driver_handle_remove_match },
2359 DBUS_TYPE_STRING_AS_STRING,
2360 DBUS_TYPE_STRING_AS_STRING,
2361 bus_driver_handle_get_service_owner },
2362 { "ListQueuedOwners",
2363 DBUS_TYPE_STRING_AS_STRING,
2364 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
2365 bus_driver_handle_list_queued_owners },
2366 { "GetConnectionUnixUser",
2367 DBUS_TYPE_STRING_AS_STRING,
2368 DBUS_TYPE_UINT32_AS_STRING,
2369 bus_driver_handle_get_connection_unix_user },
2370 { "GetConnectionUnixProcessID",
2371 DBUS_TYPE_STRING_AS_STRING,
2372 DBUS_TYPE_UINT32_AS_STRING,
2373 bus_driver_handle_get_connection_unix_process_id },
2374 { "GetAdtAuditSessionData",
2375 DBUS_TYPE_STRING_AS_STRING,
2376 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
2377 bus_driver_handle_get_adt_audit_session_data },
2378 { "GetConnectionSELinuxSecurityContext",
2379 DBUS_TYPE_STRING_AS_STRING,
2380 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
2381 bus_driver_handle_get_connection_selinux_security_context },
2385 bus_driver_handle_reload_config },
2388 DBUS_TYPE_STRING_AS_STRING,
2389 bus_driver_handle_get_id },
2390 { "GetConnectionCredentials", "s", "a{sv}",
2391 bus_driver_handle_get_connection_credentials },
2392 { "GetConnectionSmackContext",
2393 DBUS_TYPE_STRING_AS_STRING,
2394 DBUS_TYPE_STRING_AS_STRING,
2395 bus_smack_handle_get_connection_context },
2396 { NULL, NULL, NULL, NULL }
2399 static BusResult bus_driver_handle_introspect (DBusConnection *,
2400 BusTransaction *, DBusMessage *, DBusError *);
2402 static const MessageHandler introspectable_message_handlers[] = {
2403 { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
2404 { NULL, NULL, NULL, NULL }
2407 static const MessageHandler monitoring_message_handlers[] = {
2408 { "BecomeMonitor", "asu", "", bus_driver_handle_become_monitor },
2409 { NULL, NULL, NULL, NULL }
2412 #ifdef DBUS_ENABLE_VERBOSE_MODE
2413 static const MessageHandler verbose_message_handlers[] = {
2414 { "EnableVerbose", "", "", bus_driver_handle_enable_verbose},
2415 { "DisableVerbose", "", "", bus_driver_handle_disable_verbose},
2416 { NULL, NULL, NULL, NULL }
2420 #ifdef DBUS_ENABLE_STATS
2421 static const MessageHandler stats_message_handlers[] = {
2422 { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
2423 { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
2424 { "GetAllMatchRules", "", "a{sas}", bus_stats_handle_get_all_match_rules },
2425 { NULL, NULL, NULL, NULL }
2431 const MessageHandler *message_handlers;
2432 const char *extra_introspection;
2435 /* These should ideally be sorted by frequency of use, although it
2436 * probably doesn't matter with this few items */
2437 static InterfaceHandler interface_handlers[] = {
2438 { DBUS_INTERFACE_DBUS, dbus_message_handlers,
2439 " <signal name=\"NameOwnerChanged\">\n"
2440 " <arg type=\"s\"/>\n"
2441 " <arg type=\"s\"/>\n"
2442 " <arg type=\"s\"/>\n"
2444 " <signal name=\"NameLost\">\n"
2445 " <arg type=\"s\"/>\n"
2447 " <signal name=\"NameAcquired\">\n"
2448 " <arg type=\"s\"/>\n"
2450 { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
2451 { DBUS_INTERFACE_MONITORING, monitoring_message_handlers, NULL },
2452 #ifdef DBUS_ENABLE_VERBOSE_MODE
2453 { DBUS_INTERFACE_VERBOSE, verbose_message_handlers, NULL },
2455 #ifdef DBUS_ENABLE_STATS
2456 { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
2458 { NULL, NULL, NULL }
2462 write_args_for_direction (DBusString *xml,
2463 const char *signature,
2466 DBusTypeReader typereader;
2470 _dbus_string_init_const (&sigstr, signature);
2471 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
2473 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
2475 const DBusString *subsig;
2478 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
2479 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
2482 if (!_dbus_string_append_len (xml,
2483 _dbus_string_get_const_data (subsig) + start,
2486 if (!_dbus_string_append (xml, "\"/>\n"))
2489 _dbus_type_reader_next (&typereader);
2497 bus_driver_generate_introspect_string (DBusString *xml)
2499 const InterfaceHandler *ih;
2500 const MessageHandler *mh;
2502 if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
2504 if (!_dbus_string_append (xml, "<node>\n"))
2507 for (ih = interface_handlers; ih->name != NULL; ih++)
2509 if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n",
2513 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2515 if (!_dbus_string_append_printf (xml, " <method name=\"%s\">\n",
2519 if (!write_args_for_direction (xml, mh->in_args, TRUE))
2522 if (!write_args_for_direction (xml, mh->out_args, FALSE))
2525 if (!_dbus_string_append (xml, " </method>\n"))
2529 if (ih->extra_introspection != NULL &&
2530 !_dbus_string_append (xml, ih->extra_introspection))
2533 if (!_dbus_string_append (xml, " </interface>\n"))
2537 if (!_dbus_string_append (xml, "</node>\n"))
2544 bus_driver_handle_introspect (DBusConnection *connection,
2545 BusTransaction *transaction,
2546 DBusMessage *message,
2551 const char *v_STRING;
2553 _dbus_verbose ("Introspect() on bus driver\n");
2555 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2559 if (! dbus_message_get_args (message, error,
2562 _DBUS_ASSERT_ERROR_IS_SET (error);
2563 return BUS_RESULT_FALSE;
2566 if (!_dbus_string_init (&xml))
2568 BUS_SET_OOM (error);
2569 return BUS_RESULT_FALSE;
2572 if (!bus_driver_generate_introspect_string (&xml))
2575 v_STRING = _dbus_string_get_const_data (&xml);
2577 reply = dbus_message_new_method_return (message);
2581 if (! dbus_message_append_args (reply,
2582 DBUS_TYPE_STRING, &v_STRING,
2586 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2589 dbus_message_unref (reply);
2590 _dbus_string_free (&xml);
2592 return BUS_RESULT_TRUE;
2595 BUS_SET_OOM (error);
2598 dbus_message_unref (reply);
2600 _dbus_string_free (&xml);
2602 return BUS_RESULT_FALSE;
2606 * Set @error and return FALSE if the message is not directed to the
2607 * dbus-daemon by its canonical object path. This is hardening against
2608 * system services with poorly-written security policy files, which
2609 * might allow sending dangerously broad equivalence classes of messages
2610 * such as "anything with this assumed-to-be-safe object path".
2612 * dbus-daemon is unusual in that it normally ignores the object path
2613 * of incoming messages; we need to keep that behaviour for the "read"
2614 * read-only method calls like GetConnectionUnixUser for backwards
2615 * compatibility, but it seems safer to be more restrictive for things
2616 * intended to be root-only or privileged-developers-only.
2618 * It is possible that there are other system services with the same
2619 * quirk as dbus-daemon.
2622 bus_driver_check_message_is_for_us (DBusMessage *message,
2625 if (!dbus_message_has_path (message, DBUS_PATH_DBUS))
2627 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
2628 "Method '%s' is only available at the canonical object path '%s'",
2629 dbus_message_get_member (message), DBUS_PATH_DBUS);
2638 bus_driver_handle_message (DBusConnection *connection,
2639 BusTransaction *transaction,
2640 DBusMessage *message,
2643 const char *name, *interface;
2644 const InterfaceHandler *ih;
2645 const MessageHandler *mh;
2646 dbus_bool_t found_interface = FALSE;
2648 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2650 if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
2652 BusContext *context;
2653 DBusConnection *systemd;
2655 context = bus_connection_get_context (connection);
2656 systemd = bus_driver_get_owner_of_name (connection,
2657 "org.freedesktop.systemd1");
2659 if (systemd != connection)
2661 const char *attacker;
2663 attacker = bus_connection_get_name (connection);
2664 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY,
2665 "Ignoring forged ActivationFailure message from "
2666 "connection %s (%s)",
2667 attacker ? attacker : "(unauthenticated)",
2668 bus_connection_get_loginfo (connection));
2670 return BUS_RESULT_TRUE;
2673 return dbus_activation_systemd_failure(bus_context_get_activation(context), message) == TRUE ? BUS_RESULT_TRUE : BUS_RESULT_FALSE;
2676 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
2678 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
2679 return BUS_RESULT_TRUE; /* we just ignore this */
2682 /* may be NULL, which means "any interface will do" */
2683 interface = dbus_message_get_interface (message);
2685 _dbus_assert (dbus_message_get_member (message) != NULL);
2687 name = dbus_message_get_member (message);
2689 _dbus_verbose ("Driver got a method call: %s\n", name);
2691 /* security checks should have kept this from getting here */
2692 _dbus_assert (dbus_message_get_sender (message) != NULL ||
2693 strcmp (name, "Hello") == 0);
2695 for (ih = interface_handlers; ih->name != NULL; ih++)
2697 if (interface != NULL && strcmp (interface, ih->name) != 0)
2700 found_interface = TRUE;
2702 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2704 if (strcmp (mh->name, name) != 0)
2707 _dbus_verbose ("Found driver handler for %s\n", name);
2709 if (!dbus_message_has_signature (message, mh->in_args))
2711 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2712 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
2713 name, dbus_message_get_signature (message),
2716 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2717 "Call to %s has wrong args (%s, expected %s)\n",
2718 name, dbus_message_get_signature (message),
2720 _DBUS_ASSERT_ERROR_IS_SET (error);
2721 return BUS_RESULT_FALSE;
2724 switch ((* mh->handler) (connection, transaction, message, error))
2726 case BUS_RESULT_TRUE:
2727 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2728 _dbus_verbose ("Driver handler succeeded\n");
2729 return BUS_RESULT_TRUE;
2730 case BUS_RESULT_FALSE:
2731 _DBUS_ASSERT_ERROR_IS_SET (error);
2732 _dbus_verbose ("Driver handler returned failure\n");
2733 return BUS_RESULT_FALSE;
2734 case BUS_RESULT_LATER:
2735 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2736 _dbus_verbose ("Driver handler delayed message processing due to policy check\n");
2737 return BUS_RESULT_LATER;
2742 _dbus_verbose ("No driver handler for message \"%s\"\n",
2745 dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
2746 "%s does not understand message %s",
2747 DBUS_SERVICE_DBUS, name);
2749 return BUS_RESULT_FALSE;
2753 bus_driver_remove_connection (DBusConnection *connection)
2755 /* FIXME 1.0 Does nothing for now, should unregister the connection
2756 * with the bus driver.