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;
59 unsigned int allow_anonymous : 1;
62 static dbus_int32_t server_data_slot = -1;
69 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
72 server_get_context (DBusServer *server)
77 if (!dbus_server_allocate_data_slot (&server_data_slot))
80 bd = BUS_SERVER_DATA (server);
83 dbus_server_free_data_slot (&server_data_slot);
87 context = bd->context;
89 dbus_server_free_data_slot (&server_data_slot);
95 server_watch_callback (DBusWatch *watch,
96 unsigned int condition,
99 /* FIXME this can be done in dbus-mainloop.c
100 * if the code in activation.c for the babysitter
101 * watch handler is fixed.
104 return dbus_watch_handle (watch, condition);
108 add_server_watch (DBusWatch *watch,
111 DBusServer *server = data;
114 context = server_get_context (server);
116 return _dbus_loop_add_watch (context->loop,
117 watch, server_watch_callback, server,
122 remove_server_watch (DBusWatch *watch,
125 DBusServer *server = data;
128 context = server_get_context (server);
130 _dbus_loop_remove_watch (context->loop,
131 watch, server_watch_callback, server);
136 server_timeout_callback (DBusTimeout *timeout,
139 /* can return FALSE on OOM but we just let it fire again later */
140 dbus_timeout_handle (timeout);
144 add_server_timeout (DBusTimeout *timeout,
147 DBusServer *server = data;
150 context = server_get_context (server);
152 return _dbus_loop_add_timeout (context->loop,
153 timeout, server_timeout_callback, server, NULL);
157 remove_server_timeout (DBusTimeout *timeout,
160 DBusServer *server = data;
163 context = server_get_context (server);
165 _dbus_loop_remove_timeout (context->loop,
166 timeout, server_timeout_callback, server);
170 new_connection_callback (DBusServer *server,
171 DBusConnection *new_connection,
174 BusContext *context = data;
176 if (!bus_connections_setup_connection (context->connections, new_connection))
178 _dbus_verbose ("No memory to setup new connection\n");
180 /* if we don't do this, it will get unref'd without
181 * being disconnected... kind of strange really
182 * that we have to do this, people won't get it right
185 dbus_connection_close (new_connection);
188 dbus_connection_set_max_received_size (new_connection,
189 context->limits.max_incoming_bytes);
191 dbus_connection_set_max_message_size (new_connection,
192 context->limits.max_message_size);
194 dbus_connection_set_max_received_unix_fds (new_connection,
195 context->limits.max_incoming_unix_fds);
197 dbus_connection_set_max_message_unix_fds (new_connection,
198 context->limits.max_message_unix_fds);
200 dbus_connection_set_allow_anonymous (new_connection,
201 context->allow_anonymous);
203 /* on OOM, we won't have ref'd the connection so it will die. */
207 free_server_data (void *data)
209 BusServerData *bd = data;
215 setup_server (BusContext *context,
217 char **auth_mechanisms,
222 bd = dbus_new0 (BusServerData, 1);
223 if (bd == NULL || !dbus_server_set_data (server,
225 bd, free_server_data))
232 bd->context = context;
234 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
240 dbus_server_set_new_connection_function (server,
241 new_connection_callback,
244 if (!dbus_server_set_watch_functions (server,
255 if (!dbus_server_set_timeout_functions (server,
257 remove_server_timeout,
268 /* This code only gets executed the first time the
269 * config files are parsed. It is not executed
270 * when config files are reloaded.
273 process_config_first_time_only (BusContext *context,
274 BusConfigParser *parser,
278 DBusList **addresses;
279 const char *user, *pidfile;
280 char **auth_mechanisms;
281 DBusList **auth_mechanisms_list;
285 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
288 auth_mechanisms = NULL;
290 /* Check for an existing pid file. Of course this is a race;
291 * we'd have to use fcntl() locks on the pid file to
292 * avoid that. But we want to check for the pid file
293 * before overwriting any existing sockets, etc.
295 pidfile = bus_config_parser_get_pidfile (parser);
301 _dbus_string_init_const (&u, pidfile);
303 if (_dbus_stat (&u, &stbuf, NULL))
305 dbus_set_error (error, DBUS_ERROR_FAILED,
306 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
312 /* keep around the pid filename so we can delete it later */
313 context->pidfile = _dbus_strdup (pidfile);
315 /* Build an array of auth mechanisms */
317 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
318 len = _dbus_list_get_length (auth_mechanisms_list);
324 auth_mechanisms = dbus_new0 (char*, len + 1);
325 if (auth_mechanisms == NULL)
332 link = _dbus_list_get_first_link (auth_mechanisms_list);
335 auth_mechanisms[i] = _dbus_strdup (link->data);
336 if (auth_mechanisms[i] == NULL)
341 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
346 auth_mechanisms = NULL;
349 /* Listen on our addresses */
351 addresses = bus_config_parser_get_addresses (parser);
353 link = _dbus_list_get_first_link (addresses);
358 server = dbus_server_listen (link->data, error);
361 _DBUS_ASSERT_ERROR_IS_SET (error);
364 else if (!setup_server (context, server, auth_mechanisms, error))
366 _DBUS_ASSERT_ERROR_IS_SET (error);
370 if (!_dbus_list_append (&context->servers, server))
376 link = _dbus_list_get_next_link (addresses, link);
379 /* note that type may be NULL */
380 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
381 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
387 user = bus_config_parser_get_user (parser);
390 context->user = _dbus_strdup (user);
391 if (context->user == NULL)
398 context->fork = bus_config_parser_get_fork (parser);
399 context->syslog = bus_config_parser_get_syslog (parser);
400 context->keep_umask = bus_config_parser_get_keep_umask (parser);
401 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
403 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
407 dbus_free_string_array (auth_mechanisms);
411 /* This code gets executed every time the config files
412 * are parsed: both during BusContext construction
413 * and on reloads. This function is slightly screwy
414 * since it can do a "half reload" in out-of-memory
415 * situations. Realistically, unlikely to ever matter.
418 process_config_every_time (BusContext *context,
419 BusConfigParser *parser,
420 dbus_bool_t is_reload,
423 DBusString full_address;
426 BusActivation *new_activation;
428 const char *servicehelper;
433 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
438 if (!_dbus_string_init (&full_address))
444 /* get our limits and timeout lengths */
445 bus_config_parser_get_limits (parser, &context->limits);
448 bus_policy_unref (context->policy);
449 context->policy = bus_config_parser_steal_policy (parser);
450 _dbus_assert (context->policy != NULL);
452 /* We have to build the address backward, so that
453 * <listen> later in the config file have priority
455 link = _dbus_list_get_last_link (&context->servers);
458 addr = dbus_server_get_address (link->data);
465 if (_dbus_string_get_length (&full_address) > 0)
467 if (!_dbus_string_append (&full_address, ";"))
474 if (!_dbus_string_append (&full_address, addr))
483 link = _dbus_list_get_prev_link (&context->servers, link);
487 dbus_free (context->address);
489 if (!_dbus_string_copy_data (&full_address, &context->address))
495 /* get the service directories */
496 dirs = bus_config_parser_get_service_dirs (parser);
498 /* and the service helper */
499 servicehelper = bus_config_parser_get_servicehelper (parser);
501 s = _dbus_strdup(servicehelper);
502 if (s == NULL && servicehelper != NULL)
509 dbus_free(context->servicehelper);
510 context->servicehelper = s;
513 /* Create activation subsystem */
514 if (context->activation)
516 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
521 context->activation = bus_activation_new (context, &full_address, dirs, error);
524 if (context->activation == NULL)
526 _DBUS_ASSERT_ERROR_IS_SET (error);
530 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
534 _dbus_string_free (&full_address);
543 process_config_postinit (BusContext *context,
544 BusConfigParser *parser,
547 DBusHashTable *service_context_table;
549 service_context_table = bus_config_parser_steal_service_context_table (parser);
550 if (!bus_registry_set_service_context_table (context->registry,
551 service_context_table))
557 _dbus_hash_table_unref (service_context_table);
559 /* Watch all conf directories */
560 bus_set_watched_dirs (context, bus_config_parser_get_conf_dirs (parser));
566 bus_shutdown_all_directory_watches (void *data)
568 bus_set_watched_dirs ((BusContext *) data, NULL);
572 bus_context_new (const DBusString *config_file,
573 ForceForkSetting force_fork,
574 DBusPipe *print_addr_pipe,
575 DBusPipe *print_pid_pipe,
579 BusConfigParser *parser;
581 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
586 if (!dbus_server_allocate_data_slot (&server_data_slot))
592 context = dbus_new0 (BusContext, 1);
598 context->refcount = 1;
600 _dbus_generate_uuid (&context->uuid);
602 _dbus_register_shutdown_func (bus_shutdown_all_directory_watches, context);
604 if (!_dbus_string_copy_data (config_file, &context->config_file))
610 context->loop = _dbus_loop_new ();
611 if (context->loop == NULL)
617 context->registry = bus_registry_new (context);
618 if (context->registry == NULL)
624 parser = bus_config_load (config_file, TRUE, NULL, error);
627 _DBUS_ASSERT_ERROR_IS_SET (error);
631 if (!process_config_first_time_only (context, parser, error))
633 _DBUS_ASSERT_ERROR_IS_SET (error);
636 if (!process_config_every_time (context, parser, FALSE, error))
638 _DBUS_ASSERT_ERROR_IS_SET (error);
642 /* we need another ref of the server data slot for the context
645 if (!dbus_server_allocate_data_slot (&server_data_slot))
646 _dbus_assert_not_reached ("second ref of server data slot failed");
648 /* Note that we don't know whether the print_addr_pipe is
649 * one of the sockets we're using to listen on, or some
650 * other random thing. But I think the answer is "don't do
653 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
656 const char *a = bus_context_get_address (context);
659 _dbus_assert (a != NULL);
660 if (!_dbus_string_init (&addr))
666 if (!_dbus_string_append (&addr, a) ||
667 !_dbus_string_append (&addr, "\n"))
669 _dbus_string_free (&addr);
674 bytes = _dbus_string_get_length (&addr);
675 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
677 /* pipe write returns an error on failure but not short write */
678 if (error != NULL && !dbus_error_is_set (error))
680 dbus_set_error (error, DBUS_ERROR_FAILED,
681 "Printing message bus address: did not write all bytes\n");
683 _dbus_string_free (&addr);
687 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
688 _dbus_pipe_close (print_addr_pipe, NULL);
690 _dbus_string_free (&addr);
693 context->connections = bus_connections_new (context);
694 if (context->connections == NULL)
700 context->matchmaker = bus_matchmaker_new ();
701 if (context->matchmaker == NULL)
707 /* check user before we fork */
708 if (context->user != NULL)
710 if (!_dbus_verify_daemon_user (context->user))
712 dbus_set_error (error, DBUS_ERROR_FAILED,
713 "Could not get UID and GID for username \"%s\"",
719 /* Now become a daemon if appropriate and write out pid file in any case */
723 if (context->pidfile)
724 _dbus_string_init_const (&u, context->pidfile);
726 if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
728 _dbus_verbose ("Forking and becoming daemon\n");
730 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
733 context->keep_umask))
735 _DBUS_ASSERT_ERROR_IS_SET (error);
741 _dbus_verbose ("Fork not requested\n");
743 /* Need to write PID file and to PID pipe for ourselves,
744 * not for the child process. This is a no-op if the pidfile
745 * is NULL and print_pid_pipe is NULL.
747 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
752 _DBUS_ASSERT_ERROR_IS_SET (error);
758 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
759 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
760 _dbus_pipe_close (print_pid_pipe, NULL);
762 if (!bus_selinux_full_init ())
764 _dbus_warn ("SELinux initialization failed\n");
767 if (!process_config_postinit (context, parser, error))
769 _DBUS_ASSERT_ERROR_IS_SET (error);
775 bus_config_parser_unref (parser);
779 /* Here we change our credentials if required,
780 * as soon as we've set up our sockets and pidfile
782 if (context->user != NULL)
784 if (!_dbus_change_to_daemon_user (context->user, error))
786 _DBUS_ASSERT_ERROR_IS_SET (error);
791 /* FIXME - why not just put this in full_init() below? */
792 bus_selinux_audit_init ();
796 dbus_server_free_data_slot (&server_data_slot);
802 bus_config_parser_unref (parser);
804 bus_context_unref (context);
806 if (server_data_slot >= 0)
807 dbus_server_free_data_slot (&server_data_slot);
813 bus_context_get_id (BusContext *context,
816 return _dbus_uuid_encode (&context->uuid, uuid);
820 bus_context_reload_config (BusContext *context,
823 BusConfigParser *parser;
824 DBusString config_file;
827 /* Flush the user database cache */
828 _dbus_flush_caches ();
831 _dbus_string_init_const (&config_file, context->config_file);
832 parser = bus_config_load (&config_file, TRUE, NULL, error);
835 _DBUS_ASSERT_ERROR_IS_SET (error);
839 if (!process_config_every_time (context, parser, TRUE, error))
841 _DBUS_ASSERT_ERROR_IS_SET (error);
844 if (!process_config_postinit (context, parser, error))
846 _DBUS_ASSERT_ERROR_IS_SET (error);
851 bus_context_log_info (context, "Reloaded configuration");
854 bus_context_log_info (context, "Unable to reload configuration: %s", error->message);
856 bus_config_parser_unref (parser);
861 shutdown_server (BusContext *context,
864 if (server == NULL ||
865 !dbus_server_get_is_connected (server))
868 if (!dbus_server_set_watch_functions (server,
872 _dbus_assert_not_reached ("setting watch functions to NULL failed");
874 if (!dbus_server_set_timeout_functions (server,
878 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
880 dbus_server_disconnect (server);
884 bus_context_shutdown (BusContext *context)
888 link = _dbus_list_get_first_link (&context->servers);
891 shutdown_server (context, link->data);
893 link = _dbus_list_get_next_link (&context->servers, link);
898 bus_context_ref (BusContext *context)
900 _dbus_assert (context->refcount > 0);
901 context->refcount += 1;
907 bus_context_unref (BusContext *context)
909 _dbus_assert (context->refcount > 0);
910 context->refcount -= 1;
912 if (context->refcount == 0)
916 _dbus_verbose ("Finalizing bus context %p\n", context);
918 bus_context_shutdown (context);
920 if (context->connections)
922 bus_connections_unref (context->connections);
923 context->connections = NULL;
926 if (context->registry)
928 bus_registry_unref (context->registry);
929 context->registry = NULL;
932 if (context->activation)
934 bus_activation_unref (context->activation);
935 context->activation = NULL;
938 link = _dbus_list_get_first_link (&context->servers);
941 dbus_server_unref (link->data);
943 link = _dbus_list_get_next_link (&context->servers, link);
945 _dbus_list_clear (&context->servers);
949 bus_policy_unref (context->policy);
950 context->policy = NULL;
955 _dbus_loop_unref (context->loop);
956 context->loop = NULL;
959 if (context->matchmaker)
961 bus_matchmaker_unref (context->matchmaker);
962 context->matchmaker = NULL;
965 dbus_free (context->config_file);
966 dbus_free (context->type);
967 dbus_free (context->address);
968 dbus_free (context->user);
969 dbus_free (context->servicehelper);
971 if (context->pidfile)
974 _dbus_string_init_const (&u, context->pidfile);
976 /* Deliberately ignore errors here, since there's not much
977 * we can do about it, and we're exiting anyways.
979 _dbus_delete_file (&u, NULL);
981 dbus_free (context->pidfile);
985 dbus_server_free_data_slot (&server_data_slot);
989 /* type may be NULL */
991 bus_context_get_type (BusContext *context)
993 return context->type;
997 bus_context_get_address (BusContext *context)
999 return context->address;
1003 bus_context_get_servicehelper (BusContext *context)
1005 return context->servicehelper;
1009 bus_context_get_registry (BusContext *context)
1011 return context->registry;
1015 bus_context_get_connections (BusContext *context)
1017 return context->connections;
1021 bus_context_get_activation (BusContext *context)
1023 return context->activation;
1027 bus_context_get_matchmaker (BusContext *context)
1029 return context->matchmaker;
1033 bus_context_get_loop (BusContext *context)
1035 return context->loop;
1039 bus_context_allow_unix_user (BusContext *context,
1042 return bus_policy_allow_unix_user (context->policy,
1046 /* For now this is never actually called because the default
1047 * DBusConnection behavior of 'same user that owns the bus can connect'
1048 * is all it would do.
1051 bus_context_allow_windows_user (BusContext *context,
1052 const char *windows_sid)
1054 return bus_policy_allow_windows_user (context->policy,
1059 bus_context_get_policy (BusContext *context)
1061 return context->policy;
1065 bus_context_create_client_policy (BusContext *context,
1066 DBusConnection *connection,
1069 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1070 return bus_policy_create_client_policy (context->policy, connection,
1075 bus_context_get_activation_timeout (BusContext *context)
1078 return context->limits.activation_timeout;
1082 bus_context_get_auth_timeout (BusContext *context)
1084 return context->limits.auth_timeout;
1088 bus_context_get_max_completed_connections (BusContext *context)
1090 return context->limits.max_completed_connections;
1094 bus_context_get_max_incomplete_connections (BusContext *context)
1096 return context->limits.max_incomplete_connections;
1100 bus_context_get_max_connections_per_user (BusContext *context)
1102 return context->limits.max_connections_per_user;
1106 bus_context_get_max_pending_activations (BusContext *context)
1108 return context->limits.max_pending_activations;
1112 bus_context_get_max_services_per_connection (BusContext *context)
1114 return context->limits.max_services_per_connection;
1118 bus_context_get_max_match_rules_per_connection (BusContext *context)
1120 return context->limits.max_match_rules_per_connection;
1124 bus_context_get_max_replies_per_connection (BusContext *context)
1126 return context->limits.max_replies_per_connection;
1130 bus_context_get_reply_timeout (BusContext *context)
1132 return context->limits.reply_timeout;
1136 bus_context_log_info (BusContext *context, const char *msg, ...)
1140 va_start (args, msg);
1142 if (context->syslog)
1143 _dbus_log_info (msg, args);
1149 bus_context_log_security (BusContext *context, const char *msg, ...)
1153 va_start (args, msg);
1155 if (context->syslog)
1156 _dbus_log_security (msg, args);
1162 * addressed_recipient is the recipient specified in the message.
1164 * proposed_recipient is the recipient we're considering sending
1165 * to right this second, and may be an eavesdropper.
1167 * sender is the sender of the message.
1169 * NULL for proposed_recipient or sender definitely means the bus driver.
1171 * NULL for addressed_recipient may mean the bus driver, or may mean
1172 * no destination was specified in the message (e.g. a signal).
1175 bus_context_check_security_policy (BusContext *context,
1176 BusTransaction *transaction,
1177 DBusConnection *sender,
1178 DBusConnection *addressed_recipient,
1179 DBusConnection *proposed_recipient,
1180 DBusMessage *message,
1184 BusClientPolicy *sender_policy;
1185 BusClientPolicy *recipient_policy;
1186 dbus_int32_t toggles;
1189 dbus_bool_t requested_reply;
1190 const char *sender_name;
1191 const char *sender_loginfo;
1192 const char *proposed_recipient_loginfo;
1194 type = dbus_message_get_type (message);
1195 dest = dbus_message_get_destination (message);
1197 /* dispatch.c was supposed to ensure these invariants */
1198 _dbus_assert (dest != NULL ||
1199 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1200 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1201 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1202 addressed_recipient != NULL ||
1203 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1205 /* Used in logging below */
1208 sender_name = bus_connection_get_name (sender);
1209 sender_loginfo = bus_connection_get_loginfo (sender);
1214 sender_loginfo = "(bus)";
1217 if (proposed_recipient != NULL)
1218 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1220 proposed_recipient_loginfo = "bus";
1224 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1225 case DBUS_MESSAGE_TYPE_SIGNAL:
1226 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1227 case DBUS_MESSAGE_TYPE_ERROR:
1231 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1234 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1235 "Message bus will not accept messages of unknown type\n");
1240 requested_reply = FALSE;
1244 /* First verify the SELinux access controls. If allowed then
1245 * go on with the standard checks.
1247 if (!bus_selinux_allows_send (sender, proposed_recipient,
1248 dbus_message_type_to_string (dbus_message_get_type (message)),
1249 dbus_message_get_interface (message),
1250 dbus_message_get_member (message),
1251 dbus_message_get_error_name (message),
1252 dest ? dest : DBUS_SERVICE_DBUS, error))
1254 if (error != NULL && !dbus_error_is_set (error))
1256 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1257 "An SELinux policy prevents this sender "
1258 "from sending this message to this recipient "
1259 "(rejected message had sender \"%s\" interface \"%s\" "
1260 "member \"%s\" error name \"%s\" destination \"%s\")",
1261 sender_name ? sender_name : "(unset)",
1262 dbus_message_get_interface (message) ?
1263 dbus_message_get_interface (message) : "(unset)",
1264 dbus_message_get_member (message) ?
1265 dbus_message_get_member (message) : "(unset)",
1266 dbus_message_get_error_name (message) ?
1267 dbus_message_get_error_name (message) : "(unset)",
1268 dest ? dest : DBUS_SERVICE_DBUS);
1269 _dbus_verbose ("SELinux security check denying send to service\n");
1275 if (bus_connection_is_active (sender))
1277 sender_policy = bus_connection_get_policy (sender);
1278 _dbus_assert (sender_policy != NULL);
1280 /* Fill in requested_reply variable with TRUE if this is a
1281 * reply and the reply was pending.
1283 if (dbus_message_get_reply_serial (message) != 0)
1285 if (proposed_recipient != NULL /* not to the bus driver */ &&
1286 addressed_recipient == proposed_recipient /* not eavesdropping */)
1290 dbus_error_init (&error2);
1291 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1293 sender, addressed_recipient, message,
1295 if (dbus_error_is_set (&error2))
1297 dbus_move_error (&error2, error);
1305 /* Policy for inactive connections is that they can only send
1306 * the hello message to the bus driver
1308 if (proposed_recipient == NULL &&
1309 dbus_message_is_method_call (message,
1310 DBUS_INTERFACE_DBUS,
1313 _dbus_verbose ("security check allowing %s message\n",
1319 _dbus_verbose ("security check disallowing non-%s message\n",
1322 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1323 "Client tried to send a message other than %s without being registered",
1332 sender_policy = NULL;
1334 /* If the sender is the bus driver, we assume any reply was a
1335 * requested reply as bus driver won't send bogus ones
1337 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1338 dbus_message_get_reply_serial (message) != 0)
1339 requested_reply = TRUE;
1342 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1343 (sender == NULL && sender_policy == NULL));
1345 if (proposed_recipient != NULL)
1347 /* only the bus driver can send to an inactive recipient (as it
1348 * owns no services, so other apps can't address it). Inactive
1349 * recipients can receive any message.
1351 if (bus_connection_is_active (proposed_recipient))
1353 recipient_policy = bus_connection_get_policy (proposed_recipient);
1354 _dbus_assert (recipient_policy != NULL);
1356 else if (sender == NULL)
1358 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1359 recipient_policy = NULL;
1363 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1364 recipient_policy = NULL;
1368 recipient_policy = NULL;
1370 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1371 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1372 (proposed_recipient == NULL && recipient_policy == NULL));
1375 if (sender_policy &&
1376 !bus_client_policy_check_can_send (sender_policy,
1380 message, &toggles, &log))
1382 const char *msg = "Rejected send message, %d matched rules; "
1383 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))";
1385 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1387 dbus_message_type_to_string (dbus_message_get_type (message)),
1388 sender_name ? sender_name : "(unset)",
1390 dbus_message_get_interface (message) ?
1391 dbus_message_get_interface (message) : "(unset)",
1392 dbus_message_get_member (message) ?
1393 dbus_message_get_member (message) : "(unset)",
1394 dbus_message_get_error_name (message) ?
1395 dbus_message_get_error_name (message) : "(unset)",
1397 dest ? dest : DBUS_SERVICE_DBUS,
1398 proposed_recipient_loginfo);
1399 /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1400 if (addressed_recipient == proposed_recipient)
1401 bus_context_log_security (context, msg,
1403 dbus_message_type_to_string (dbus_message_get_type (message)),
1404 sender_name ? sender_name : "(unset)",
1406 dbus_message_get_interface (message) ?
1407 dbus_message_get_interface (message) : "(unset)",
1408 dbus_message_get_member (message) ?
1409 dbus_message_get_member (message) : "(unset)",
1410 dbus_message_get_error_name (message) ?
1411 dbus_message_get_error_name (message) : "(unset)",
1413 dest ? dest : DBUS_SERVICE_DBUS,
1414 proposed_recipient_loginfo);
1415 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1420 bus_context_log_security (context,
1421 "Would reject message, %d matched rules; "
1422 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))",
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);
1437 if (recipient_policy &&
1438 !bus_client_policy_check_can_receive (recipient_policy,
1442 addressed_recipient, proposed_recipient,
1445 const char *msg = "Rejected receive message, %d matched rules; "
1446 "type=\"%s\" sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" reply serial=%u requested_reply=%d destination=\"%s\" (%s))";
1448 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1450 dbus_message_type_to_string (dbus_message_get_type (message)),
1451 sender_name ? sender_name : "(unset)",
1453 dbus_message_get_interface (message) ?
1454 dbus_message_get_interface (message) : "(unset)",
1455 dbus_message_get_member (message) ?
1456 dbus_message_get_member (message) : "(unset)",
1457 dbus_message_get_error_name (message) ?
1458 dbus_message_get_error_name (message) : "(unset)",
1459 dbus_message_get_reply_serial (message),
1461 dest ? dest : DBUS_SERVICE_DBUS,
1462 proposed_recipient_loginfo);
1463 /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1464 if (addressed_recipient == proposed_recipient)
1465 bus_context_log_security (context, msg,
1467 dbus_message_type_to_string (dbus_message_get_type (message)),
1468 sender_name ? sender_name : "(unset)",
1470 dbus_message_get_interface (message) ?
1471 dbus_message_get_interface (message) : "(unset)",
1472 dbus_message_get_member (message) ?
1473 dbus_message_get_member (message) : "(unset)",
1474 dbus_message_get_error_name (message) ?
1475 dbus_message_get_error_name (message) : "(unset)",
1476 dbus_message_get_reply_serial (message),
1478 dest ? dest : DBUS_SERVICE_DBUS,
1479 proposed_recipient_loginfo);
1480 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1484 /* See if limits on size have been exceeded */
1485 if (proposed_recipient &&
1486 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1487 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1489 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1490 "The destination service \"%s\" has a full message queue",
1491 dest ? dest : (proposed_recipient ?
1492 bus_connection_get_name (proposed_recipient) :
1493 DBUS_SERVICE_DBUS));
1494 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1498 /* Record that we will allow a reply here in the future (don't
1499 * bother if the recipient is the bus or this is an eavesdropping
1500 * connection). Only the addressed recipient may reply.
1502 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1504 addressed_recipient &&
1505 addressed_recipient == proposed_recipient && /* not eavesdropping */
1506 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1508 sender, addressed_recipient,
1511 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1515 _dbus_verbose ("security policy allowing message\n");