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-credentials.h>
37 #include <dbus/dbus-internals.h>
52 BusConnections *connections;
53 BusActivation *activation;
54 BusRegistry *registry;
56 BusMatchmaker *matchmaker;
58 unsigned int fork : 1;
59 unsigned int syslog : 1;
60 unsigned int keep_umask : 1;
61 unsigned int allow_anonymous : 1;
64 static dbus_int32_t server_data_slot = -1;
71 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
74 server_get_context (DBusServer *server)
79 if (!dbus_server_allocate_data_slot (&server_data_slot))
82 bd = BUS_SERVER_DATA (server);
85 dbus_server_free_data_slot (&server_data_slot);
89 context = bd->context;
91 dbus_server_free_data_slot (&server_data_slot);
97 server_watch_callback (DBusWatch *watch,
98 unsigned int condition,
101 /* FIXME this can be done in dbus-mainloop.c
102 * if the code in activation.c for the babysitter
103 * watch handler is fixed.
106 return dbus_watch_handle (watch, condition);
110 add_server_watch (DBusWatch *watch,
113 DBusServer *server = data;
116 context = server_get_context (server);
118 return _dbus_loop_add_watch (context->loop,
119 watch, server_watch_callback, server,
124 remove_server_watch (DBusWatch *watch,
127 DBusServer *server = data;
130 context = server_get_context (server);
132 _dbus_loop_remove_watch (context->loop,
133 watch, server_watch_callback, server);
138 server_timeout_callback (DBusTimeout *timeout,
141 /* can return FALSE on OOM but we just let it fire again later */
142 dbus_timeout_handle (timeout);
146 add_server_timeout (DBusTimeout *timeout,
149 DBusServer *server = data;
152 context = server_get_context (server);
154 return _dbus_loop_add_timeout (context->loop,
155 timeout, server_timeout_callback, server, NULL);
159 remove_server_timeout (DBusTimeout *timeout,
162 DBusServer *server = data;
165 context = server_get_context (server);
167 _dbus_loop_remove_timeout (context->loop,
168 timeout, server_timeout_callback, server);
172 new_connection_callback (DBusServer *server,
173 DBusConnection *new_connection,
176 BusContext *context = data;
178 if (!bus_connections_setup_connection (context->connections, new_connection))
180 _dbus_verbose ("No memory to setup new connection\n");
182 /* if we don't do this, it will get unref'd without
183 * being disconnected... kind of strange really
184 * that we have to do this, people won't get it right
187 dbus_connection_close (new_connection);
190 dbus_connection_set_max_received_size (new_connection,
191 context->limits.max_incoming_bytes);
193 dbus_connection_set_max_message_size (new_connection,
194 context->limits.max_message_size);
196 dbus_connection_set_max_received_unix_fds (new_connection,
197 context->limits.max_incoming_unix_fds);
199 dbus_connection_set_max_message_unix_fds (new_connection,
200 context->limits.max_message_unix_fds);
202 dbus_connection_set_allow_anonymous (new_connection,
203 context->allow_anonymous);
205 /* on OOM, we won't have ref'd the connection so it will die. */
209 free_server_data (void *data)
211 BusServerData *bd = data;
217 setup_server (BusContext *context,
219 char **auth_mechanisms,
224 bd = dbus_new0 (BusServerData, 1);
225 if (bd == NULL || !dbus_server_set_data (server,
227 bd, free_server_data))
234 bd->context = context;
236 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
242 dbus_server_set_new_connection_function (server,
243 new_connection_callback,
246 if (!dbus_server_set_watch_functions (server,
257 if (!dbus_server_set_timeout_functions (server,
259 remove_server_timeout,
270 /* This code only gets executed the first time the
271 * config files are parsed. It is not executed
272 * when config files are reloaded.
275 process_config_first_time_only (BusContext *context,
276 BusConfigParser *parser,
279 DBusString log_prefix;
281 DBusList **addresses;
282 const char *user, *pidfile;
283 char **auth_mechanisms;
284 DBusList **auth_mechanisms_list;
288 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
291 auth_mechanisms = NULL;
293 /* Check for an existing pid file. Of course this is a race;
294 * we'd have to use fcntl() locks on the pid file to
295 * avoid that. But we want to check for the pid file
296 * before overwriting any existing sockets, etc.
298 pidfile = bus_config_parser_get_pidfile (parser);
304 _dbus_string_init_const (&u, pidfile);
306 if (_dbus_stat (&u, &stbuf, NULL))
308 dbus_set_error (error, DBUS_ERROR_FAILED,
309 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
315 /* keep around the pid filename so we can delete it later */
316 context->pidfile = _dbus_strdup (pidfile);
318 /* note that type may be NULL */
319 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
320 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
323 user = bus_config_parser_get_user (parser);
326 context->user = _dbus_strdup (user);
327 if (context->user == NULL)
331 /* Set up the prefix for syslog messages */
332 if (!_dbus_string_init (&log_prefix))
334 if (context->type && !strcmp (context->type, "system"))
336 if (!_dbus_string_append (&log_prefix, "[system] "))
339 else if (context->type && !strcmp (context->type, "session"))
341 DBusCredentials *credentials;
343 credentials = _dbus_credentials_new_from_current_process ();
346 if (!_dbus_string_append (&log_prefix, "[session "))
348 if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
350 if (!_dbus_string_append (&log_prefix, "] "))
352 _dbus_credentials_unref (credentials);
354 if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
356 _dbus_string_free (&log_prefix);
358 /* Build an array of auth mechanisms */
360 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
361 len = _dbus_list_get_length (auth_mechanisms_list);
367 auth_mechanisms = dbus_new0 (char*, len + 1);
368 if (auth_mechanisms == NULL)
372 link = _dbus_list_get_first_link (auth_mechanisms_list);
375 auth_mechanisms[i] = _dbus_strdup (link->data);
376 if (auth_mechanisms[i] == NULL)
378 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
383 auth_mechanisms = NULL;
386 /* Listen on our addresses */
388 addresses = bus_config_parser_get_addresses (parser);
390 link = _dbus_list_get_first_link (addresses);
395 server = dbus_server_listen (link->data, error);
398 _DBUS_ASSERT_ERROR_IS_SET (error);
401 else if (!setup_server (context, server, auth_mechanisms, error))
403 _DBUS_ASSERT_ERROR_IS_SET (error);
407 if (!_dbus_list_append (&context->servers, server))
410 link = _dbus_list_get_next_link (addresses, link);
413 context->fork = bus_config_parser_get_fork (parser);
414 context->syslog = bus_config_parser_get_syslog (parser);
415 context->keep_umask = bus_config_parser_get_keep_umask (parser);
416 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
418 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
422 dbus_free_string_array (auth_mechanisms);
427 dbus_free_string_array (auth_mechanisms);
431 /* This code gets executed every time the config files
432 * are parsed: both during BusContext construction
433 * and on reloads. This function is slightly screwy
434 * since it can do a "half reload" in out-of-memory
435 * situations. Realistically, unlikely to ever matter.
438 process_config_every_time (BusContext *context,
439 BusConfigParser *parser,
440 dbus_bool_t is_reload,
443 DBusString full_address;
446 BusActivation *new_activation;
448 const char *servicehelper;
453 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
458 if (!_dbus_string_init (&full_address))
464 /* get our limits and timeout lengths */
465 bus_config_parser_get_limits (parser, &context->limits);
468 bus_policy_unref (context->policy);
469 context->policy = bus_config_parser_steal_policy (parser);
470 _dbus_assert (context->policy != NULL);
472 /* We have to build the address backward, so that
473 * <listen> later in the config file have priority
475 link = _dbus_list_get_last_link (&context->servers);
478 addr = dbus_server_get_address (link->data);
485 if (_dbus_string_get_length (&full_address) > 0)
487 if (!_dbus_string_append (&full_address, ";"))
494 if (!_dbus_string_append (&full_address, addr))
503 link = _dbus_list_get_prev_link (&context->servers, link);
507 dbus_free (context->address);
509 if (!_dbus_string_copy_data (&full_address, &context->address))
515 /* get the service directories */
516 dirs = bus_config_parser_get_service_dirs (parser);
518 /* and the service helper */
519 servicehelper = bus_config_parser_get_servicehelper (parser);
521 s = _dbus_strdup(servicehelper);
522 if (s == NULL && servicehelper != NULL)
529 dbus_free(context->servicehelper);
530 context->servicehelper = s;
533 /* Create activation subsystem */
534 if (context->activation)
536 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
541 context->activation = bus_activation_new (context, &full_address, dirs, error);
544 if (context->activation == NULL)
546 _DBUS_ASSERT_ERROR_IS_SET (error);
550 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
554 _dbus_string_free (&full_address);
563 list_concat_new (DBusList **a,
571 link = _dbus_list_get_first_link (a);
572 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
574 if (!_dbus_list_append (result, link->data))
577 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
579 if (!_dbus_list_append (result, link->data))
585 _dbus_list_clear (result);
590 process_config_postinit (BusContext *context,
591 BusConfigParser *parser,
594 DBusHashTable *service_context_table;
595 DBusList *watched_dirs = NULL;
597 service_context_table = bus_config_parser_steal_service_context_table (parser);
598 if (!bus_registry_set_service_context_table (context->registry,
599 service_context_table))
605 _dbus_hash_table_unref (service_context_table);
607 /* We need to monitor both the configuration directories and directories
608 * containing .service files.
610 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
611 bus_config_parser_get_service_dirs (parser),
618 bus_set_watched_dirs (context, &watched_dirs);
620 _dbus_list_clear (&watched_dirs);
626 bus_context_new (const DBusString *config_file,
627 ForceForkSetting force_fork,
628 DBusPipe *print_addr_pipe,
629 DBusPipe *print_pid_pipe,
632 DBusString log_prefix;
634 BusConfigParser *parser;
636 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
641 if (!dbus_server_allocate_data_slot (&server_data_slot))
647 context = dbus_new0 (BusContext, 1);
653 context->refcount = 1;
655 _dbus_generate_uuid (&context->uuid);
657 if (!_dbus_string_copy_data (config_file, &context->config_file))
663 context->loop = _dbus_loop_new ();
664 if (context->loop == NULL)
670 context->registry = bus_registry_new (context);
671 if (context->registry == NULL)
677 parser = bus_config_load (config_file, TRUE, NULL, error);
680 _DBUS_ASSERT_ERROR_IS_SET (error);
684 if (!process_config_first_time_only (context, parser, error))
686 _DBUS_ASSERT_ERROR_IS_SET (error);
689 if (!process_config_every_time (context, parser, FALSE, error))
691 _DBUS_ASSERT_ERROR_IS_SET (error);
695 /* we need another ref of the server data slot for the context
698 if (!dbus_server_allocate_data_slot (&server_data_slot))
699 _dbus_assert_not_reached ("second ref of server data slot failed");
701 /* Note that we don't know whether the print_addr_pipe is
702 * one of the sockets we're using to listen on, or some
703 * other random thing. But I think the answer is "don't do
706 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
709 const char *a = bus_context_get_address (context);
712 _dbus_assert (a != NULL);
713 if (!_dbus_string_init (&addr))
719 if (!_dbus_string_append (&addr, a) ||
720 !_dbus_string_append (&addr, "\n"))
722 _dbus_string_free (&addr);
727 bytes = _dbus_string_get_length (&addr);
728 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
730 /* pipe write returns an error on failure but not short write */
731 if (error != NULL && !dbus_error_is_set (error))
733 dbus_set_error (error, DBUS_ERROR_FAILED,
734 "Printing message bus address: did not write all bytes\n");
736 _dbus_string_free (&addr);
740 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
741 _dbus_pipe_close (print_addr_pipe, NULL);
743 _dbus_string_free (&addr);
746 context->connections = bus_connections_new (context);
747 if (context->connections == NULL)
753 context->matchmaker = bus_matchmaker_new ();
754 if (context->matchmaker == NULL)
760 /* check user before we fork */
761 if (context->user != NULL)
763 if (!_dbus_verify_daemon_user (context->user))
765 dbus_set_error (error, DBUS_ERROR_FAILED,
766 "Could not get UID and GID for username \"%s\"",
772 /* Now become a daemon if appropriate and write out pid file in any case */
776 if (context->pidfile)
777 _dbus_string_init_const (&u, context->pidfile);
779 if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
781 _dbus_verbose ("Forking and becoming daemon\n");
783 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
786 context->keep_umask))
788 _DBUS_ASSERT_ERROR_IS_SET (error);
794 _dbus_verbose ("Fork not requested\n");
796 /* Need to write PID file and to PID pipe for ourselves,
797 * not for the child process. This is a no-op if the pidfile
798 * is NULL and print_pid_pipe is NULL.
800 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
805 _DBUS_ASSERT_ERROR_IS_SET (error);
811 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
812 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
813 _dbus_pipe_close (print_pid_pipe, NULL);
815 if (!bus_selinux_full_init ())
817 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but AVC initialization failed; check system log\n");
820 if (!process_config_postinit (context, parser, error))
822 _DBUS_ASSERT_ERROR_IS_SET (error);
828 bus_config_parser_unref (parser);
832 /* Here we change our credentials if required,
833 * as soon as we've set up our sockets and pidfile
835 if (context->user != NULL)
837 if (!_dbus_change_to_daemon_user (context->user, error))
839 _DBUS_ASSERT_ERROR_IS_SET (error);
844 /* FIXME - why not just put this in full_init() below? */
845 bus_selinux_audit_init ();
849 dbus_server_free_data_slot (&server_data_slot);
855 bus_config_parser_unref (parser);
857 bus_context_unref (context);
859 if (server_data_slot >= 0)
860 dbus_server_free_data_slot (&server_data_slot);
866 bus_context_get_id (BusContext *context,
869 return _dbus_uuid_encode (&context->uuid, uuid);
873 bus_context_reload_config (BusContext *context,
876 BusConfigParser *parser;
877 DBusString config_file;
880 /* Flush the user database cache */
881 _dbus_flush_caches ();
884 _dbus_string_init_const (&config_file, context->config_file);
885 parser = bus_config_load (&config_file, TRUE, NULL, error);
888 _DBUS_ASSERT_ERROR_IS_SET (error);
892 if (!process_config_every_time (context, parser, TRUE, error))
894 _DBUS_ASSERT_ERROR_IS_SET (error);
897 if (!process_config_postinit (context, parser, error))
899 _DBUS_ASSERT_ERROR_IS_SET (error);
904 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
907 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
909 bus_config_parser_unref (parser);
914 shutdown_server (BusContext *context,
917 if (server == NULL ||
918 !dbus_server_get_is_connected (server))
921 if (!dbus_server_set_watch_functions (server,
925 _dbus_assert_not_reached ("setting watch functions to NULL failed");
927 if (!dbus_server_set_timeout_functions (server,
931 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
933 dbus_server_disconnect (server);
937 bus_context_shutdown (BusContext *context)
941 link = _dbus_list_get_first_link (&context->servers);
944 shutdown_server (context, link->data);
946 link = _dbus_list_get_next_link (&context->servers, link);
951 bus_context_ref (BusContext *context)
953 _dbus_assert (context->refcount > 0);
954 context->refcount += 1;
960 bus_context_unref (BusContext *context)
962 _dbus_assert (context->refcount > 0);
963 context->refcount -= 1;
965 if (context->refcount == 0)
969 _dbus_verbose ("Finalizing bus context %p\n", context);
971 bus_context_shutdown (context);
973 if (context->connections)
975 bus_connections_unref (context->connections);
976 context->connections = NULL;
979 if (context->registry)
981 bus_registry_unref (context->registry);
982 context->registry = NULL;
985 if (context->activation)
987 bus_activation_unref (context->activation);
988 context->activation = NULL;
991 link = _dbus_list_get_first_link (&context->servers);
994 dbus_server_unref (link->data);
996 link = _dbus_list_get_next_link (&context->servers, link);
998 _dbus_list_clear (&context->servers);
1000 if (context->policy)
1002 bus_policy_unref (context->policy);
1003 context->policy = NULL;
1008 _dbus_loop_unref (context->loop);
1009 context->loop = NULL;
1012 if (context->matchmaker)
1014 bus_matchmaker_unref (context->matchmaker);
1015 context->matchmaker = NULL;
1018 dbus_free (context->config_file);
1019 dbus_free (context->log_prefix);
1020 dbus_free (context->type);
1021 dbus_free (context->address);
1022 dbus_free (context->user);
1023 dbus_free (context->servicehelper);
1025 if (context->pidfile)
1028 _dbus_string_init_const (&u, context->pidfile);
1030 /* Deliberately ignore errors here, since there's not much
1031 * we can do about it, and we're exiting anyways.
1033 _dbus_delete_file (&u, NULL);
1035 dbus_free (context->pidfile);
1037 dbus_free (context);
1039 dbus_server_free_data_slot (&server_data_slot);
1043 /* type may be NULL */
1045 bus_context_get_type (BusContext *context)
1047 return context->type;
1051 bus_context_get_address (BusContext *context)
1053 return context->address;
1057 bus_context_get_servicehelper (BusContext *context)
1059 return context->servicehelper;
1063 bus_context_get_registry (BusContext *context)
1065 return context->registry;
1069 bus_context_get_connections (BusContext *context)
1071 return context->connections;
1075 bus_context_get_activation (BusContext *context)
1077 return context->activation;
1081 bus_context_get_matchmaker (BusContext *context)
1083 return context->matchmaker;
1087 bus_context_get_loop (BusContext *context)
1089 return context->loop;
1093 bus_context_allow_unix_user (BusContext *context,
1096 return bus_policy_allow_unix_user (context->policy,
1100 /* For now this is never actually called because the default
1101 * DBusConnection behavior of 'same user that owns the bus can connect'
1102 * is all it would do.
1105 bus_context_allow_windows_user (BusContext *context,
1106 const char *windows_sid)
1108 return bus_policy_allow_windows_user (context->policy,
1113 bus_context_get_policy (BusContext *context)
1115 return context->policy;
1119 bus_context_create_client_policy (BusContext *context,
1120 DBusConnection *connection,
1123 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1124 return bus_policy_create_client_policy (context->policy, connection,
1129 bus_context_get_activation_timeout (BusContext *context)
1132 return context->limits.activation_timeout;
1136 bus_context_get_auth_timeout (BusContext *context)
1138 return context->limits.auth_timeout;
1142 bus_context_get_max_completed_connections (BusContext *context)
1144 return context->limits.max_completed_connections;
1148 bus_context_get_max_incomplete_connections (BusContext *context)
1150 return context->limits.max_incomplete_connections;
1154 bus_context_get_max_connections_per_user (BusContext *context)
1156 return context->limits.max_connections_per_user;
1160 bus_context_get_max_pending_activations (BusContext *context)
1162 return context->limits.max_pending_activations;
1166 bus_context_get_max_services_per_connection (BusContext *context)
1168 return context->limits.max_services_per_connection;
1172 bus_context_get_max_match_rules_per_connection (BusContext *context)
1174 return context->limits.max_match_rules_per_connection;
1178 bus_context_get_max_replies_per_connection (BusContext *context)
1180 return context->limits.max_replies_per_connection;
1184 bus_context_get_reply_timeout (BusContext *context)
1186 return context->limits.reply_timeout;
1190 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1194 if (!context->syslog)
1197 va_start (args, msg);
1199 if (context->log_prefix)
1201 DBusString full_msg;
1203 if (!_dbus_string_init (&full_msg))
1205 if (!_dbus_string_append (&full_msg, context->log_prefix))
1207 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1210 _dbus_system_log (severity, "%s", full_msg);
1212 _dbus_string_free (&full_msg);
1215 _dbus_system_logv (severity, msg, args);
1222 * addressed_recipient is the recipient specified in the message.
1224 * proposed_recipient is the recipient we're considering sending
1225 * to right this second, and may be an eavesdropper.
1227 * sender is the sender of the message.
1229 * NULL for proposed_recipient or sender definitely means the bus driver.
1231 * NULL for addressed_recipient may mean the bus driver, or may mean
1232 * no destination was specified in the message (e.g. a signal).
1235 bus_context_check_security_policy (BusContext *context,
1236 BusTransaction *transaction,
1237 DBusConnection *sender,
1238 DBusConnection *addressed_recipient,
1239 DBusConnection *proposed_recipient,
1240 DBusMessage *message,
1244 BusClientPolicy *sender_policy;
1245 BusClientPolicy *recipient_policy;
1246 dbus_int32_t toggles;
1249 dbus_bool_t requested_reply;
1250 const char *sender_name;
1251 const char *sender_loginfo;
1252 const char *proposed_recipient_loginfo;
1254 type = dbus_message_get_type (message);
1255 dest = dbus_message_get_destination (message);
1257 /* dispatch.c was supposed to ensure these invariants */
1258 _dbus_assert (dest != NULL ||
1259 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1260 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1261 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1262 addressed_recipient != NULL ||
1263 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1265 /* Used in logging below */
1268 sender_name = bus_connection_get_name (sender);
1269 sender_loginfo = bus_connection_get_loginfo (sender);
1274 sender_loginfo = "(bus)";
1277 if (proposed_recipient != NULL)
1278 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1280 proposed_recipient_loginfo = "bus";
1284 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1285 case DBUS_MESSAGE_TYPE_SIGNAL:
1286 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1287 case DBUS_MESSAGE_TYPE_ERROR:
1291 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1294 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1295 "Message bus will not accept messages of unknown type\n");
1300 requested_reply = FALSE;
1304 /* First verify the SELinux access controls. If allowed then
1305 * go on with the standard checks.
1307 if (!bus_selinux_allows_send (sender, proposed_recipient,
1308 dbus_message_type_to_string (dbus_message_get_type (message)),
1309 dbus_message_get_interface (message),
1310 dbus_message_get_member (message),
1311 dbus_message_get_error_name (message),
1312 dest ? dest : DBUS_SERVICE_DBUS, error))
1314 if (error != NULL && !dbus_error_is_set (error))
1316 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1317 "An SELinux policy prevents this sender "
1318 "from sending this message to this recipient "
1319 "(rejected message had sender \"%s\" interface \"%s\" "
1320 "member \"%s\" error name \"%s\" destination \"%s\")",
1321 sender_name ? sender_name : "(unset)",
1322 dbus_message_get_interface (message) ?
1323 dbus_message_get_interface (message) : "(unset)",
1324 dbus_message_get_member (message) ?
1325 dbus_message_get_member (message) : "(unset)",
1326 dbus_message_get_error_name (message) ?
1327 dbus_message_get_error_name (message) : "(unset)",
1328 dest ? dest : DBUS_SERVICE_DBUS);
1329 _dbus_verbose ("SELinux security check denying send to service\n");
1335 if (bus_connection_is_active (sender))
1337 sender_policy = bus_connection_get_policy (sender);
1338 _dbus_assert (sender_policy != NULL);
1340 /* Fill in requested_reply variable with TRUE if this is a
1341 * reply and the reply was pending.
1343 if (dbus_message_get_reply_serial (message) != 0)
1345 if (proposed_recipient != NULL /* not to the bus driver */ &&
1346 addressed_recipient == proposed_recipient /* not eavesdropping */)
1350 dbus_error_init (&error2);
1351 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1353 sender, addressed_recipient, message,
1355 if (dbus_error_is_set (&error2))
1357 dbus_move_error (&error2, error);
1365 /* Policy for inactive connections is that they can only send
1366 * the hello message to the bus driver
1368 if (proposed_recipient == NULL &&
1369 dbus_message_is_method_call (message,
1370 DBUS_INTERFACE_DBUS,
1373 _dbus_verbose ("security check allowing %s message\n",
1379 _dbus_verbose ("security check disallowing non-%s message\n",
1382 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1383 "Client tried to send a message other than %s without being registered",
1392 sender_policy = NULL;
1394 /* If the sender is the bus driver, we assume any reply was a
1395 * requested reply as bus driver won't send bogus ones
1397 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1398 dbus_message_get_reply_serial (message) != 0)
1399 requested_reply = TRUE;
1402 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1403 (sender == NULL && sender_policy == NULL));
1405 if (proposed_recipient != NULL)
1407 /* only the bus driver can send to an inactive recipient (as it
1408 * owns no services, so other apps can't address it). Inactive
1409 * recipients can receive any message.
1411 if (bus_connection_is_active (proposed_recipient))
1413 recipient_policy = bus_connection_get_policy (proposed_recipient);
1414 _dbus_assert (recipient_policy != NULL);
1416 else if (sender == NULL)
1418 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1419 recipient_policy = NULL;
1423 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1424 recipient_policy = NULL;
1428 recipient_policy = NULL;
1430 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1431 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1432 (proposed_recipient == NULL && recipient_policy == NULL));
1435 if (sender_policy &&
1436 !bus_client_policy_check_can_send (sender_policy,
1440 message, &toggles, &log))
1442 const char *msg = "Rejected send 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_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1447 dbus_message_type_to_string (dbus_message_get_type (message)),
1448 sender_name ? sender_name : "(unset)",
1450 dbus_message_get_interface (message) ?
1451 dbus_message_get_interface (message) : "(unset)",
1452 dbus_message_get_member (message) ?
1453 dbus_message_get_member (message) : "(unset)",
1454 dbus_message_get_error_name (message) ?
1455 dbus_message_get_error_name (message) : "(unset)",
1457 dest ? dest : DBUS_SERVICE_DBUS,
1458 proposed_recipient_loginfo);
1459 /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1460 if (addressed_recipient == proposed_recipient)
1461 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, msg,
1463 dbus_message_type_to_string (dbus_message_get_type (message)),
1464 sender_name ? sender_name : "(unset)",
1466 dbus_message_get_interface (message) ?
1467 dbus_message_get_interface (message) : "(unset)",
1468 dbus_message_get_member (message) ?
1469 dbus_message_get_member (message) : "(unset)",
1470 dbus_message_get_error_name (message) ?
1471 dbus_message_get_error_name (message) : "(unset)",
1473 dest ? dest : DBUS_SERVICE_DBUS,
1474 proposed_recipient_loginfo);
1475 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1480 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY,
1481 "Would reject message, %d matched rules; "
1482 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))",
1484 dbus_message_type_to_string (dbus_message_get_type (message)),
1485 sender_name ? sender_name : "(unset)",
1487 dbus_message_get_interface (message) ?
1488 dbus_message_get_interface (message) : "(unset)",
1489 dbus_message_get_member (message) ?
1490 dbus_message_get_member (message) : "(unset)",
1491 dbus_message_get_error_name (message) ?
1492 dbus_message_get_error_name (message) : "(unset)",
1494 dest ? dest : DBUS_SERVICE_DBUS,
1495 proposed_recipient_loginfo);
1497 if (recipient_policy &&
1498 !bus_client_policy_check_can_receive (recipient_policy,
1502 addressed_recipient, proposed_recipient,
1505 const char *msg = "Rejected receive message, %d matched rules; "
1506 "type=\"%s\" sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" reply serial=%u requested_reply=%d destination=\"%s\" (%s))";
1508 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1510 dbus_message_type_to_string (dbus_message_get_type (message)),
1511 sender_name ? sender_name : "(unset)",
1513 dbus_message_get_interface (message) ?
1514 dbus_message_get_interface (message) : "(unset)",
1515 dbus_message_get_member (message) ?
1516 dbus_message_get_member (message) : "(unset)",
1517 dbus_message_get_error_name (message) ?
1518 dbus_message_get_error_name (message) : "(unset)",
1519 dbus_message_get_reply_serial (message),
1521 dest ? dest : DBUS_SERVICE_DBUS,
1522 proposed_recipient_loginfo);
1523 /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1524 if (addressed_recipient == proposed_recipient)
1525 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, msg,
1527 dbus_message_type_to_string (dbus_message_get_type (message)),
1528 sender_name ? sender_name : "(unset)",
1530 dbus_message_get_interface (message) ?
1531 dbus_message_get_interface (message) : "(unset)",
1532 dbus_message_get_member (message) ?
1533 dbus_message_get_member (message) : "(unset)",
1534 dbus_message_get_error_name (message) ?
1535 dbus_message_get_error_name (message) : "(unset)",
1536 dbus_message_get_reply_serial (message),
1538 dest ? dest : DBUS_SERVICE_DBUS,
1539 proposed_recipient_loginfo);
1540 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1544 /* See if limits on size have been exceeded */
1545 if (proposed_recipient &&
1546 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1547 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1549 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1550 "The destination service \"%s\" has a full message queue",
1551 dest ? dest : (proposed_recipient ?
1552 bus_connection_get_name (proposed_recipient) :
1553 DBUS_SERVICE_DBUS));
1554 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1558 /* Record that we will allow a reply here in the future (don't
1559 * bother if the recipient is the bus or this is an eavesdropping
1560 * connection). Only the addressed recipient may reply.
1562 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1564 addressed_recipient &&
1565 addressed_recipient == proposed_recipient && /* not eavesdropping */
1566 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1568 sender, addressed_recipient,
1571 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1575 _dbus_verbose ("security policy allowing message\n");