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
25 #include "activation.h"
26 #include "connection.h"
30 #include "config-parser.h"
33 #include "dir-watch.h"
34 #include <dbus/dbus-list.h>
35 #include <dbus/dbus-hash.h>
36 #include <dbus/dbus-internals.h>
50 BusConnections *connections;
51 BusActivation *activation;
52 BusRegistry *registry;
54 BusMatchmaker *matchmaker;
56 unsigned int fork : 1;
57 unsigned int syslog : 1;
58 unsigned int keep_umask : 1;
61 static dbus_int32_t server_data_slot = -1;
68 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
71 server_get_context (DBusServer *server)
76 if (!dbus_server_allocate_data_slot (&server_data_slot))
79 bd = BUS_SERVER_DATA (server);
82 dbus_server_free_data_slot (&server_data_slot);
86 context = bd->context;
88 dbus_server_free_data_slot (&server_data_slot);
94 server_watch_callback (DBusWatch *watch,
95 unsigned int condition,
98 /* FIXME this can be done in dbus-mainloop.c
99 * if the code in activation.c for the babysitter
100 * watch handler is fixed.
103 return dbus_watch_handle (watch, condition);
107 add_server_watch (DBusWatch *watch,
110 DBusServer *server = data;
113 context = server_get_context (server);
115 return _dbus_loop_add_watch (context->loop,
116 watch, server_watch_callback, server,
121 remove_server_watch (DBusWatch *watch,
124 DBusServer *server = data;
127 context = server_get_context (server);
129 _dbus_loop_remove_watch (context->loop,
130 watch, server_watch_callback, server);
135 server_timeout_callback (DBusTimeout *timeout,
138 /* can return FALSE on OOM but we just let it fire again later */
139 dbus_timeout_handle (timeout);
143 add_server_timeout (DBusTimeout *timeout,
146 DBusServer *server = data;
149 context = server_get_context (server);
151 return _dbus_loop_add_timeout (context->loop,
152 timeout, server_timeout_callback, server, NULL);
156 remove_server_timeout (DBusTimeout *timeout,
159 DBusServer *server = data;
162 context = server_get_context (server);
164 _dbus_loop_remove_timeout (context->loop,
165 timeout, server_timeout_callback, server);
169 new_connection_callback (DBusServer *server,
170 DBusConnection *new_connection,
173 BusContext *context = data;
175 if (!bus_connections_setup_connection (context->connections, new_connection))
177 _dbus_verbose ("No memory to setup new connection\n");
179 /* if we don't do this, it will get unref'd without
180 * being disconnected... kind of strange really
181 * that we have to do this, people won't get it right
184 dbus_connection_close (new_connection);
187 dbus_connection_set_max_received_size (new_connection,
188 context->limits.max_incoming_bytes);
190 dbus_connection_set_max_message_size (new_connection,
191 context->limits.max_message_size);
193 /* on OOM, we won't have ref'd the connection so it will die. */
197 free_server_data (void *data)
199 BusServerData *bd = data;
205 setup_server (BusContext *context,
207 char **auth_mechanisms,
212 bd = dbus_new0 (BusServerData, 1);
213 if (bd == NULL || !dbus_server_set_data (server,
215 bd, free_server_data))
222 bd->context = context;
224 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
230 dbus_server_set_new_connection_function (server,
231 new_connection_callback,
234 if (!dbus_server_set_watch_functions (server,
245 if (!dbus_server_set_timeout_functions (server,
247 remove_server_timeout,
258 /* This code only gets executed the first time the
259 * config files are parsed. It is not executed
260 * when config files are reloaded.
263 process_config_first_time_only (BusContext *context,
264 BusConfigParser *parser,
268 DBusList **addresses;
269 const char *user, *pidfile;
270 char **auth_mechanisms;
271 DBusList **auth_mechanisms_list;
275 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
278 auth_mechanisms = NULL;
280 /* Check for an existing pid file. Of course this is a race;
281 * we'd have to use fcntl() locks on the pid file to
282 * avoid that. But we want to check for the pid file
283 * before overwriting any existing sockets, etc.
285 pidfile = bus_config_parser_get_pidfile (parser);
291 _dbus_string_init_const (&u, pidfile);
293 if (_dbus_stat (&u, &stbuf, NULL))
295 dbus_set_error (error, DBUS_ERROR_FAILED,
296 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
302 /* keep around the pid filename so we can delete it later */
303 context->pidfile = _dbus_strdup (pidfile);
305 /* Build an array of auth mechanisms */
307 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
308 len = _dbus_list_get_length (auth_mechanisms_list);
314 auth_mechanisms = dbus_new0 (char*, len + 1);
315 if (auth_mechanisms == NULL)
322 link = _dbus_list_get_first_link (auth_mechanisms_list);
325 auth_mechanisms[i] = _dbus_strdup (link->data);
326 if (auth_mechanisms[i] == NULL)
331 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
336 auth_mechanisms = NULL;
339 /* Listen on our addresses */
341 addresses = bus_config_parser_get_addresses (parser);
343 link = _dbus_list_get_first_link (addresses);
348 server = dbus_server_listen (link->data, error);
351 _DBUS_ASSERT_ERROR_IS_SET (error);
354 else if (!setup_server (context, server, auth_mechanisms, error))
356 _DBUS_ASSERT_ERROR_IS_SET (error);
360 if (!_dbus_list_append (&context->servers, server))
366 link = _dbus_list_get_next_link (addresses, link);
369 /* note that type may be NULL */
370 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
371 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
377 user = bus_config_parser_get_user (parser);
380 context->user = _dbus_strdup (user);
381 if (context->user == NULL)
388 context->fork = bus_config_parser_get_fork (parser);
389 context->syslog = bus_config_parser_get_syslog (parser);
390 context->keep_umask = bus_config_parser_get_keep_umask (parser);
392 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
396 dbus_free_string_array (auth_mechanisms);
400 /* This code gets executed every time the config files
401 * are parsed: both during BusContext construction
402 * and on reloads. This function is slightly screwy
403 * since it can do a "half reload" in out-of-memory
404 * situations. Realistically, unlikely to ever matter.
407 process_config_every_time (BusContext *context,
408 BusConfigParser *parser,
409 dbus_bool_t is_reload,
412 DBusString full_address;
415 BusActivation *new_activation;
417 const char *servicehelper;
422 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
427 if (!_dbus_string_init (&full_address))
433 /* get our limits and timeout lengths */
434 bus_config_parser_get_limits (parser, &context->limits);
437 bus_policy_unref (context->policy);
438 context->policy = bus_config_parser_steal_policy (parser);
439 _dbus_assert (context->policy != NULL);
441 /* We have to build the address backward, so that
442 * <listen> later in the config file have priority
444 link = _dbus_list_get_last_link (&context->servers);
447 addr = dbus_server_get_address (link->data);
454 if (_dbus_string_get_length (&full_address) > 0)
456 if (!_dbus_string_append (&full_address, ";"))
463 if (!_dbus_string_append (&full_address, addr))
472 link = _dbus_list_get_prev_link (&context->servers, link);
476 dbus_free (context->address);
478 if (!_dbus_string_copy_data (&full_address, &context->address))
484 /* get the service directories */
485 dirs = bus_config_parser_get_service_dirs (parser);
487 /* and the service helper */
488 servicehelper = bus_config_parser_get_servicehelper (parser);
490 s = _dbus_strdup(servicehelper);
491 if (s == NULL && servicehelper != NULL)
498 dbus_free(context->servicehelper);
499 context->servicehelper = s;
502 /* Create activation subsystem */
503 if (context->activation)
505 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
510 context->activation = bus_activation_new (context, &full_address, dirs, error);
513 if (context->activation == NULL)
515 _DBUS_ASSERT_ERROR_IS_SET (error);
519 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
523 _dbus_string_free (&full_address);
532 list_concat_new (DBusList **a,
540 link = _dbus_list_get_first_link (a);
541 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
543 if (!_dbus_list_append (result, link->data))
546 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
548 if (!_dbus_list_append (result, link->data))
554 _dbus_list_clear (result);
559 process_config_postinit (BusContext *context,
560 BusConfigParser *parser,
563 DBusHashTable *service_context_table;
564 DBusList *watched_dirs = NULL;
566 service_context_table = bus_config_parser_steal_service_context_table (parser);
567 if (!bus_registry_set_service_context_table (context->registry,
568 service_context_table))
574 _dbus_hash_table_unref (service_context_table);
576 /* We need to monitor both the configuration directories and directories
577 * containing .service files.
579 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
580 bus_config_parser_get_service_dirs (parser),
587 bus_set_watched_dirs (context, &watched_dirs);
589 _dbus_list_clear (&watched_dirs);
595 bus_context_new (const DBusString *config_file,
596 ForceForkSetting force_fork,
597 DBusPipe *print_addr_pipe,
598 DBusPipe *print_pid_pipe,
602 BusConfigParser *parser;
604 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
609 if (!dbus_server_allocate_data_slot (&server_data_slot))
615 context = dbus_new0 (BusContext, 1);
621 context->refcount = 1;
623 _dbus_generate_uuid (&context->uuid);
625 if (!_dbus_string_copy_data (config_file, &context->config_file))
631 context->loop = _dbus_loop_new ();
632 if (context->loop == NULL)
638 context->registry = bus_registry_new (context);
639 if (context->registry == NULL)
645 parser = bus_config_load (config_file, TRUE, NULL, error);
648 _DBUS_ASSERT_ERROR_IS_SET (error);
652 if (!process_config_first_time_only (context, parser, error))
654 _DBUS_ASSERT_ERROR_IS_SET (error);
657 if (!process_config_every_time (context, parser, FALSE, error))
659 _DBUS_ASSERT_ERROR_IS_SET (error);
663 /* we need another ref of the server data slot for the context
666 if (!dbus_server_allocate_data_slot (&server_data_slot))
667 _dbus_assert_not_reached ("second ref of server data slot failed");
669 /* Note that we don't know whether the print_addr_pipe is
670 * one of the sockets we're using to listen on, or some
671 * other random thing. But I think the answer is "don't do
674 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
677 const char *a = bus_context_get_address (context);
680 _dbus_assert (a != NULL);
681 if (!_dbus_string_init (&addr))
687 if (!_dbus_string_append (&addr, a) ||
688 !_dbus_string_append (&addr, "\n"))
690 _dbus_string_free (&addr);
695 bytes = _dbus_string_get_length (&addr);
696 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
698 /* pipe write returns an error on failure but not short write */
699 if (error != NULL && !dbus_error_is_set (error))
701 dbus_set_error (error, DBUS_ERROR_FAILED,
702 "Printing message bus address: did not write all bytes\n");
704 _dbus_string_free (&addr);
708 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
709 _dbus_pipe_close (print_addr_pipe, NULL);
711 _dbus_string_free (&addr);
714 context->connections = bus_connections_new (context);
715 if (context->connections == NULL)
721 context->matchmaker = bus_matchmaker_new ();
722 if (context->matchmaker == NULL)
728 /* check user before we fork */
729 if (context->user != NULL)
731 if (!_dbus_verify_daemon_user (context->user))
733 dbus_set_error (error, DBUS_ERROR_FAILED,
734 "Could not get UID and GID for username \"%s\"",
740 /* Now become a daemon if appropriate and write out pid file in any case */
744 if (context->pidfile)
745 _dbus_string_init_const (&u, context->pidfile);
747 if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
749 _dbus_verbose ("Forking and becoming daemon\n");
751 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
754 context->keep_umask))
756 _DBUS_ASSERT_ERROR_IS_SET (error);
762 _dbus_verbose ("Fork not requested\n");
764 /* Need to write PID file and to PID pipe for ourselves,
765 * not for the child process. This is a no-op if the pidfile
766 * is NULL and print_pid_pipe is NULL.
768 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
773 _DBUS_ASSERT_ERROR_IS_SET (error);
779 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
780 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
781 _dbus_pipe_close (print_pid_pipe, NULL);
783 if (!bus_selinux_full_init ())
785 _dbus_warn ("SELinux initialization failed\n");
788 if (!process_config_postinit (context, parser, error))
790 _DBUS_ASSERT_ERROR_IS_SET (error);
796 bus_config_parser_unref (parser);
800 /* Here we change our credentials if required,
801 * as soon as we've set up our sockets and pidfile
803 if (context->user != NULL)
805 if (!_dbus_change_to_daemon_user (context->user, error))
807 _DBUS_ASSERT_ERROR_IS_SET (error);
812 /* FIXME - why not just put this in full_init() below? */
813 bus_selinux_audit_init ();
817 dbus_server_free_data_slot (&server_data_slot);
823 bus_config_parser_unref (parser);
825 bus_context_unref (context);
827 if (server_data_slot >= 0)
828 dbus_server_free_data_slot (&server_data_slot);
834 bus_context_get_id (BusContext *context,
837 return _dbus_uuid_encode (&context->uuid, uuid);
841 bus_context_reload_config (BusContext *context,
844 BusConfigParser *parser;
845 DBusString config_file;
848 /* Flush the user database cache */
849 _dbus_flush_caches ();
852 _dbus_string_init_const (&config_file, context->config_file);
853 parser = bus_config_load (&config_file, TRUE, NULL, error);
856 _DBUS_ASSERT_ERROR_IS_SET (error);
860 if (!process_config_every_time (context, parser, TRUE, error))
862 _DBUS_ASSERT_ERROR_IS_SET (error);
865 if (!process_config_postinit (context, parser, error))
867 _DBUS_ASSERT_ERROR_IS_SET (error);
872 bus_context_log_info (context, "Reloaded configuration");
875 bus_context_log_info (context, "Unable to reload configuration: %s", error->message);
877 bus_config_parser_unref (parser);
882 shutdown_server (BusContext *context,
885 if (server == NULL ||
886 !dbus_server_get_is_connected (server))
889 if (!dbus_server_set_watch_functions (server,
893 _dbus_assert_not_reached ("setting watch functions to NULL failed");
895 if (!dbus_server_set_timeout_functions (server,
899 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
901 dbus_server_disconnect (server);
905 bus_context_shutdown (BusContext *context)
909 link = _dbus_list_get_first_link (&context->servers);
912 shutdown_server (context, link->data);
914 link = _dbus_list_get_next_link (&context->servers, link);
919 bus_context_ref (BusContext *context)
921 _dbus_assert (context->refcount > 0);
922 context->refcount += 1;
928 bus_context_unref (BusContext *context)
930 _dbus_assert (context->refcount > 0);
931 context->refcount -= 1;
933 if (context->refcount == 0)
937 _dbus_verbose ("Finalizing bus context %p\n", context);
939 bus_context_shutdown (context);
941 if (context->connections)
943 bus_connections_unref (context->connections);
944 context->connections = NULL;
947 if (context->registry)
949 bus_registry_unref (context->registry);
950 context->registry = NULL;
953 if (context->activation)
955 bus_activation_unref (context->activation);
956 context->activation = NULL;
959 link = _dbus_list_get_first_link (&context->servers);
962 dbus_server_unref (link->data);
964 link = _dbus_list_get_next_link (&context->servers, link);
966 _dbus_list_clear (&context->servers);
970 bus_policy_unref (context->policy);
971 context->policy = NULL;
976 _dbus_loop_unref (context->loop);
977 context->loop = NULL;
980 if (context->matchmaker)
982 bus_matchmaker_unref (context->matchmaker);
983 context->matchmaker = NULL;
986 dbus_free (context->config_file);
987 dbus_free (context->type);
988 dbus_free (context->address);
989 dbus_free (context->user);
990 dbus_free (context->servicehelper);
992 if (context->pidfile)
995 _dbus_string_init_const (&u, context->pidfile);
997 /* Deliberately ignore errors here, since there's not much
998 * we can do about it, and we're exiting anyways.
1000 _dbus_delete_file (&u, NULL);
1002 dbus_free (context->pidfile);
1004 dbus_free (context);
1006 dbus_server_free_data_slot (&server_data_slot);
1010 /* type may be NULL */
1012 bus_context_get_type (BusContext *context)
1014 return context->type;
1018 bus_context_get_address (BusContext *context)
1020 return context->address;
1024 bus_context_get_servicehelper (BusContext *context)
1026 return context->servicehelper;
1030 bus_context_get_registry (BusContext *context)
1032 return context->registry;
1036 bus_context_get_connections (BusContext *context)
1038 return context->connections;
1042 bus_context_get_activation (BusContext *context)
1044 return context->activation;
1048 bus_context_get_matchmaker (BusContext *context)
1050 return context->matchmaker;
1054 bus_context_get_loop (BusContext *context)
1056 return context->loop;
1060 bus_context_allow_unix_user (BusContext *context,
1063 return bus_policy_allow_unix_user (context->policy,
1067 /* For now this is never actually called because the default
1068 * DBusConnection behavior of 'same user that owns the bus can connect'
1069 * is all it would do.
1072 bus_context_allow_windows_user (BusContext *context,
1073 const char *windows_sid)
1075 return bus_policy_allow_windows_user (context->policy,
1080 bus_context_get_policy (BusContext *context)
1082 return context->policy;
1086 bus_context_create_client_policy (BusContext *context,
1087 DBusConnection *connection,
1090 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1091 return bus_policy_create_client_policy (context->policy, connection,
1096 bus_context_get_activation_timeout (BusContext *context)
1099 return context->limits.activation_timeout;
1103 bus_context_get_auth_timeout (BusContext *context)
1105 return context->limits.auth_timeout;
1109 bus_context_get_max_completed_connections (BusContext *context)
1111 return context->limits.max_completed_connections;
1115 bus_context_get_max_incomplete_connections (BusContext *context)
1117 return context->limits.max_incomplete_connections;
1121 bus_context_get_max_connections_per_user (BusContext *context)
1123 return context->limits.max_connections_per_user;
1127 bus_context_get_max_pending_activations (BusContext *context)
1129 return context->limits.max_pending_activations;
1133 bus_context_get_max_services_per_connection (BusContext *context)
1135 return context->limits.max_services_per_connection;
1139 bus_context_get_max_match_rules_per_connection (BusContext *context)
1141 return context->limits.max_match_rules_per_connection;
1145 bus_context_get_max_replies_per_connection (BusContext *context)
1147 return context->limits.max_replies_per_connection;
1151 bus_context_get_reply_timeout (BusContext *context)
1153 return context->limits.reply_timeout;
1157 bus_context_log_info (BusContext *context, const char *msg, ...)
1161 va_start (args, msg);
1163 if (context->syslog)
1164 _dbus_log_info (msg, args);
1170 bus_context_log_security (BusContext *context, const char *msg, ...)
1174 va_start (args, msg);
1176 if (context->syslog)
1177 _dbus_log_security (msg, args);
1183 * addressed_recipient is the recipient specified in the message.
1185 * proposed_recipient is the recipient we're considering sending
1186 * to right this second, and may be an eavesdropper.
1188 * sender is the sender of the message.
1190 * NULL for proposed_recipient or sender definitely means the bus driver.
1192 * NULL for addressed_recipient may mean the bus driver, or may mean
1193 * no destination was specified in the message (e.g. a signal).
1196 bus_context_check_security_policy (BusContext *context,
1197 BusTransaction *transaction,
1198 DBusConnection *sender,
1199 DBusConnection *addressed_recipient,
1200 DBusConnection *proposed_recipient,
1201 DBusMessage *message,
1205 BusClientPolicy *sender_policy;
1206 BusClientPolicy *recipient_policy;
1207 dbus_int32_t toggles;
1210 dbus_bool_t requested_reply;
1211 const char *sender_name;
1212 const char *sender_loginfo;
1213 const char *proposed_recipient_loginfo;
1215 type = dbus_message_get_type (message);
1216 dest = dbus_message_get_destination (message);
1218 /* dispatch.c was supposed to ensure these invariants */
1219 _dbus_assert (dest != NULL ||
1220 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1221 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1222 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1223 addressed_recipient != NULL ||
1224 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1226 /* Used in logging below */
1229 sender_name = bus_connection_get_name (sender);
1230 sender_loginfo = bus_connection_get_loginfo (sender);
1235 sender_loginfo = "(bus)";
1238 if (proposed_recipient != NULL)
1239 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1241 proposed_recipient_loginfo = "bus";
1245 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1246 case DBUS_MESSAGE_TYPE_SIGNAL:
1247 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1248 case DBUS_MESSAGE_TYPE_ERROR:
1252 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1255 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1256 "Message bus will not accept messages of unknown type\n");
1261 requested_reply = FALSE;
1265 /* First verify the SELinux access controls. If allowed then
1266 * go on with the standard checks.
1268 if (!bus_selinux_allows_send (sender, proposed_recipient,
1269 dbus_message_type_to_string (dbus_message_get_type (message)),
1270 dbus_message_get_interface (message),
1271 dbus_message_get_member (message),
1272 dbus_message_get_error_name (message),
1273 dest ? dest : DBUS_SERVICE_DBUS, error))
1275 if (error != NULL && !dbus_error_is_set (error))
1277 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1278 "An SELinux policy prevents this sender "
1279 "from sending this message to this recipient "
1280 "(rejected message had sender \"%s\" interface \"%s\" "
1281 "member \"%s\" error name \"%s\" destination \"%s\")",
1282 sender_name ? sender_name : "(unset)",
1283 dbus_message_get_interface (message) ?
1284 dbus_message_get_interface (message) : "(unset)",
1285 dbus_message_get_member (message) ?
1286 dbus_message_get_member (message) : "(unset)",
1287 dbus_message_get_error_name (message) ?
1288 dbus_message_get_error_name (message) : "(unset)",
1289 dest ? dest : DBUS_SERVICE_DBUS);
1290 _dbus_verbose ("SELinux security check denying send to service\n");
1296 if (bus_connection_is_active (sender))
1298 sender_policy = bus_connection_get_policy (sender);
1299 _dbus_assert (sender_policy != NULL);
1301 /* Fill in requested_reply variable with TRUE if this is a
1302 * reply and the reply was pending.
1304 if (dbus_message_get_reply_serial (message) != 0)
1306 if (proposed_recipient != NULL /* not to the bus driver */ &&
1307 addressed_recipient == proposed_recipient /* not eavesdropping */)
1311 dbus_error_init (&error2);
1312 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1314 sender, addressed_recipient, message,
1316 if (dbus_error_is_set (&error2))
1318 dbus_move_error (&error2, error);
1326 /* Policy for inactive connections is that they can only send
1327 * the hello message to the bus driver
1329 if (proposed_recipient == NULL &&
1330 dbus_message_is_method_call (message,
1331 DBUS_INTERFACE_DBUS,
1334 _dbus_verbose ("security check allowing %s message\n",
1340 _dbus_verbose ("security check disallowing non-%s message\n",
1343 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1344 "Client tried to send a message other than %s without being registered",
1353 sender_policy = NULL;
1355 /* If the sender is the bus driver, we assume any reply was a
1356 * requested reply as bus driver won't send bogus ones
1358 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1359 dbus_message_get_reply_serial (message) != 0)
1360 requested_reply = TRUE;
1363 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1364 (sender == NULL && sender_policy == NULL));
1366 if (proposed_recipient != NULL)
1368 /* only the bus driver can send to an inactive recipient (as it
1369 * owns no services, so other apps can't address it). Inactive
1370 * recipients can receive any message.
1372 if (bus_connection_is_active (proposed_recipient))
1374 recipient_policy = bus_connection_get_policy (proposed_recipient);
1375 _dbus_assert (recipient_policy != NULL);
1377 else if (sender == NULL)
1379 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1380 recipient_policy = NULL;
1384 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1385 recipient_policy = NULL;
1389 recipient_policy = NULL;
1391 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1392 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1393 (proposed_recipient == NULL && recipient_policy == NULL));
1396 if (sender_policy &&
1397 !bus_client_policy_check_can_send (sender_policy,
1401 message, &toggles, &log))
1403 const char *msg = "Rejected send message, %d matched rules; "
1404 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))";
1406 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1408 dbus_message_type_to_string (dbus_message_get_type (message)),
1409 sender_name ? sender_name : "(unset)",
1411 dbus_message_get_interface (message) ?
1412 dbus_message_get_interface (message) : "(unset)",
1413 dbus_message_get_member (message) ?
1414 dbus_message_get_member (message) : "(unset)",
1415 dbus_message_get_error_name (message) ?
1416 dbus_message_get_error_name (message) : "(unset)",
1418 dest ? dest : DBUS_SERVICE_DBUS,
1419 proposed_recipient_loginfo);
1420 /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1421 if (addressed_recipient == proposed_recipient)
1422 bus_context_log_security (context, msg,
1424 dbus_message_type_to_string (dbus_message_get_type (message)),
1425 sender_name ? sender_name : "(unset)",
1427 dbus_message_get_interface (message) ?
1428 dbus_message_get_interface (message) : "(unset)",
1429 dbus_message_get_member (message) ?
1430 dbus_message_get_member (message) : "(unset)",
1431 dbus_message_get_error_name (message) ?
1432 dbus_message_get_error_name (message) : "(unset)",
1434 dest ? dest : DBUS_SERVICE_DBUS,
1435 proposed_recipient_loginfo);
1436 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1441 bus_context_log_security (context,
1442 "Would reject message, %d matched rules; "
1443 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))",
1445 dbus_message_type_to_string (dbus_message_get_type (message)),
1446 sender_name ? sender_name : "(unset)",
1448 dbus_message_get_interface (message) ?
1449 dbus_message_get_interface (message) : "(unset)",
1450 dbus_message_get_member (message) ?
1451 dbus_message_get_member (message) : "(unset)",
1452 dbus_message_get_error_name (message) ?
1453 dbus_message_get_error_name (message) : "(unset)",
1455 dest ? dest : DBUS_SERVICE_DBUS,
1456 proposed_recipient_loginfo);
1458 if (recipient_policy &&
1459 !bus_client_policy_check_can_receive (recipient_policy,
1463 addressed_recipient, proposed_recipient,
1466 const char *msg = "Rejected receive message, %d matched rules; "
1467 "type=\"%s\" sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" reply serial=%u requested_reply=%d destination=\"%s\" (%s))";
1469 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1471 dbus_message_type_to_string (dbus_message_get_type (message)),
1472 sender_name ? sender_name : "(unset)",
1474 dbus_message_get_interface (message) ?
1475 dbus_message_get_interface (message) : "(unset)",
1476 dbus_message_get_member (message) ?
1477 dbus_message_get_member (message) : "(unset)",
1478 dbus_message_get_error_name (message) ?
1479 dbus_message_get_error_name (message) : "(unset)",
1480 dbus_message_get_reply_serial (message),
1482 dest ? dest : DBUS_SERVICE_DBUS,
1483 proposed_recipient_loginfo);
1484 /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1485 if (addressed_recipient == proposed_recipient)
1486 bus_context_log_security (context, msg,
1488 dbus_message_type_to_string (dbus_message_get_type (message)),
1489 sender_name ? sender_name : "(unset)",
1491 dbus_message_get_interface (message) ?
1492 dbus_message_get_interface (message) : "(unset)",
1493 dbus_message_get_member (message) ?
1494 dbus_message_get_member (message) : "(unset)",
1495 dbus_message_get_error_name (message) ?
1496 dbus_message_get_error_name (message) : "(unset)",
1497 dbus_message_get_reply_serial (message),
1499 dest ? dest : DBUS_SERVICE_DBUS,
1500 proposed_recipient_loginfo);
1501 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1505 /* See if limits on size have been exceeded */
1506 if (proposed_recipient &&
1507 dbus_connection_get_outgoing_size (proposed_recipient) >
1508 context->limits.max_outgoing_bytes)
1510 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1511 "The destination service \"%s\" has a full message queue",
1512 dest ? dest : (proposed_recipient ?
1513 bus_connection_get_name (proposed_recipient) :
1514 DBUS_SERVICE_DBUS));
1515 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1519 /* Record that we will allow a reply here in the future (don't
1520 * bother if the recipient is the bus or this is an eavesdropping
1521 * connection). Only the addressed recipient may reply.
1523 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1525 addressed_recipient &&
1526 addressed_recipient == proposed_recipient && /* not eavesdropping */
1527 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1529 sender, addressed_recipient,
1532 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1536 _dbus_verbose ("security policy allowing message\n");