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;
65 static dbus_int32_t server_data_slot = -1;
72 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
75 server_get_context (DBusServer *server)
80 if (!dbus_server_allocate_data_slot (&server_data_slot))
83 bd = BUS_SERVER_DATA (server);
86 dbus_server_free_data_slot (&server_data_slot);
90 context = bd->context;
92 dbus_server_free_data_slot (&server_data_slot);
98 server_watch_callback (DBusWatch *watch,
99 unsigned int condition,
102 /* FIXME this can be done in dbus-mainloop.c
103 * if the code in activation.c for the babysitter
104 * watch handler is fixed.
107 return dbus_watch_handle (watch, condition);
111 add_server_watch (DBusWatch *watch,
114 DBusServer *server = data;
117 context = server_get_context (server);
119 return _dbus_loop_add_watch (context->loop,
120 watch, server_watch_callback, server,
125 remove_server_watch (DBusWatch *watch,
128 DBusServer *server = data;
131 context = server_get_context (server);
133 _dbus_loop_remove_watch (context->loop,
134 watch, server_watch_callback, server);
139 server_timeout_callback (DBusTimeout *timeout,
142 /* can return FALSE on OOM but we just let it fire again later */
143 dbus_timeout_handle (timeout);
147 add_server_timeout (DBusTimeout *timeout,
150 DBusServer *server = data;
153 context = server_get_context (server);
155 return _dbus_loop_add_timeout (context->loop,
156 timeout, server_timeout_callback, server, NULL);
160 remove_server_timeout (DBusTimeout *timeout,
163 DBusServer *server = data;
166 context = server_get_context (server);
168 _dbus_loop_remove_timeout (context->loop,
169 timeout, server_timeout_callback, server);
173 new_connection_callback (DBusServer *server,
174 DBusConnection *new_connection,
177 BusContext *context = data;
179 if (!bus_connections_setup_connection (context->connections, new_connection))
181 _dbus_verbose ("No memory to setup new connection\n");
183 /* if we don't do this, it will get unref'd without
184 * being disconnected... kind of strange really
185 * that we have to do this, people won't get it right
188 dbus_connection_close (new_connection);
191 dbus_connection_set_max_received_size (new_connection,
192 context->limits.max_incoming_bytes);
194 dbus_connection_set_max_message_size (new_connection,
195 context->limits.max_message_size);
197 dbus_connection_set_max_received_unix_fds (new_connection,
198 context->limits.max_incoming_unix_fds);
200 dbus_connection_set_max_message_unix_fds (new_connection,
201 context->limits.max_message_unix_fds);
203 dbus_connection_set_allow_anonymous (new_connection,
204 context->allow_anonymous);
206 /* on OOM, we won't have ref'd the connection so it will die. */
210 free_server_data (void *data)
212 BusServerData *bd = data;
218 setup_server (BusContext *context,
220 char **auth_mechanisms,
225 bd = dbus_new0 (BusServerData, 1);
226 if (bd == NULL || !dbus_server_set_data (server,
228 bd, free_server_data))
235 bd->context = context;
237 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
243 dbus_server_set_new_connection_function (server,
244 new_connection_callback,
247 if (!dbus_server_set_watch_functions (server,
258 if (!dbus_server_set_timeout_functions (server,
260 remove_server_timeout,
271 /* This code only gets executed the first time the
272 * config files are parsed. It is not executed
273 * when config files are reloaded.
276 process_config_first_time_only (BusContext *context,
277 BusConfigParser *parser,
278 const DBusString *address,
281 DBusString log_prefix;
283 DBusList **addresses;
284 const char *user, *pidfile;
285 char **auth_mechanisms;
286 DBusList **auth_mechanisms_list;
290 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
293 auth_mechanisms = NULL;
295 /* Check for an existing pid file. Of course this is a race;
296 * we'd have to use fcntl() locks on the pid file to
297 * avoid that. But we want to check for the pid file
298 * before overwriting any existing sockets, etc.
300 pidfile = bus_config_parser_get_pidfile (parser);
306 _dbus_string_init_const (&u, pidfile);
308 if (_dbus_stat (&u, &stbuf, NULL))
310 dbus_set_error (error, DBUS_ERROR_FAILED,
311 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
317 /* keep around the pid filename so we can delete it later */
318 context->pidfile = _dbus_strdup (pidfile);
320 /* note that type may be NULL */
321 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
322 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
325 user = bus_config_parser_get_user (parser);
328 context->user = _dbus_strdup (user);
329 if (context->user == NULL)
333 /* Set up the prefix for syslog messages */
334 if (!_dbus_string_init (&log_prefix))
336 if (context->type && !strcmp (context->type, "system"))
338 if (!_dbus_string_append (&log_prefix, "[system] "))
341 else if (context->type && !strcmp (context->type, "session"))
343 DBusCredentials *credentials;
345 credentials = _dbus_credentials_new_from_current_process ();
348 if (!_dbus_string_append (&log_prefix, "[session "))
350 if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
352 if (!_dbus_string_append (&log_prefix, "] "))
354 _dbus_credentials_unref (credentials);
356 if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
358 _dbus_string_free (&log_prefix);
360 /* Build an array of auth mechanisms */
362 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
363 len = _dbus_list_get_length (auth_mechanisms_list);
369 auth_mechanisms = dbus_new0 (char*, len + 1);
370 if (auth_mechanisms == NULL)
374 link = _dbus_list_get_first_link (auth_mechanisms_list);
377 auth_mechanisms[i] = _dbus_strdup (link->data);
378 if (auth_mechanisms[i] == NULL)
380 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
385 auth_mechanisms = NULL;
388 /* Listen on our addresses */
394 server = dbus_server_listen (_dbus_string_get_const_data(address), error);
397 _DBUS_ASSERT_ERROR_IS_SET (error);
400 else if (!setup_server (context, server, auth_mechanisms, error))
402 _DBUS_ASSERT_ERROR_IS_SET (error);
406 if (!_dbus_list_append (&context->servers, server))
411 addresses = bus_config_parser_get_addresses (parser);
413 link = _dbus_list_get_first_link (addresses);
418 server = dbus_server_listen (link->data, error);
421 _DBUS_ASSERT_ERROR_IS_SET (error);
424 else if (!setup_server (context, server, auth_mechanisms, error))
426 _DBUS_ASSERT_ERROR_IS_SET (error);
430 if (!_dbus_list_append (&context->servers, server))
433 link = _dbus_list_get_next_link (addresses, link);
437 context->fork = bus_config_parser_get_fork (parser);
438 context->syslog = bus_config_parser_get_syslog (parser);
439 context->keep_umask = bus_config_parser_get_keep_umask (parser);
440 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
442 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
446 dbus_free_string_array (auth_mechanisms);
451 dbus_free_string_array (auth_mechanisms);
455 /* This code gets executed every time the config files
456 * are parsed: both during BusContext construction
457 * and on reloads. This function is slightly screwy
458 * since it can do a "half reload" in out-of-memory
459 * situations. Realistically, unlikely to ever matter.
462 process_config_every_time (BusContext *context,
463 BusConfigParser *parser,
464 dbus_bool_t is_reload,
467 DBusString full_address;
470 BusActivation *new_activation;
472 const char *servicehelper;
477 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
482 if (!_dbus_string_init (&full_address))
488 /* get our limits and timeout lengths */
489 bus_config_parser_get_limits (parser, &context->limits);
492 bus_policy_unref (context->policy);
493 context->policy = bus_config_parser_steal_policy (parser);
494 _dbus_assert (context->policy != NULL);
496 /* We have to build the address backward, so that
497 * <listen> later in the config file have priority
499 link = _dbus_list_get_last_link (&context->servers);
502 addr = dbus_server_get_address (link->data);
509 if (_dbus_string_get_length (&full_address) > 0)
511 if (!_dbus_string_append (&full_address, ";"))
518 if (!_dbus_string_append (&full_address, addr))
527 link = _dbus_list_get_prev_link (&context->servers, link);
531 dbus_free (context->address);
533 if (!_dbus_string_copy_data (&full_address, &context->address))
539 /* get the service directories */
540 dirs = bus_config_parser_get_service_dirs (parser);
542 /* and the service helper */
543 servicehelper = bus_config_parser_get_servicehelper (parser);
545 s = _dbus_strdup(servicehelper);
546 if (s == NULL && servicehelper != NULL)
553 dbus_free(context->servicehelper);
554 context->servicehelper = s;
557 /* Create activation subsystem */
558 if (context->activation)
560 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
565 context->activation = bus_activation_new (context, &full_address, dirs, error);
568 if (context->activation == NULL)
570 _DBUS_ASSERT_ERROR_IS_SET (error);
574 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
578 _dbus_string_free (&full_address);
587 list_concat_new (DBusList **a,
595 link = _dbus_list_get_first_link (a);
596 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
598 if (!_dbus_list_append (result, link->data))
601 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
603 if (!_dbus_list_append (result, link->data))
609 _dbus_list_clear (result);
614 process_config_postinit (BusContext *context,
615 BusConfigParser *parser,
618 DBusHashTable *service_context_table;
619 DBusList *watched_dirs = NULL;
621 service_context_table = bus_config_parser_steal_service_context_table (parser);
622 if (!bus_registry_set_service_context_table (context->registry,
623 service_context_table))
629 _dbus_hash_table_unref (service_context_table);
631 /* We need to monitor both the configuration directories and directories
632 * containing .service files.
634 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
635 bus_config_parser_get_service_dirs (parser),
642 bus_set_watched_dirs (context, &watched_dirs);
644 _dbus_list_clear (&watched_dirs);
650 bus_context_new (const DBusString *config_file,
651 ForceForkSetting force_fork,
652 DBusPipe *print_addr_pipe,
653 DBusPipe *print_pid_pipe,
654 const DBusString *address,
657 DBusString log_prefix;
659 BusConfigParser *parser;
661 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
666 if (!dbus_server_allocate_data_slot (&server_data_slot))
672 context = dbus_new0 (BusContext, 1);
678 context->refcount = 1;
680 _dbus_generate_uuid (&context->uuid);
682 if (!_dbus_string_copy_data (config_file, &context->config_file))
688 context->loop = _dbus_loop_new ();
689 if (context->loop == NULL)
695 context->registry = bus_registry_new (context);
696 if (context->registry == NULL)
702 parser = bus_config_load (config_file, TRUE, NULL, error);
705 _DBUS_ASSERT_ERROR_IS_SET (error);
709 if (!process_config_first_time_only (context, parser, address, error))
711 _DBUS_ASSERT_ERROR_IS_SET (error);
714 if (!process_config_every_time (context, parser, FALSE, error))
716 _DBUS_ASSERT_ERROR_IS_SET (error);
720 /* we need another ref of the server data slot for the context
723 if (!dbus_server_allocate_data_slot (&server_data_slot))
724 _dbus_assert_not_reached ("second ref of server data slot failed");
726 /* Note that we don't know whether the print_addr_pipe is
727 * one of the sockets we're using to listen on, or some
728 * other random thing. But I think the answer is "don't do
731 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
734 const char *a = bus_context_get_address (context);
737 _dbus_assert (a != NULL);
738 if (!_dbus_string_init (&addr))
744 if (!_dbus_string_append (&addr, a) ||
745 !_dbus_string_append (&addr, "\n"))
747 _dbus_string_free (&addr);
752 bytes = _dbus_string_get_length (&addr);
753 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
755 /* pipe write returns an error on failure but not short write */
756 if (error != NULL && !dbus_error_is_set (error))
758 dbus_set_error (error, DBUS_ERROR_FAILED,
759 "Printing message bus address: did not write all bytes\n");
761 _dbus_string_free (&addr);
765 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
766 _dbus_pipe_close (print_addr_pipe, NULL);
768 _dbus_string_free (&addr);
771 context->connections = bus_connections_new (context);
772 if (context->connections == NULL)
778 context->matchmaker = bus_matchmaker_new ();
779 if (context->matchmaker == NULL)
785 /* check user before we fork */
786 if (context->user != NULL)
788 if (!_dbus_verify_daemon_user (context->user))
790 dbus_set_error (error, DBUS_ERROR_FAILED,
791 "Could not get UID and GID for username \"%s\"",
797 /* Now become a daemon if appropriate and write out pid file in any case */
801 if (context->pidfile)
802 _dbus_string_init_const (&u, context->pidfile);
804 if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
806 _dbus_verbose ("Forking and becoming daemon\n");
808 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
811 context->keep_umask))
813 _DBUS_ASSERT_ERROR_IS_SET (error);
819 _dbus_verbose ("Fork not requested\n");
821 /* Need to write PID file and to PID pipe for ourselves,
822 * not for the child process. This is a no-op if the pidfile
823 * is NULL and print_pid_pipe is NULL.
825 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
830 _DBUS_ASSERT_ERROR_IS_SET (error);
836 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
837 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
838 _dbus_pipe_close (print_pid_pipe, NULL);
840 if (!bus_selinux_full_init ())
842 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but AVC initialization failed; check system log\n");
845 if (!process_config_postinit (context, parser, error))
847 _DBUS_ASSERT_ERROR_IS_SET (error);
853 bus_config_parser_unref (parser);
857 /* Here we change our credentials if required,
858 * as soon as we've set up our sockets and pidfile
860 if (context->user != NULL)
862 if (!_dbus_change_to_daemon_user (context->user, error))
864 _DBUS_ASSERT_ERROR_IS_SET (error);
869 /* FIXME - why not just put this in full_init() below? */
870 bus_selinux_audit_init ();
874 dbus_server_free_data_slot (&server_data_slot);
880 bus_config_parser_unref (parser);
882 bus_context_unref (context);
884 if (server_data_slot >= 0)
885 dbus_server_free_data_slot (&server_data_slot);
891 bus_context_get_id (BusContext *context,
894 return _dbus_uuid_encode (&context->uuid, uuid);
898 bus_context_reload_config (BusContext *context,
901 BusConfigParser *parser;
902 DBusString config_file;
905 /* Flush the user database cache */
906 _dbus_flush_caches ();
909 _dbus_string_init_const (&config_file, context->config_file);
910 parser = bus_config_load (&config_file, TRUE, NULL, error);
913 _DBUS_ASSERT_ERROR_IS_SET (error);
917 if (!process_config_every_time (context, parser, TRUE, error))
919 _DBUS_ASSERT_ERROR_IS_SET (error);
922 if (!process_config_postinit (context, parser, error))
924 _DBUS_ASSERT_ERROR_IS_SET (error);
929 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
932 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
934 bus_config_parser_unref (parser);
939 shutdown_server (BusContext *context,
942 if (server == NULL ||
943 !dbus_server_get_is_connected (server))
946 if (!dbus_server_set_watch_functions (server,
950 _dbus_assert_not_reached ("setting watch functions to NULL failed");
952 if (!dbus_server_set_timeout_functions (server,
956 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
958 dbus_server_disconnect (server);
962 bus_context_shutdown (BusContext *context)
966 link = _dbus_list_get_first_link (&context->servers);
969 shutdown_server (context, link->data);
971 link = _dbus_list_get_next_link (&context->servers, link);
976 bus_context_ref (BusContext *context)
978 _dbus_assert (context->refcount > 0);
979 context->refcount += 1;
985 bus_context_unref (BusContext *context)
987 _dbus_assert (context->refcount > 0);
988 context->refcount -= 1;
990 if (context->refcount == 0)
994 _dbus_verbose ("Finalizing bus context %p\n", context);
996 bus_context_shutdown (context);
998 if (context->connections)
1000 bus_connections_unref (context->connections);
1001 context->connections = NULL;
1004 if (context->registry)
1006 bus_registry_unref (context->registry);
1007 context->registry = NULL;
1010 if (context->activation)
1012 bus_activation_unref (context->activation);
1013 context->activation = NULL;
1016 link = _dbus_list_get_first_link (&context->servers);
1017 while (link != NULL)
1019 dbus_server_unref (link->data);
1021 link = _dbus_list_get_next_link (&context->servers, link);
1023 _dbus_list_clear (&context->servers);
1025 if (context->policy)
1027 bus_policy_unref (context->policy);
1028 context->policy = NULL;
1033 _dbus_loop_unref (context->loop);
1034 context->loop = NULL;
1037 if (context->matchmaker)
1039 bus_matchmaker_unref (context->matchmaker);
1040 context->matchmaker = NULL;
1043 dbus_free (context->config_file);
1044 dbus_free (context->log_prefix);
1045 dbus_free (context->type);
1046 dbus_free (context->address);
1047 dbus_free (context->user);
1048 dbus_free (context->servicehelper);
1050 if (context->pidfile)
1053 _dbus_string_init_const (&u, context->pidfile);
1055 /* Deliberately ignore errors here, since there's not much
1056 * we can do about it, and we're exiting anyways.
1058 _dbus_delete_file (&u, NULL);
1060 dbus_free (context->pidfile);
1062 dbus_free (context);
1064 dbus_server_free_data_slot (&server_data_slot);
1068 /* type may be NULL */
1070 bus_context_get_type (BusContext *context)
1072 return context->type;
1076 bus_context_get_address (BusContext *context)
1078 return context->address;
1082 bus_context_get_servicehelper (BusContext *context)
1084 return context->servicehelper;
1088 bus_context_get_registry (BusContext *context)
1090 return context->registry;
1094 bus_context_get_connections (BusContext *context)
1096 return context->connections;
1100 bus_context_get_activation (BusContext *context)
1102 return context->activation;
1106 bus_context_get_matchmaker (BusContext *context)
1108 return context->matchmaker;
1112 bus_context_get_loop (BusContext *context)
1114 return context->loop;
1118 bus_context_allow_unix_user (BusContext *context,
1121 return bus_policy_allow_unix_user (context->policy,
1125 /* For now this is never actually called because the default
1126 * DBusConnection behavior of 'same user that owns the bus can connect'
1127 * is all it would do.
1130 bus_context_allow_windows_user (BusContext *context,
1131 const char *windows_sid)
1133 return bus_policy_allow_windows_user (context->policy,
1138 bus_context_get_policy (BusContext *context)
1140 return context->policy;
1144 bus_context_create_client_policy (BusContext *context,
1145 DBusConnection *connection,
1148 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1149 return bus_policy_create_client_policy (context->policy, connection,
1154 bus_context_get_activation_timeout (BusContext *context)
1157 return context->limits.activation_timeout;
1161 bus_context_get_auth_timeout (BusContext *context)
1163 return context->limits.auth_timeout;
1167 bus_context_get_max_completed_connections (BusContext *context)
1169 return context->limits.max_completed_connections;
1173 bus_context_get_max_incomplete_connections (BusContext *context)
1175 return context->limits.max_incomplete_connections;
1179 bus_context_get_max_connections_per_user (BusContext *context)
1181 return context->limits.max_connections_per_user;
1185 bus_context_get_max_pending_activations (BusContext *context)
1187 return context->limits.max_pending_activations;
1191 bus_context_get_max_services_per_connection (BusContext *context)
1193 return context->limits.max_services_per_connection;
1197 bus_context_get_max_match_rules_per_connection (BusContext *context)
1199 return context->limits.max_match_rules_per_connection;
1203 bus_context_get_max_replies_per_connection (BusContext *context)
1205 return context->limits.max_replies_per_connection;
1209 bus_context_get_reply_timeout (BusContext *context)
1211 return context->limits.reply_timeout;
1215 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
1218 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1222 if (!context->syslog)
1225 va_start (args, msg);
1227 if (context->log_prefix)
1229 DBusString full_msg;
1231 if (!_dbus_string_init (&full_msg))
1233 if (!_dbus_string_append (&full_msg, context->log_prefix))
1235 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1238 _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1240 _dbus_string_free (&full_msg);
1243 _dbus_system_logv (severity, msg, args);
1250 * addressed_recipient is the recipient specified in the message.
1252 * proposed_recipient is the recipient we're considering sending
1253 * to right this second, and may be an eavesdropper.
1255 * sender is the sender of the message.
1257 * NULL for proposed_recipient or sender definitely means the bus driver.
1259 * NULL for addressed_recipient may mean the bus driver, or may mean
1260 * no destination was specified in the message (e.g. a signal).
1263 bus_context_check_security_policy (BusContext *context,
1264 BusTransaction *transaction,
1265 DBusConnection *sender,
1266 DBusConnection *addressed_recipient,
1267 DBusConnection *proposed_recipient,
1268 DBusMessage *message,
1272 BusClientPolicy *sender_policy;
1273 BusClientPolicy *recipient_policy;
1274 dbus_int32_t toggles;
1277 dbus_bool_t requested_reply;
1278 const char *sender_name;
1279 const char *sender_loginfo;
1280 const char *proposed_recipient_loginfo;
1282 type = dbus_message_get_type (message);
1283 dest = dbus_message_get_destination (message);
1285 /* dispatch.c was supposed to ensure these invariants */
1286 _dbus_assert (dest != NULL ||
1287 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1288 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1289 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1290 addressed_recipient != NULL ||
1291 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1293 /* Used in logging below */
1296 sender_name = bus_connection_get_name (sender);
1297 sender_loginfo = bus_connection_get_loginfo (sender);
1302 sender_loginfo = "(bus)";
1305 if (proposed_recipient != NULL)
1306 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1308 proposed_recipient_loginfo = "bus";
1312 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1313 case DBUS_MESSAGE_TYPE_SIGNAL:
1314 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1315 case DBUS_MESSAGE_TYPE_ERROR:
1319 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1322 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1323 "Message bus will not accept messages of unknown type\n");
1328 requested_reply = FALSE;
1332 /* First verify the SELinux access controls. If allowed then
1333 * go on with the standard checks.
1335 if (!bus_selinux_allows_send (sender, proposed_recipient,
1336 dbus_message_type_to_string (dbus_message_get_type (message)),
1337 dbus_message_get_interface (message),
1338 dbus_message_get_member (message),
1339 dbus_message_get_error_name (message),
1340 dest ? dest : DBUS_SERVICE_DBUS, error))
1342 if (error != NULL && !dbus_error_is_set (error))
1344 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1345 "An SELinux policy prevents this sender "
1346 "from sending this message to this recipient "
1347 "(rejected message had sender \"%s\" interface \"%s\" "
1348 "member \"%s\" error name \"%s\" destination \"%s\")",
1349 sender_name ? sender_name : "(unset)",
1350 dbus_message_get_interface (message) ?
1351 dbus_message_get_interface (message) : "(unset)",
1352 dbus_message_get_member (message) ?
1353 dbus_message_get_member (message) : "(unset)",
1354 dbus_message_get_error_name (message) ?
1355 dbus_message_get_error_name (message) : "(unset)",
1356 dest ? dest : DBUS_SERVICE_DBUS);
1357 _dbus_verbose ("SELinux security check denying send to service\n");
1363 if (bus_connection_is_active (sender))
1365 sender_policy = bus_connection_get_policy (sender);
1366 _dbus_assert (sender_policy != NULL);
1368 /* Fill in requested_reply variable with TRUE if this is a
1369 * reply and the reply was pending.
1371 if (dbus_message_get_reply_serial (message) != 0)
1373 if (proposed_recipient != NULL /* not to the bus driver */ &&
1374 addressed_recipient == proposed_recipient /* not eavesdropping */)
1378 dbus_error_init (&error2);
1379 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1381 sender, addressed_recipient, message,
1383 if (dbus_error_is_set (&error2))
1385 dbus_move_error (&error2, error);
1393 /* Policy for inactive connections is that they can only send
1394 * the hello message to the bus driver
1396 if (proposed_recipient == NULL &&
1397 dbus_message_is_method_call (message,
1398 DBUS_INTERFACE_DBUS,
1401 _dbus_verbose ("security check allowing %s message\n",
1407 _dbus_verbose ("security check disallowing non-%s message\n",
1410 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1411 "Client tried to send a message other than %s without being registered",
1420 sender_policy = NULL;
1422 /* If the sender is the bus driver, we assume any reply was a
1423 * requested reply as bus driver won't send bogus ones
1425 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1426 dbus_message_get_reply_serial (message) != 0)
1427 requested_reply = TRUE;
1430 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1431 (sender == NULL && sender_policy == NULL));
1433 if (proposed_recipient != NULL)
1435 /* only the bus driver can send to an inactive recipient (as it
1436 * owns no services, so other apps can't address it). Inactive
1437 * recipients can receive any message.
1439 if (bus_connection_is_active (proposed_recipient))
1441 recipient_policy = bus_connection_get_policy (proposed_recipient);
1442 _dbus_assert (recipient_policy != NULL);
1444 else if (sender == NULL)
1446 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1447 recipient_policy = NULL;
1451 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1452 recipient_policy = NULL;
1456 recipient_policy = NULL;
1458 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1459 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1460 (proposed_recipient == NULL && recipient_policy == NULL));
1463 if (sender_policy &&
1464 !bus_client_policy_check_can_send (sender_policy,
1468 message, &toggles, &log))
1470 const char *msg = "Rejected send message, %d matched rules; "
1471 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))";
1473 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1475 dbus_message_type_to_string (dbus_message_get_type (message)),
1476 sender_name ? sender_name : "(unset)",
1478 dbus_message_get_interface (message) ?
1479 dbus_message_get_interface (message) : "(unset)",
1480 dbus_message_get_member (message) ?
1481 dbus_message_get_member (message) : "(unset)",
1482 dbus_message_get_error_name (message) ?
1483 dbus_message_get_error_name (message) : "(unset)",
1485 dest ? dest : DBUS_SERVICE_DBUS,
1486 proposed_recipient_loginfo);
1487 /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1488 if (addressed_recipient == proposed_recipient)
1489 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, msg,
1491 dbus_message_type_to_string (dbus_message_get_type (message)),
1492 sender_name ? sender_name : "(unset)",
1494 dbus_message_get_interface (message) ?
1495 dbus_message_get_interface (message) : "(unset)",
1496 dbus_message_get_member (message) ?
1497 dbus_message_get_member (message) : "(unset)",
1498 dbus_message_get_error_name (message) ?
1499 dbus_message_get_error_name (message) : "(unset)",
1501 dest ? dest : DBUS_SERVICE_DBUS,
1502 proposed_recipient_loginfo);
1503 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1508 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY,
1509 "Would reject message, %d matched rules; "
1510 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))",
1512 dbus_message_type_to_string (dbus_message_get_type (message)),
1513 sender_name ? sender_name : "(unset)",
1515 dbus_message_get_interface (message) ?
1516 dbus_message_get_interface (message) : "(unset)",
1517 dbus_message_get_member (message) ?
1518 dbus_message_get_member (message) : "(unset)",
1519 dbus_message_get_error_name (message) ?
1520 dbus_message_get_error_name (message) : "(unset)",
1522 dest ? dest : DBUS_SERVICE_DBUS,
1523 proposed_recipient_loginfo);
1525 if (recipient_policy &&
1526 !bus_client_policy_check_can_receive (recipient_policy,
1530 addressed_recipient, proposed_recipient,
1533 const char *msg = "Rejected receive message, %d matched rules; "
1534 "type=\"%s\" sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" reply serial=%u requested_reply=%d destination=\"%s\" (%s))";
1536 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1538 dbus_message_type_to_string (dbus_message_get_type (message)),
1539 sender_name ? sender_name : "(unset)",
1541 dbus_message_get_interface (message) ?
1542 dbus_message_get_interface (message) : "(unset)",
1543 dbus_message_get_member (message) ?
1544 dbus_message_get_member (message) : "(unset)",
1545 dbus_message_get_error_name (message) ?
1546 dbus_message_get_error_name (message) : "(unset)",
1547 dbus_message_get_reply_serial (message),
1549 dest ? dest : DBUS_SERVICE_DBUS,
1550 proposed_recipient_loginfo);
1551 /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1552 if (addressed_recipient == proposed_recipient)
1553 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, msg,
1555 dbus_message_type_to_string (dbus_message_get_type (message)),
1556 sender_name ? sender_name : "(unset)",
1558 dbus_message_get_interface (message) ?
1559 dbus_message_get_interface (message) : "(unset)",
1560 dbus_message_get_member (message) ?
1561 dbus_message_get_member (message) : "(unset)",
1562 dbus_message_get_error_name (message) ?
1563 dbus_message_get_error_name (message) : "(unset)",
1564 dbus_message_get_reply_serial (message),
1566 dest ? dest : DBUS_SERVICE_DBUS,
1567 proposed_recipient_loginfo);
1568 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1572 /* See if limits on size have been exceeded */
1573 if (proposed_recipient &&
1574 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1575 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1577 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1578 "The destination service \"%s\" has a full message queue",
1579 dest ? dest : (proposed_recipient ?
1580 bus_connection_get_name (proposed_recipient) :
1581 DBUS_SERVICE_DBUS));
1582 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1586 /* Record that we will allow a reply here in the future (don't
1587 * bother if the recipient is the bus or this is an eavesdropping
1588 * connection). Only the addressed recipient may reply.
1590 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1592 addressed_recipient &&
1593 addressed_recipient == proposed_recipient && /* not eavesdropping */
1594 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1596 sender, addressed_recipient,
1599 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1603 _dbus_verbose ("security policy allowing message\n");