1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* bus.c message bus context object
4 * Copyright (C) 2003, 2004 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "activation.h"
27 #include "connection.h"
31 #include "config-parser.h"
34 #include "dir-watch.h"
35 #include <dbus/dbus-list.h>
36 #include <dbus/dbus-hash.h>
37 #include <dbus/dbus-credentials.h>
38 #include <dbus/dbus-internals.h>
53 BusConnections *connections;
54 BusActivation *activation;
55 BusRegistry *registry;
57 BusMatchmaker *matchmaker;
59 unsigned int fork : 1;
60 unsigned int syslog : 1;
61 unsigned int keep_umask : 1;
62 unsigned int allow_anonymous : 1;
63 unsigned int systemd_activation : 1;
66 static dbus_int32_t server_data_slot = -1;
73 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
76 server_get_context (DBusServer *server)
81 if (!dbus_server_allocate_data_slot (&server_data_slot))
84 bd = BUS_SERVER_DATA (server);
87 dbus_server_free_data_slot (&server_data_slot);
91 context = bd->context;
93 dbus_server_free_data_slot (&server_data_slot);
99 server_watch_callback (DBusWatch *watch,
100 unsigned int condition,
103 /* FIXME this can be done in dbus-mainloop.c
104 * if the code in activation.c for the babysitter
105 * watch handler is fixed.
108 return dbus_watch_handle (watch, condition);
112 add_server_watch (DBusWatch *watch,
115 DBusServer *server = data;
118 context = server_get_context (server);
120 return _dbus_loop_add_watch (context->loop,
121 watch, server_watch_callback, server,
126 remove_server_watch (DBusWatch *watch,
129 DBusServer *server = data;
132 context = server_get_context (server);
134 _dbus_loop_remove_watch (context->loop,
135 watch, server_watch_callback, server);
140 server_timeout_callback (DBusTimeout *timeout,
143 /* can return FALSE on OOM but we just let it fire again later */
144 dbus_timeout_handle (timeout);
148 add_server_timeout (DBusTimeout *timeout,
151 DBusServer *server = data;
154 context = server_get_context (server);
156 return _dbus_loop_add_timeout (context->loop,
157 timeout, server_timeout_callback, server, NULL);
161 remove_server_timeout (DBusTimeout *timeout,
164 DBusServer *server = data;
167 context = server_get_context (server);
169 _dbus_loop_remove_timeout (context->loop,
170 timeout, server_timeout_callback, server);
174 new_connection_callback (DBusServer *server,
175 DBusConnection *new_connection,
178 BusContext *context = data;
180 if (!bus_connections_setup_connection (context->connections, new_connection))
182 _dbus_verbose ("No memory to setup new connection\n");
184 /* if we don't do this, it will get unref'd without
185 * being disconnected... kind of strange really
186 * that we have to do this, people won't get it right
189 dbus_connection_close (new_connection);
192 dbus_connection_set_max_received_size (new_connection,
193 context->limits.max_incoming_bytes);
195 dbus_connection_set_max_message_size (new_connection,
196 context->limits.max_message_size);
198 dbus_connection_set_max_received_unix_fds (new_connection,
199 context->limits.max_incoming_unix_fds);
201 dbus_connection_set_max_message_unix_fds (new_connection,
202 context->limits.max_message_unix_fds);
204 dbus_connection_set_allow_anonymous (new_connection,
205 context->allow_anonymous);
207 /* on OOM, we won't have ref'd the connection so it will die. */
211 free_server_data (void *data)
213 BusServerData *bd = data;
219 setup_server (BusContext *context,
221 char **auth_mechanisms,
226 bd = dbus_new0 (BusServerData, 1);
227 if (bd == NULL || !dbus_server_set_data (server,
229 bd, free_server_data))
236 bd->context = context;
238 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
244 dbus_server_set_new_connection_function (server,
245 new_connection_callback,
248 if (!dbus_server_set_watch_functions (server,
259 if (!dbus_server_set_timeout_functions (server,
261 remove_server_timeout,
272 /* This code only gets executed the first time the
273 * config files are parsed. It is not executed
274 * when config files are reloaded.
277 process_config_first_time_only (BusContext *context,
278 BusConfigParser *parser,
279 const DBusString *address,
280 dbus_bool_t systemd_activation,
283 DBusString log_prefix;
285 DBusList **addresses;
286 const char *user, *pidfile;
287 char **auth_mechanisms;
288 DBusList **auth_mechanisms_list;
292 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
295 auth_mechanisms = NULL;
297 context->systemd_activation = systemd_activation;
299 /* Check for an existing pid file. Of course this is a race;
300 * we'd have to use fcntl() locks on the pid file to
301 * avoid that. But we want to check for the pid file
302 * before overwriting any existing sockets, etc.
304 pidfile = bus_config_parser_get_pidfile (parser);
310 _dbus_string_init_const (&u, pidfile);
312 if (_dbus_stat (&u, &stbuf, NULL))
314 dbus_set_error (error, DBUS_ERROR_FAILED,
315 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
321 /* keep around the pid filename so we can delete it later */
322 context->pidfile = _dbus_strdup (pidfile);
324 /* note that type may be NULL */
325 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
326 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
329 user = bus_config_parser_get_user (parser);
332 context->user = _dbus_strdup (user);
333 if (context->user == NULL)
337 /* Set up the prefix for syslog messages */
338 if (!_dbus_string_init (&log_prefix))
340 if (context->type && !strcmp (context->type, "system"))
342 if (!_dbus_string_append (&log_prefix, "[system] "))
345 else if (context->type && !strcmp (context->type, "session"))
347 DBusCredentials *credentials;
349 credentials = _dbus_credentials_new_from_current_process ();
352 if (!_dbus_string_append (&log_prefix, "[session "))
354 if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
356 if (!_dbus_string_append (&log_prefix, "] "))
358 _dbus_credentials_unref (credentials);
360 if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
362 _dbus_string_free (&log_prefix);
364 /* Build an array of auth mechanisms */
366 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
367 len = _dbus_list_get_length (auth_mechanisms_list);
373 auth_mechanisms = dbus_new0 (char*, len + 1);
374 if (auth_mechanisms == NULL)
378 link = _dbus_list_get_first_link (auth_mechanisms_list);
381 auth_mechanisms[i] = _dbus_strdup (link->data);
382 if (auth_mechanisms[i] == NULL)
384 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
389 auth_mechanisms = NULL;
392 /* Listen on our addresses */
398 server = dbus_server_listen (_dbus_string_get_const_data(address), error);
401 _DBUS_ASSERT_ERROR_IS_SET (error);
404 else if (!setup_server (context, server, auth_mechanisms, error))
406 _DBUS_ASSERT_ERROR_IS_SET (error);
410 if (!_dbus_list_append (&context->servers, server))
415 addresses = bus_config_parser_get_addresses (parser);
417 link = _dbus_list_get_first_link (addresses);
422 server = dbus_server_listen (link->data, error);
425 _DBUS_ASSERT_ERROR_IS_SET (error);
428 else if (!setup_server (context, server, auth_mechanisms, error))
430 _DBUS_ASSERT_ERROR_IS_SET (error);
434 if (!_dbus_list_append (&context->servers, server))
437 link = _dbus_list_get_next_link (addresses, link);
441 context->fork = bus_config_parser_get_fork (parser);
442 context->syslog = bus_config_parser_get_syslog (parser);
443 context->keep_umask = bus_config_parser_get_keep_umask (parser);
444 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
446 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
450 dbus_free_string_array (auth_mechanisms);
455 dbus_free_string_array (auth_mechanisms);
459 /* This code gets executed every time the config files
460 * are parsed: both during BusContext construction
461 * and on reloads. This function is slightly screwy
462 * since it can do a "half reload" in out-of-memory
463 * situations. Realistically, unlikely to ever matter.
466 process_config_every_time (BusContext *context,
467 BusConfigParser *parser,
468 dbus_bool_t is_reload,
471 DBusString full_address;
474 BusActivation *new_activation;
476 const char *servicehelper;
481 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
486 if (!_dbus_string_init (&full_address))
492 /* get our limits and timeout lengths */
493 bus_config_parser_get_limits (parser, &context->limits);
496 bus_policy_unref (context->policy);
497 context->policy = bus_config_parser_steal_policy (parser);
498 _dbus_assert (context->policy != NULL);
500 /* We have to build the address backward, so that
501 * <listen> later in the config file have priority
503 link = _dbus_list_get_last_link (&context->servers);
506 addr = dbus_server_get_address (link->data);
513 if (_dbus_string_get_length (&full_address) > 0)
515 if (!_dbus_string_append (&full_address, ";"))
522 if (!_dbus_string_append (&full_address, addr))
531 link = _dbus_list_get_prev_link (&context->servers, link);
535 dbus_free (context->address);
537 if (!_dbus_string_copy_data (&full_address, &context->address))
543 /* get the service directories */
544 dirs = bus_config_parser_get_service_dirs (parser);
546 /* and the service helper */
547 servicehelper = bus_config_parser_get_servicehelper (parser);
549 s = _dbus_strdup(servicehelper);
550 if (s == NULL && servicehelper != NULL)
557 dbus_free(context->servicehelper);
558 context->servicehelper = s;
561 /* Create activation subsystem */
562 if (context->activation)
564 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
569 context->activation = bus_activation_new (context, &full_address, dirs, error);
572 if (context->activation == NULL)
574 _DBUS_ASSERT_ERROR_IS_SET (error);
578 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
582 _dbus_string_free (&full_address);
591 list_concat_new (DBusList **a,
599 link = _dbus_list_get_first_link (a);
600 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
602 if (!_dbus_list_append (result, link->data))
605 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
607 if (!_dbus_list_append (result, link->data))
613 _dbus_list_clear (result);
618 process_config_postinit (BusContext *context,
619 BusConfigParser *parser,
622 DBusHashTable *service_context_table;
623 DBusList *watched_dirs = NULL;
625 service_context_table = bus_config_parser_steal_service_context_table (parser);
626 if (!bus_registry_set_service_context_table (context->registry,
627 service_context_table))
633 _dbus_hash_table_unref (service_context_table);
635 /* We need to monitor both the configuration directories and directories
636 * containing .service files.
638 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
639 bus_config_parser_get_service_dirs (parser),
646 bus_set_watched_dirs (context, &watched_dirs);
648 _dbus_list_clear (&watched_dirs);
654 bus_context_new (const DBusString *config_file,
655 ForceForkSetting force_fork,
656 DBusPipe *print_addr_pipe,
657 DBusPipe *print_pid_pipe,
658 const DBusString *address,
659 dbus_bool_t systemd_activation,
662 DBusString log_prefix;
664 BusConfigParser *parser;
666 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
671 if (!dbus_server_allocate_data_slot (&server_data_slot))
677 context = dbus_new0 (BusContext, 1);
683 context->refcount = 1;
685 _dbus_generate_uuid (&context->uuid);
687 if (!_dbus_string_copy_data (config_file, &context->config_file))
693 context->loop = _dbus_loop_new ();
694 if (context->loop == NULL)
700 context->registry = bus_registry_new (context);
701 if (context->registry == NULL)
707 parser = bus_config_load (config_file, TRUE, NULL, error);
710 _DBUS_ASSERT_ERROR_IS_SET (error);
714 if (!process_config_first_time_only (context, parser, address, systemd_activation, error))
716 _DBUS_ASSERT_ERROR_IS_SET (error);
719 if (!process_config_every_time (context, parser, FALSE, error))
721 _DBUS_ASSERT_ERROR_IS_SET (error);
725 /* we need another ref of the server data slot for the context
728 if (!dbus_server_allocate_data_slot (&server_data_slot))
729 _dbus_assert_not_reached ("second ref of server data slot failed");
731 /* Note that we don't know whether the print_addr_pipe is
732 * one of the sockets we're using to listen on, or some
733 * other random thing. But I think the answer is "don't do
736 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
739 const char *a = bus_context_get_address (context);
742 _dbus_assert (a != NULL);
743 if (!_dbus_string_init (&addr))
749 if (!_dbus_string_append (&addr, a) ||
750 !_dbus_string_append (&addr, "\n"))
752 _dbus_string_free (&addr);
757 bytes = _dbus_string_get_length (&addr);
758 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
760 /* pipe write returns an error on failure but not short write */
761 if (error != NULL && !dbus_error_is_set (error))
763 dbus_set_error (error, DBUS_ERROR_FAILED,
764 "Printing message bus address: did not write all bytes\n");
766 _dbus_string_free (&addr);
770 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
771 _dbus_pipe_close (print_addr_pipe, NULL);
773 _dbus_string_free (&addr);
776 context->connections = bus_connections_new (context);
777 if (context->connections == NULL)
783 context->matchmaker = bus_matchmaker_new ();
784 if (context->matchmaker == NULL)
790 /* check user before we fork */
791 if (context->user != NULL)
793 if (!_dbus_verify_daemon_user (context->user))
795 dbus_set_error (error, DBUS_ERROR_FAILED,
796 "Could not get UID and GID for username \"%s\"",
802 /* Now become a daemon if appropriate and write out pid file in any case */
806 if (context->pidfile)
807 _dbus_string_init_const (&u, context->pidfile);
809 if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
811 _dbus_verbose ("Forking and becoming daemon\n");
813 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
816 context->keep_umask))
818 _DBUS_ASSERT_ERROR_IS_SET (error);
824 _dbus_verbose ("Fork not requested\n");
826 /* Need to write PID file and to PID pipe for ourselves,
827 * not for the child process. This is a no-op if the pidfile
828 * is NULL and print_pid_pipe is NULL.
830 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
835 _DBUS_ASSERT_ERROR_IS_SET (error);
841 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
842 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
843 _dbus_pipe_close (print_pid_pipe, NULL);
845 if (!bus_selinux_full_init ())
847 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but AVC initialization failed; check system log\n");
850 if (!process_config_postinit (context, parser, error))
852 _DBUS_ASSERT_ERROR_IS_SET (error);
858 bus_config_parser_unref (parser);
862 /* Here we change our credentials if required,
863 * as soon as we've set up our sockets and pidfile
865 if (context->user != NULL)
867 if (!_dbus_change_to_daemon_user (context->user, error))
869 _DBUS_ASSERT_ERROR_IS_SET (error);
874 /* FIXME - why not just put this in full_init() below? */
875 bus_selinux_audit_init ();
879 dbus_server_free_data_slot (&server_data_slot);
885 bus_config_parser_unref (parser);
887 bus_context_unref (context);
889 if (server_data_slot >= 0)
890 dbus_server_free_data_slot (&server_data_slot);
896 bus_context_get_id (BusContext *context,
899 return _dbus_uuid_encode (&context->uuid, uuid);
903 bus_context_reload_config (BusContext *context,
906 BusConfigParser *parser;
907 DBusString config_file;
910 /* Flush the user database cache */
911 _dbus_flush_caches ();
914 _dbus_string_init_const (&config_file, context->config_file);
915 parser = bus_config_load (&config_file, TRUE, NULL, error);
918 _DBUS_ASSERT_ERROR_IS_SET (error);
922 if (!process_config_every_time (context, parser, TRUE, error))
924 _DBUS_ASSERT_ERROR_IS_SET (error);
927 if (!process_config_postinit (context, parser, error))
929 _DBUS_ASSERT_ERROR_IS_SET (error);
934 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
937 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
939 bus_config_parser_unref (parser);
944 shutdown_server (BusContext *context,
947 if (server == NULL ||
948 !dbus_server_get_is_connected (server))
951 if (!dbus_server_set_watch_functions (server,
955 _dbus_assert_not_reached ("setting watch functions to NULL failed");
957 if (!dbus_server_set_timeout_functions (server,
961 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
963 dbus_server_disconnect (server);
967 bus_context_shutdown (BusContext *context)
971 link = _dbus_list_get_first_link (&context->servers);
974 shutdown_server (context, link->data);
976 link = _dbus_list_get_next_link (&context->servers, link);
981 bus_context_ref (BusContext *context)
983 _dbus_assert (context->refcount > 0);
984 context->refcount += 1;
990 bus_context_unref (BusContext *context)
992 _dbus_assert (context->refcount > 0);
993 context->refcount -= 1;
995 if (context->refcount == 0)
999 _dbus_verbose ("Finalizing bus context %p\n", context);
1001 bus_context_shutdown (context);
1003 if (context->connections)
1005 bus_connections_unref (context->connections);
1006 context->connections = NULL;
1009 if (context->registry)
1011 bus_registry_unref (context->registry);
1012 context->registry = NULL;
1015 if (context->activation)
1017 bus_activation_unref (context->activation);
1018 context->activation = NULL;
1021 link = _dbus_list_get_first_link (&context->servers);
1022 while (link != NULL)
1024 dbus_server_unref (link->data);
1026 link = _dbus_list_get_next_link (&context->servers, link);
1028 _dbus_list_clear (&context->servers);
1030 if (context->policy)
1032 bus_policy_unref (context->policy);
1033 context->policy = NULL;
1038 _dbus_loop_unref (context->loop);
1039 context->loop = NULL;
1042 if (context->matchmaker)
1044 bus_matchmaker_unref (context->matchmaker);
1045 context->matchmaker = NULL;
1048 dbus_free (context->config_file);
1049 dbus_free (context->log_prefix);
1050 dbus_free (context->type);
1051 dbus_free (context->address);
1052 dbus_free (context->user);
1053 dbus_free (context->servicehelper);
1055 if (context->pidfile)
1058 _dbus_string_init_const (&u, context->pidfile);
1060 /* Deliberately ignore errors here, since there's not much
1061 * we can do about it, and we're exiting anyways.
1063 _dbus_delete_file (&u, NULL);
1065 dbus_free (context->pidfile);
1067 dbus_free (context);
1069 dbus_server_free_data_slot (&server_data_slot);
1073 /* type may be NULL */
1075 bus_context_get_type (BusContext *context)
1077 return context->type;
1081 bus_context_get_address (BusContext *context)
1083 return context->address;
1087 bus_context_get_servicehelper (BusContext *context)
1089 return context->servicehelper;
1093 bus_context_get_systemd_activation (BusContext *context)
1095 return context->systemd_activation;
1099 bus_context_get_registry (BusContext *context)
1101 return context->registry;
1105 bus_context_get_connections (BusContext *context)
1107 return context->connections;
1111 bus_context_get_activation (BusContext *context)
1113 return context->activation;
1117 bus_context_get_matchmaker (BusContext *context)
1119 return context->matchmaker;
1123 bus_context_get_loop (BusContext *context)
1125 return context->loop;
1129 bus_context_allow_unix_user (BusContext *context,
1132 return bus_policy_allow_unix_user (context->policy,
1136 /* For now this is never actually called because the default
1137 * DBusConnection behavior of 'same user that owns the bus can connect'
1138 * is all it would do.
1141 bus_context_allow_windows_user (BusContext *context,
1142 const char *windows_sid)
1144 return bus_policy_allow_windows_user (context->policy,
1149 bus_context_get_policy (BusContext *context)
1151 return context->policy;
1155 bus_context_create_client_policy (BusContext *context,
1156 DBusConnection *connection,
1159 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1160 return bus_policy_create_client_policy (context->policy, connection,
1165 bus_context_get_activation_timeout (BusContext *context)
1168 return context->limits.activation_timeout;
1172 bus_context_get_auth_timeout (BusContext *context)
1174 return context->limits.auth_timeout;
1178 bus_context_get_max_completed_connections (BusContext *context)
1180 return context->limits.max_completed_connections;
1184 bus_context_get_max_incomplete_connections (BusContext *context)
1186 return context->limits.max_incomplete_connections;
1190 bus_context_get_max_connections_per_user (BusContext *context)
1192 return context->limits.max_connections_per_user;
1196 bus_context_get_max_pending_activations (BusContext *context)
1198 return context->limits.max_pending_activations;
1202 bus_context_get_max_services_per_connection (BusContext *context)
1204 return context->limits.max_services_per_connection;
1208 bus_context_get_max_match_rules_per_connection (BusContext *context)
1210 return context->limits.max_match_rules_per_connection;
1214 bus_context_get_max_replies_per_connection (BusContext *context)
1216 return context->limits.max_replies_per_connection;
1220 bus_context_get_reply_timeout (BusContext *context)
1222 return context->limits.reply_timeout;
1226 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
1229 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1233 if (!context->syslog)
1236 va_start (args, msg);
1238 if (context->log_prefix)
1240 DBusString full_msg;
1242 if (!_dbus_string_init (&full_msg))
1244 if (!_dbus_string_append (&full_msg, context->log_prefix))
1246 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1249 _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1251 _dbus_string_free (&full_msg);
1254 _dbus_system_logv (severity, msg, args);
1261 * addressed_recipient is the recipient specified in the message.
1263 * proposed_recipient is the recipient we're considering sending
1264 * to right this second, and may be an eavesdropper.
1266 * sender is the sender of the message.
1268 * NULL for proposed_recipient or sender definitely means the bus driver.
1270 * NULL for addressed_recipient may mean the bus driver, or may mean
1271 * no destination was specified in the message (e.g. a signal).
1274 bus_context_check_security_policy (BusContext *context,
1275 BusTransaction *transaction,
1276 DBusConnection *sender,
1277 DBusConnection *addressed_recipient,
1278 DBusConnection *proposed_recipient,
1279 DBusMessage *message,
1283 BusClientPolicy *sender_policy;
1284 BusClientPolicy *recipient_policy;
1285 dbus_int32_t toggles;
1288 dbus_bool_t requested_reply;
1289 const char *sender_name;
1290 const char *sender_loginfo;
1291 const char *proposed_recipient_loginfo;
1293 type = dbus_message_get_type (message);
1294 dest = dbus_message_get_destination (message);
1296 /* dispatch.c was supposed to ensure these invariants */
1297 _dbus_assert (dest != NULL ||
1298 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1299 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1300 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1301 addressed_recipient != NULL ||
1302 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1304 /* Used in logging below */
1307 sender_name = bus_connection_get_name (sender);
1308 sender_loginfo = bus_connection_get_loginfo (sender);
1313 sender_loginfo = "(bus)";
1316 if (proposed_recipient != NULL)
1317 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1319 proposed_recipient_loginfo = "bus";
1323 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1324 case DBUS_MESSAGE_TYPE_SIGNAL:
1325 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1326 case DBUS_MESSAGE_TYPE_ERROR:
1330 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1333 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1334 "Message bus will not accept messages of unknown type\n");
1339 requested_reply = FALSE;
1343 /* First verify the SELinux access controls. If allowed then
1344 * go on with the standard checks.
1346 if (!bus_selinux_allows_send (sender, proposed_recipient,
1347 dbus_message_type_to_string (dbus_message_get_type (message)),
1348 dbus_message_get_interface (message),
1349 dbus_message_get_member (message),
1350 dbus_message_get_error_name (message),
1351 dest ? dest : DBUS_SERVICE_DBUS, error))
1353 if (error != NULL && !dbus_error_is_set (error))
1355 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1356 "An SELinux policy prevents this sender "
1357 "from sending this message to this recipient "
1358 "(rejected message had sender \"%s\" interface \"%s\" "
1359 "member \"%s\" error name \"%s\" destination \"%s\")",
1360 sender_name ? sender_name : "(unset)",
1361 dbus_message_get_interface (message) ?
1362 dbus_message_get_interface (message) : "(unset)",
1363 dbus_message_get_member (message) ?
1364 dbus_message_get_member (message) : "(unset)",
1365 dbus_message_get_error_name (message) ?
1366 dbus_message_get_error_name (message) : "(unset)",
1367 dest ? dest : DBUS_SERVICE_DBUS);
1368 _dbus_verbose ("SELinux security check denying send to service\n");
1374 if (bus_connection_is_active (sender))
1376 sender_policy = bus_connection_get_policy (sender);
1377 _dbus_assert (sender_policy != NULL);
1379 /* Fill in requested_reply variable with TRUE if this is a
1380 * reply and the reply was pending.
1382 if (dbus_message_get_reply_serial (message) != 0)
1384 if (proposed_recipient != NULL /* not to the bus driver */ &&
1385 addressed_recipient == proposed_recipient /* not eavesdropping */)
1389 dbus_error_init (&error2);
1390 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1392 sender, addressed_recipient, message,
1394 if (dbus_error_is_set (&error2))
1396 dbus_move_error (&error2, error);
1404 /* Policy for inactive connections is that they can only send
1405 * the hello message to the bus driver
1407 if (proposed_recipient == NULL &&
1408 dbus_message_is_method_call (message,
1409 DBUS_INTERFACE_DBUS,
1412 _dbus_verbose ("security check allowing %s message\n",
1418 _dbus_verbose ("security check disallowing non-%s message\n",
1421 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1422 "Client tried to send a message other than %s without being registered",
1431 sender_policy = NULL;
1433 /* If the sender is the bus driver, we assume any reply was a
1434 * requested reply as bus driver won't send bogus ones
1436 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1437 dbus_message_get_reply_serial (message) != 0)
1438 requested_reply = TRUE;
1441 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1442 (sender == NULL && sender_policy == NULL));
1444 if (proposed_recipient != NULL)
1446 /* only the bus driver can send to an inactive recipient (as it
1447 * owns no services, so other apps can't address it). Inactive
1448 * recipients can receive any message.
1450 if (bus_connection_is_active (proposed_recipient))
1452 recipient_policy = bus_connection_get_policy (proposed_recipient);
1453 _dbus_assert (recipient_policy != NULL);
1455 else if (sender == NULL)
1457 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1458 recipient_policy = NULL;
1462 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1463 recipient_policy = NULL;
1467 recipient_policy = NULL;
1469 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1470 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1471 (proposed_recipient == NULL && recipient_policy == NULL));
1474 if (sender_policy &&
1475 !bus_client_policy_check_can_send (sender_policy,
1479 message, &toggles, &log))
1481 const char *msg = "Rejected send message, %d matched rules; "
1482 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))";
1484 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1486 dbus_message_type_to_string (dbus_message_get_type (message)),
1487 sender_name ? sender_name : "(unset)",
1489 dbus_message_get_interface (message) ?
1490 dbus_message_get_interface (message) : "(unset)",
1491 dbus_message_get_member (message) ?
1492 dbus_message_get_member (message) : "(unset)",
1493 dbus_message_get_error_name (message) ?
1494 dbus_message_get_error_name (message) : "(unset)",
1496 dest ? dest : DBUS_SERVICE_DBUS,
1497 proposed_recipient_loginfo);
1498 /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1499 if (addressed_recipient == proposed_recipient)
1500 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, msg,
1502 dbus_message_type_to_string (dbus_message_get_type (message)),
1503 sender_name ? sender_name : "(unset)",
1505 dbus_message_get_interface (message) ?
1506 dbus_message_get_interface (message) : "(unset)",
1507 dbus_message_get_member (message) ?
1508 dbus_message_get_member (message) : "(unset)",
1509 dbus_message_get_error_name (message) ?
1510 dbus_message_get_error_name (message) : "(unset)",
1512 dest ? dest : DBUS_SERVICE_DBUS,
1513 proposed_recipient_loginfo);
1514 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1519 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY,
1520 "Would reject message, %d matched rules; "
1521 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))",
1523 dbus_message_type_to_string (dbus_message_get_type (message)),
1524 sender_name ? sender_name : "(unset)",
1526 dbus_message_get_interface (message) ?
1527 dbus_message_get_interface (message) : "(unset)",
1528 dbus_message_get_member (message) ?
1529 dbus_message_get_member (message) : "(unset)",
1530 dbus_message_get_error_name (message) ?
1531 dbus_message_get_error_name (message) : "(unset)",
1533 dest ? dest : DBUS_SERVICE_DBUS,
1534 proposed_recipient_loginfo);
1536 if (recipient_policy &&
1537 !bus_client_policy_check_can_receive (recipient_policy,
1541 addressed_recipient, proposed_recipient,
1544 const char *msg = "Rejected receive message, %d matched rules; "
1545 "type=\"%s\" sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" reply serial=%u requested_reply=%d destination=\"%s\" (%s))";
1547 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1549 dbus_message_type_to_string (dbus_message_get_type (message)),
1550 sender_name ? sender_name : "(unset)",
1552 dbus_message_get_interface (message) ?
1553 dbus_message_get_interface (message) : "(unset)",
1554 dbus_message_get_member (message) ?
1555 dbus_message_get_member (message) : "(unset)",
1556 dbus_message_get_error_name (message) ?
1557 dbus_message_get_error_name (message) : "(unset)",
1558 dbus_message_get_reply_serial (message),
1560 dest ? dest : DBUS_SERVICE_DBUS,
1561 proposed_recipient_loginfo);
1562 /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1563 if (addressed_recipient == proposed_recipient)
1564 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, msg,
1566 dbus_message_type_to_string (dbus_message_get_type (message)),
1567 sender_name ? sender_name : "(unset)",
1569 dbus_message_get_interface (message) ?
1570 dbus_message_get_interface (message) : "(unset)",
1571 dbus_message_get_member (message) ?
1572 dbus_message_get_member (message) : "(unset)",
1573 dbus_message_get_error_name (message) ?
1574 dbus_message_get_error_name (message) : "(unset)",
1575 dbus_message_get_reply_serial (message),
1577 dest ? dest : DBUS_SERVICE_DBUS,
1578 proposed_recipient_loginfo);
1579 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1583 /* See if limits on size have been exceeded */
1584 if (proposed_recipient &&
1585 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1586 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1588 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1589 "The destination service \"%s\" has a full message queue",
1590 dest ? dest : (proposed_recipient ?
1591 bus_connection_get_name (proposed_recipient) :
1592 DBUS_SERVICE_DBUS));
1593 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1597 /* Record that we will allow a reply here in the future (don't
1598 * bother if the recipient is the bus or this is an eavesdropping
1599 * connection). Only the addressed recipient may reply.
1601 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1603 addressed_recipient &&
1604 addressed_recipient == proposed_recipient && /* not eavesdropping */
1605 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1607 sender, addressed_recipient,
1610 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1614 _dbus_verbose ("security policy allowing message\n");