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,
280 DBusString log_prefix;
282 DBusList **addresses;
283 const char *user, *pidfile;
284 char **auth_mechanisms;
285 DBusList **auth_mechanisms_list;
289 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
292 auth_mechanisms = NULL;
294 /* Check for an existing pid file. Of course this is a race;
295 * we'd have to use fcntl() locks on the pid file to
296 * avoid that. But we want to check for the pid file
297 * before overwriting any existing sockets, etc.
299 pidfile = bus_config_parser_get_pidfile (parser);
305 _dbus_string_init_const (&u, pidfile);
307 if (_dbus_stat (&u, &stbuf, NULL))
309 dbus_set_error (error, DBUS_ERROR_FAILED,
310 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
316 /* keep around the pid filename so we can delete it later */
317 context->pidfile = _dbus_strdup (pidfile);
319 /* note that type may be NULL */
320 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
321 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
324 user = bus_config_parser_get_user (parser);
327 context->user = _dbus_strdup (user);
328 if (context->user == NULL)
332 /* Set up the prefix for syslog messages */
333 if (!_dbus_string_init (&log_prefix))
335 if (context->type && !strcmp (context->type, "system"))
337 if (!_dbus_string_append (&log_prefix, "[system] "))
340 else if (context->type && !strcmp (context->type, "session"))
342 DBusCredentials *credentials;
344 credentials = _dbus_credentials_new_from_current_process ();
347 if (!_dbus_string_append (&log_prefix, "[session "))
349 if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
351 if (!_dbus_string_append (&log_prefix, "] "))
353 _dbus_credentials_unref (credentials);
355 if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
357 _dbus_string_free (&log_prefix);
359 /* Build an array of auth mechanisms */
361 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
362 len = _dbus_list_get_length (auth_mechanisms_list);
368 auth_mechanisms = dbus_new0 (char*, len + 1);
369 if (auth_mechanisms == NULL)
373 link = _dbus_list_get_first_link (auth_mechanisms_list);
376 auth_mechanisms[i] = _dbus_strdup (link->data);
377 if (auth_mechanisms[i] == NULL)
379 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
384 auth_mechanisms = NULL;
387 /* Listen on our addresses */
389 addresses = bus_config_parser_get_addresses (parser);
391 link = _dbus_list_get_first_link (addresses);
396 server = dbus_server_listen (link->data, error);
399 _DBUS_ASSERT_ERROR_IS_SET (error);
402 else if (!setup_server (context, server, auth_mechanisms, error))
404 _DBUS_ASSERT_ERROR_IS_SET (error);
408 if (!_dbus_list_append (&context->servers, server))
411 link = _dbus_list_get_next_link (addresses, link);
414 context->fork = bus_config_parser_get_fork (parser);
415 context->syslog = bus_config_parser_get_syslog (parser);
416 context->keep_umask = bus_config_parser_get_keep_umask (parser);
417 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
419 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
423 dbus_free_string_array (auth_mechanisms);
428 dbus_free_string_array (auth_mechanisms);
432 /* This code gets executed every time the config files
433 * are parsed: both during BusContext construction
434 * and on reloads. This function is slightly screwy
435 * since it can do a "half reload" in out-of-memory
436 * situations. Realistically, unlikely to ever matter.
439 process_config_every_time (BusContext *context,
440 BusConfigParser *parser,
441 dbus_bool_t is_reload,
444 DBusString full_address;
447 BusActivation *new_activation;
449 const char *servicehelper;
454 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
459 if (!_dbus_string_init (&full_address))
465 /* get our limits and timeout lengths */
466 bus_config_parser_get_limits (parser, &context->limits);
469 bus_policy_unref (context->policy);
470 context->policy = bus_config_parser_steal_policy (parser);
471 _dbus_assert (context->policy != NULL);
473 /* We have to build the address backward, so that
474 * <listen> later in the config file have priority
476 link = _dbus_list_get_last_link (&context->servers);
479 addr = dbus_server_get_address (link->data);
486 if (_dbus_string_get_length (&full_address) > 0)
488 if (!_dbus_string_append (&full_address, ";"))
495 if (!_dbus_string_append (&full_address, addr))
504 link = _dbus_list_get_prev_link (&context->servers, link);
508 dbus_free (context->address);
510 if (!_dbus_string_copy_data (&full_address, &context->address))
516 /* get the service directories */
517 dirs = bus_config_parser_get_service_dirs (parser);
519 /* and the service helper */
520 servicehelper = bus_config_parser_get_servicehelper (parser);
522 s = _dbus_strdup(servicehelper);
523 if (s == NULL && servicehelper != NULL)
530 dbus_free(context->servicehelper);
531 context->servicehelper = s;
534 /* Create activation subsystem */
535 if (context->activation)
537 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
542 context->activation = bus_activation_new (context, &full_address, dirs, error);
545 if (context->activation == NULL)
547 _DBUS_ASSERT_ERROR_IS_SET (error);
551 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
555 _dbus_string_free (&full_address);
564 list_concat_new (DBusList **a,
572 link = _dbus_list_get_first_link (a);
573 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
575 if (!_dbus_list_append (result, link->data))
578 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
580 if (!_dbus_list_append (result, link->data))
586 _dbus_list_clear (result);
591 process_config_postinit (BusContext *context,
592 BusConfigParser *parser,
595 DBusHashTable *service_context_table;
596 DBusList *watched_dirs = NULL;
598 service_context_table = bus_config_parser_steal_service_context_table (parser);
599 if (!bus_registry_set_service_context_table (context->registry,
600 service_context_table))
606 _dbus_hash_table_unref (service_context_table);
608 /* We need to monitor both the configuration directories and directories
609 * containing .service files.
611 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
612 bus_config_parser_get_service_dirs (parser),
619 bus_set_watched_dirs (context, &watched_dirs);
621 _dbus_list_clear (&watched_dirs);
627 bus_context_new (const DBusString *config_file,
628 ForceForkSetting force_fork,
629 DBusPipe *print_addr_pipe,
630 DBusPipe *print_pid_pipe,
633 DBusString log_prefix;
635 BusConfigParser *parser;
637 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
642 if (!dbus_server_allocate_data_slot (&server_data_slot))
648 context = dbus_new0 (BusContext, 1);
654 context->refcount = 1;
656 _dbus_generate_uuid (&context->uuid);
658 if (!_dbus_string_copy_data (config_file, &context->config_file))
664 context->loop = _dbus_loop_new ();
665 if (context->loop == NULL)
671 context->registry = bus_registry_new (context);
672 if (context->registry == NULL)
678 parser = bus_config_load (config_file, TRUE, NULL, error);
681 _DBUS_ASSERT_ERROR_IS_SET (error);
685 if (!process_config_first_time_only (context, parser, error))
687 _DBUS_ASSERT_ERROR_IS_SET (error);
690 if (!process_config_every_time (context, parser, FALSE, error))
692 _DBUS_ASSERT_ERROR_IS_SET (error);
696 /* we need another ref of the server data slot for the context
699 if (!dbus_server_allocate_data_slot (&server_data_slot))
700 _dbus_assert_not_reached ("second ref of server data slot failed");
702 /* Note that we don't know whether the print_addr_pipe is
703 * one of the sockets we're using to listen on, or some
704 * other random thing. But I think the answer is "don't do
707 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
710 const char *a = bus_context_get_address (context);
713 _dbus_assert (a != NULL);
714 if (!_dbus_string_init (&addr))
720 if (!_dbus_string_append (&addr, a) ||
721 !_dbus_string_append (&addr, "\n"))
723 _dbus_string_free (&addr);
728 bytes = _dbus_string_get_length (&addr);
729 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
731 /* pipe write returns an error on failure but not short write */
732 if (error != NULL && !dbus_error_is_set (error))
734 dbus_set_error (error, DBUS_ERROR_FAILED,
735 "Printing message bus address: did not write all bytes\n");
737 _dbus_string_free (&addr);
741 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
742 _dbus_pipe_close (print_addr_pipe, NULL);
744 _dbus_string_free (&addr);
747 context->connections = bus_connections_new (context);
748 if (context->connections == NULL)
754 context->matchmaker = bus_matchmaker_new ();
755 if (context->matchmaker == NULL)
761 /* check user before we fork */
762 if (context->user != NULL)
764 if (!_dbus_verify_daemon_user (context->user))
766 dbus_set_error (error, DBUS_ERROR_FAILED,
767 "Could not get UID and GID for username \"%s\"",
773 /* Now become a daemon if appropriate and write out pid file in any case */
777 if (context->pidfile)
778 _dbus_string_init_const (&u, context->pidfile);
780 if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
782 _dbus_verbose ("Forking and becoming daemon\n");
784 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
787 context->keep_umask))
789 _DBUS_ASSERT_ERROR_IS_SET (error);
795 _dbus_verbose ("Fork not requested\n");
797 /* Need to write PID file and to PID pipe for ourselves,
798 * not for the child process. This is a no-op if the pidfile
799 * is NULL and print_pid_pipe is NULL.
801 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
806 _DBUS_ASSERT_ERROR_IS_SET (error);
812 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
813 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
814 _dbus_pipe_close (print_pid_pipe, NULL);
816 if (!bus_selinux_full_init ())
818 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but AVC initialization failed; check system log\n");
821 if (!process_config_postinit (context, parser, error))
823 _DBUS_ASSERT_ERROR_IS_SET (error);
829 bus_config_parser_unref (parser);
833 /* Here we change our credentials if required,
834 * as soon as we've set up our sockets and pidfile
836 if (context->user != NULL)
838 if (!_dbus_change_to_daemon_user (context->user, error))
840 _DBUS_ASSERT_ERROR_IS_SET (error);
845 /* FIXME - why not just put this in full_init() below? */
846 bus_selinux_audit_init ();
850 dbus_server_free_data_slot (&server_data_slot);
856 bus_config_parser_unref (parser);
858 bus_context_unref (context);
860 if (server_data_slot >= 0)
861 dbus_server_free_data_slot (&server_data_slot);
867 bus_context_get_id (BusContext *context,
870 return _dbus_uuid_encode (&context->uuid, uuid);
874 bus_context_reload_config (BusContext *context,
877 BusConfigParser *parser;
878 DBusString config_file;
881 /* Flush the user database cache */
882 _dbus_flush_caches ();
885 _dbus_string_init_const (&config_file, context->config_file);
886 parser = bus_config_load (&config_file, TRUE, NULL, error);
889 _DBUS_ASSERT_ERROR_IS_SET (error);
893 if (!process_config_every_time (context, parser, TRUE, error))
895 _DBUS_ASSERT_ERROR_IS_SET (error);
898 if (!process_config_postinit (context, parser, error))
900 _DBUS_ASSERT_ERROR_IS_SET (error);
905 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
908 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
910 bus_config_parser_unref (parser);
915 shutdown_server (BusContext *context,
918 if (server == NULL ||
919 !dbus_server_get_is_connected (server))
922 if (!dbus_server_set_watch_functions (server,
926 _dbus_assert_not_reached ("setting watch functions to NULL failed");
928 if (!dbus_server_set_timeout_functions (server,
932 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
934 dbus_server_disconnect (server);
938 bus_context_shutdown (BusContext *context)
942 link = _dbus_list_get_first_link (&context->servers);
945 shutdown_server (context, link->data);
947 link = _dbus_list_get_next_link (&context->servers, link);
952 bus_context_ref (BusContext *context)
954 _dbus_assert (context->refcount > 0);
955 context->refcount += 1;
961 bus_context_unref (BusContext *context)
963 _dbus_assert (context->refcount > 0);
964 context->refcount -= 1;
966 if (context->refcount == 0)
970 _dbus_verbose ("Finalizing bus context %p\n", context);
972 bus_context_shutdown (context);
974 if (context->connections)
976 bus_connections_unref (context->connections);
977 context->connections = NULL;
980 if (context->registry)
982 bus_registry_unref (context->registry);
983 context->registry = NULL;
986 if (context->activation)
988 bus_activation_unref (context->activation);
989 context->activation = NULL;
992 link = _dbus_list_get_first_link (&context->servers);
995 dbus_server_unref (link->data);
997 link = _dbus_list_get_next_link (&context->servers, link);
999 _dbus_list_clear (&context->servers);
1001 if (context->policy)
1003 bus_policy_unref (context->policy);
1004 context->policy = NULL;
1009 _dbus_loop_unref (context->loop);
1010 context->loop = NULL;
1013 if (context->matchmaker)
1015 bus_matchmaker_unref (context->matchmaker);
1016 context->matchmaker = NULL;
1019 dbus_free (context->config_file);
1020 dbus_free (context->log_prefix);
1021 dbus_free (context->type);
1022 dbus_free (context->address);
1023 dbus_free (context->user);
1024 dbus_free (context->servicehelper);
1026 if (context->pidfile)
1029 _dbus_string_init_const (&u, context->pidfile);
1031 /* Deliberately ignore errors here, since there's not much
1032 * we can do about it, and we're exiting anyways.
1034 _dbus_delete_file (&u, NULL);
1036 dbus_free (context->pidfile);
1038 dbus_free (context);
1040 dbus_server_free_data_slot (&server_data_slot);
1044 /* type may be NULL */
1046 bus_context_get_type (BusContext *context)
1048 return context->type;
1052 bus_context_get_address (BusContext *context)
1054 return context->address;
1058 bus_context_get_servicehelper (BusContext *context)
1060 return context->servicehelper;
1064 bus_context_get_registry (BusContext *context)
1066 return context->registry;
1070 bus_context_get_connections (BusContext *context)
1072 return context->connections;
1076 bus_context_get_activation (BusContext *context)
1078 return context->activation;
1082 bus_context_get_matchmaker (BusContext *context)
1084 return context->matchmaker;
1088 bus_context_get_loop (BusContext *context)
1090 return context->loop;
1094 bus_context_allow_unix_user (BusContext *context,
1097 return bus_policy_allow_unix_user (context->policy,
1101 /* For now this is never actually called because the default
1102 * DBusConnection behavior of 'same user that owns the bus can connect'
1103 * is all it would do.
1106 bus_context_allow_windows_user (BusContext *context,
1107 const char *windows_sid)
1109 return bus_policy_allow_windows_user (context->policy,
1114 bus_context_get_policy (BusContext *context)
1116 return context->policy;
1120 bus_context_create_client_policy (BusContext *context,
1121 DBusConnection *connection,
1124 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1125 return bus_policy_create_client_policy (context->policy, connection,
1130 bus_context_get_activation_timeout (BusContext *context)
1133 return context->limits.activation_timeout;
1137 bus_context_get_auth_timeout (BusContext *context)
1139 return context->limits.auth_timeout;
1143 bus_context_get_max_completed_connections (BusContext *context)
1145 return context->limits.max_completed_connections;
1149 bus_context_get_max_incomplete_connections (BusContext *context)
1151 return context->limits.max_incomplete_connections;
1155 bus_context_get_max_connections_per_user (BusContext *context)
1157 return context->limits.max_connections_per_user;
1161 bus_context_get_max_pending_activations (BusContext *context)
1163 return context->limits.max_pending_activations;
1167 bus_context_get_max_services_per_connection (BusContext *context)
1169 return context->limits.max_services_per_connection;
1173 bus_context_get_max_match_rules_per_connection (BusContext *context)
1175 return context->limits.max_match_rules_per_connection;
1179 bus_context_get_max_replies_per_connection (BusContext *context)
1181 return context->limits.max_replies_per_connection;
1185 bus_context_get_reply_timeout (BusContext *context)
1187 return context->limits.reply_timeout;
1191 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
1194 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1198 if (!context->syslog)
1201 va_start (args, msg);
1203 if (context->log_prefix)
1205 DBusString full_msg;
1207 if (!_dbus_string_init (&full_msg))
1209 if (!_dbus_string_append (&full_msg, context->log_prefix))
1211 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1214 _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1216 _dbus_string_free (&full_msg);
1219 _dbus_system_logv (severity, msg, args);
1226 * addressed_recipient is the recipient specified in the message.
1228 * proposed_recipient is the recipient we're considering sending
1229 * to right this second, and may be an eavesdropper.
1231 * sender is the sender of the message.
1233 * NULL for proposed_recipient or sender definitely means the bus driver.
1235 * NULL for addressed_recipient may mean the bus driver, or may mean
1236 * no destination was specified in the message (e.g. a signal).
1239 bus_context_check_security_policy (BusContext *context,
1240 BusTransaction *transaction,
1241 DBusConnection *sender,
1242 DBusConnection *addressed_recipient,
1243 DBusConnection *proposed_recipient,
1244 DBusMessage *message,
1248 BusClientPolicy *sender_policy;
1249 BusClientPolicy *recipient_policy;
1250 dbus_int32_t toggles;
1253 dbus_bool_t requested_reply;
1254 const char *sender_name;
1255 const char *sender_loginfo;
1256 const char *proposed_recipient_loginfo;
1258 type = dbus_message_get_type (message);
1259 dest = dbus_message_get_destination (message);
1261 /* dispatch.c was supposed to ensure these invariants */
1262 _dbus_assert (dest != NULL ||
1263 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1264 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1265 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1266 addressed_recipient != NULL ||
1267 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1269 /* Used in logging below */
1272 sender_name = bus_connection_get_name (sender);
1273 sender_loginfo = bus_connection_get_loginfo (sender);
1278 sender_loginfo = "(bus)";
1281 if (proposed_recipient != NULL)
1282 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1284 proposed_recipient_loginfo = "bus";
1288 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1289 case DBUS_MESSAGE_TYPE_SIGNAL:
1290 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1291 case DBUS_MESSAGE_TYPE_ERROR:
1295 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1298 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1299 "Message bus will not accept messages of unknown type\n");
1304 requested_reply = FALSE;
1308 /* First verify the SELinux access controls. If allowed then
1309 * go on with the standard checks.
1311 if (!bus_selinux_allows_send (sender, proposed_recipient,
1312 dbus_message_type_to_string (dbus_message_get_type (message)),
1313 dbus_message_get_interface (message),
1314 dbus_message_get_member (message),
1315 dbus_message_get_error_name (message),
1316 dest ? dest : DBUS_SERVICE_DBUS, error))
1318 if (error != NULL && !dbus_error_is_set (error))
1320 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1321 "An SELinux policy prevents this sender "
1322 "from sending this message to this recipient "
1323 "(rejected message had sender \"%s\" interface \"%s\" "
1324 "member \"%s\" error name \"%s\" destination \"%s\")",
1325 sender_name ? sender_name : "(unset)",
1326 dbus_message_get_interface (message) ?
1327 dbus_message_get_interface (message) : "(unset)",
1328 dbus_message_get_member (message) ?
1329 dbus_message_get_member (message) : "(unset)",
1330 dbus_message_get_error_name (message) ?
1331 dbus_message_get_error_name (message) : "(unset)",
1332 dest ? dest : DBUS_SERVICE_DBUS);
1333 _dbus_verbose ("SELinux security check denying send to service\n");
1339 if (bus_connection_is_active (sender))
1341 sender_policy = bus_connection_get_policy (sender);
1342 _dbus_assert (sender_policy != NULL);
1344 /* Fill in requested_reply variable with TRUE if this is a
1345 * reply and the reply was pending.
1347 if (dbus_message_get_reply_serial (message) != 0)
1349 if (proposed_recipient != NULL /* not to the bus driver */ &&
1350 addressed_recipient == proposed_recipient /* not eavesdropping */)
1354 dbus_error_init (&error2);
1355 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1357 sender, addressed_recipient, message,
1359 if (dbus_error_is_set (&error2))
1361 dbus_move_error (&error2, error);
1369 /* Policy for inactive connections is that they can only send
1370 * the hello message to the bus driver
1372 if (proposed_recipient == NULL &&
1373 dbus_message_is_method_call (message,
1374 DBUS_INTERFACE_DBUS,
1377 _dbus_verbose ("security check allowing %s message\n",
1383 _dbus_verbose ("security check disallowing non-%s message\n",
1386 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1387 "Client tried to send a message other than %s without being registered",
1396 sender_policy = NULL;
1398 /* If the sender is the bus driver, we assume any reply was a
1399 * requested reply as bus driver won't send bogus ones
1401 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1402 dbus_message_get_reply_serial (message) != 0)
1403 requested_reply = TRUE;
1406 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1407 (sender == NULL && sender_policy == NULL));
1409 if (proposed_recipient != NULL)
1411 /* only the bus driver can send to an inactive recipient (as it
1412 * owns no services, so other apps can't address it). Inactive
1413 * recipients can receive any message.
1415 if (bus_connection_is_active (proposed_recipient))
1417 recipient_policy = bus_connection_get_policy (proposed_recipient);
1418 _dbus_assert (recipient_policy != NULL);
1420 else if (sender == NULL)
1422 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1423 recipient_policy = NULL;
1427 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1428 recipient_policy = NULL;
1432 recipient_policy = NULL;
1434 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1435 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1436 (proposed_recipient == NULL && recipient_policy == NULL));
1439 if (sender_policy &&
1440 !bus_client_policy_check_can_send (sender_policy,
1444 message, &toggles, &log))
1446 const char *msg = "Rejected send message, %d matched rules; "
1447 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))";
1449 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1451 dbus_message_type_to_string (dbus_message_get_type (message)),
1452 sender_name ? sender_name : "(unset)",
1454 dbus_message_get_interface (message) ?
1455 dbus_message_get_interface (message) : "(unset)",
1456 dbus_message_get_member (message) ?
1457 dbus_message_get_member (message) : "(unset)",
1458 dbus_message_get_error_name (message) ?
1459 dbus_message_get_error_name (message) : "(unset)",
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 (context, DBUS_SYSTEM_LOG_SECURITY, 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)",
1477 dest ? dest : DBUS_SERVICE_DBUS,
1478 proposed_recipient_loginfo);
1479 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1484 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY,
1485 "Would reject message, %d matched rules; "
1486 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))",
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)",
1498 dest ? dest : DBUS_SERVICE_DBUS,
1499 proposed_recipient_loginfo);
1501 if (recipient_policy &&
1502 !bus_client_policy_check_can_receive (recipient_policy,
1506 addressed_recipient, proposed_recipient,
1509 const char *msg = "Rejected receive message, %d matched rules; "
1510 "type=\"%s\" sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" reply serial=%u requested_reply=%d destination=\"%s\" (%s))";
1512 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1514 dbus_message_type_to_string (dbus_message_get_type (message)),
1515 sender_name ? sender_name : "(unset)",
1517 dbus_message_get_interface (message) ?
1518 dbus_message_get_interface (message) : "(unset)",
1519 dbus_message_get_member (message) ?
1520 dbus_message_get_member (message) : "(unset)",
1521 dbus_message_get_error_name (message) ?
1522 dbus_message_get_error_name (message) : "(unset)",
1523 dbus_message_get_reply_serial (message),
1525 dest ? dest : DBUS_SERVICE_DBUS,
1526 proposed_recipient_loginfo);
1527 /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1528 if (addressed_recipient == proposed_recipient)
1529 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, msg,
1531 dbus_message_type_to_string (dbus_message_get_type (message)),
1532 sender_name ? sender_name : "(unset)",
1534 dbus_message_get_interface (message) ?
1535 dbus_message_get_interface (message) : "(unset)",
1536 dbus_message_get_member (message) ?
1537 dbus_message_get_member (message) : "(unset)",
1538 dbus_message_get_error_name (message) ?
1539 dbus_message_get_error_name (message) : "(unset)",
1540 dbus_message_get_reply_serial (message),
1542 dest ? dest : DBUS_SERVICE_DBUS,
1543 proposed_recipient_loginfo);
1544 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1548 /* See if limits on size have been exceeded */
1549 if (proposed_recipient &&
1550 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1551 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1553 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1554 "The destination service \"%s\" has a full message queue",
1555 dest ? dest : (proposed_recipient ?
1556 bus_connection_get_name (proposed_recipient) :
1557 DBUS_SERVICE_DBUS));
1558 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1562 /* Record that we will allow a reply here in the future (don't
1563 * bother if the recipient is the bus or this is an eavesdropping
1564 * connection). Only the addressed recipient may reply.
1566 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1568 addressed_recipient &&
1569 addressed_recipient == proposed_recipient && /* not eavesdropping */
1570 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1572 sender, addressed_recipient,
1575 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1579 _dbus_verbose ("security policy allowing message\n");