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>
56 BusConnections *connections;
57 BusActivation *activation;
58 BusRegistry *registry;
60 BusMatchmaker *matchmaker;
62 unsigned int fork : 1;
63 unsigned int syslog : 1;
64 unsigned int keep_umask : 1;
65 unsigned int allow_anonymous : 1;
66 unsigned int systemd_activation : 1;
69 static dbus_int32_t server_data_slot = -1;
76 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
79 server_get_context (DBusServer *server)
84 if (!dbus_server_allocate_data_slot (&server_data_slot))
87 bd = BUS_SERVER_DATA (server);
90 dbus_server_free_data_slot (&server_data_slot);
94 context = bd->context;
96 dbus_server_free_data_slot (&server_data_slot);
102 server_watch_callback (DBusWatch *watch,
103 unsigned int condition,
106 /* FIXME this can be done in dbus-mainloop.c
107 * if the code in activation.c for the babysitter
108 * watch handler is fixed.
111 return dbus_watch_handle (watch, condition);
115 add_server_watch (DBusWatch *watch,
118 DBusServer *server = data;
121 context = server_get_context (server);
123 return _dbus_loop_add_watch (context->loop,
124 watch, server_watch_callback, server,
129 remove_server_watch (DBusWatch *watch,
132 DBusServer *server = data;
135 context = server_get_context (server);
137 _dbus_loop_remove_watch (context->loop,
138 watch, server_watch_callback, server);
143 server_timeout_callback (DBusTimeout *timeout,
146 /* can return FALSE on OOM but we just let it fire again later */
147 dbus_timeout_handle (timeout);
151 add_server_timeout (DBusTimeout *timeout,
154 DBusServer *server = data;
157 context = server_get_context (server);
159 return _dbus_loop_add_timeout (context->loop,
160 timeout, server_timeout_callback, server, NULL);
164 remove_server_timeout (DBusTimeout *timeout,
167 DBusServer *server = data;
170 context = server_get_context (server);
172 _dbus_loop_remove_timeout (context->loop,
173 timeout, server_timeout_callback, server);
177 new_connection_callback (DBusServer *server,
178 DBusConnection *new_connection,
181 BusContext *context = data;
183 if (!bus_connections_setup_connection (context->connections, new_connection))
185 _dbus_verbose ("No memory to setup new connection\n");
187 /* if we don't do this, it will get unref'd without
188 * being disconnected... kind of strange really
189 * that we have to do this, people won't get it right
192 dbus_connection_close (new_connection);
195 dbus_connection_set_max_received_size (new_connection,
196 context->limits.max_incoming_bytes);
198 dbus_connection_set_max_message_size (new_connection,
199 context->limits.max_message_size);
201 dbus_connection_set_max_received_unix_fds (new_connection,
202 context->limits.max_incoming_unix_fds);
204 dbus_connection_set_max_message_unix_fds (new_connection,
205 context->limits.max_message_unix_fds);
207 dbus_connection_set_allow_anonymous (new_connection,
208 context->allow_anonymous);
210 /* on OOM, we won't have ref'd the connection so it will die. */
214 free_server_data (void *data)
216 BusServerData *bd = data;
222 setup_server (BusContext *context,
224 char **auth_mechanisms,
229 bd = dbus_new0 (BusServerData, 1);
230 if (bd == NULL || !dbus_server_set_data (server,
232 bd, free_server_data))
239 bd->context = context;
241 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
247 dbus_server_set_new_connection_function (server,
248 new_connection_callback,
251 if (!dbus_server_set_watch_functions (server,
262 if (!dbus_server_set_timeout_functions (server,
264 remove_server_timeout,
275 /* This code only gets executed the first time the
276 * config files are parsed. It is not executed
277 * when config files are reloaded.
280 process_config_first_time_only (BusContext *context,
281 BusConfigParser *parser,
282 const DBusString *address,
283 dbus_bool_t systemd_activation,
286 DBusString log_prefix;
288 DBusList **addresses;
289 const char *user, *pidfile;
290 char **auth_mechanisms;
291 DBusList **auth_mechanisms_list;
295 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
298 auth_mechanisms = NULL;
300 context->systemd_activation = systemd_activation;
302 /* Check for an existing pid file. Of course this is a race;
303 * we'd have to use fcntl() locks on the pid file to
304 * avoid that. But we want to check for the pid file
305 * before overwriting any existing sockets, etc.
307 pidfile = bus_config_parser_get_pidfile (parser);
313 _dbus_string_init_const (&u, pidfile);
315 if (_dbus_stat (&u, &stbuf, NULL))
321 _dbus_string_init (&p);
322 _dbus_file_get_contents(&p, &u, NULL);
323 _dbus_string_parse_int(&p, 0, &pid, NULL);
324 _dbus_string_free(&p);
326 if ((kill((int)pid, 0))) {
327 dbus_set_error(NULL, DBUS_ERROR_FILE_EXISTS,
328 "pid %ld not running, removing stale pid file\n",
330 _dbus_delete_file(&u, NULL);
333 dbus_set_error (error, DBUS_ERROR_FAILED,
334 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
343 /* keep around the pid filename so we can delete it later */
344 context->pidfile = _dbus_strdup (pidfile);
346 /* note that type may be NULL */
347 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
348 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
351 user = bus_config_parser_get_user (parser);
354 context->user = _dbus_strdup (user);
355 if (context->user == NULL)
359 /* Set up the prefix for syslog messages */
360 if (!_dbus_string_init (&log_prefix))
362 if (context->type && !strcmp (context->type, "system"))
364 if (!_dbus_string_append (&log_prefix, "[system] "))
367 else if (context->type && !strcmp (context->type, "session"))
369 DBusCredentials *credentials;
371 credentials = _dbus_credentials_new_from_current_process ();
374 if (!_dbus_string_append (&log_prefix, "[session "))
376 _dbus_credentials_unref (credentials);
379 if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
381 _dbus_credentials_unref (credentials);
384 if (!_dbus_string_append (&log_prefix, "] "))
386 _dbus_credentials_unref (credentials);
389 _dbus_credentials_unref (credentials);
391 if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
393 _dbus_string_free (&log_prefix);
395 /* Build an array of auth mechanisms */
397 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
398 len = _dbus_list_get_length (auth_mechanisms_list);
404 auth_mechanisms = dbus_new0 (char*, len + 1);
405 if (auth_mechanisms == NULL)
409 link = _dbus_list_get_first_link (auth_mechanisms_list);
412 auth_mechanisms[i] = _dbus_strdup (link->data);
413 if (auth_mechanisms[i] == NULL)
415 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
420 auth_mechanisms = NULL;
423 /* Listen on our addresses */
429 server = dbus_server_listen (_dbus_string_get_const_data(address), error);
432 _DBUS_ASSERT_ERROR_IS_SET (error);
435 else if (!setup_server (context, server, auth_mechanisms, error))
437 _DBUS_ASSERT_ERROR_IS_SET (error);
441 if (!_dbus_list_append (&context->servers, server))
446 addresses = bus_config_parser_get_addresses (parser);
448 link = _dbus_list_get_first_link (addresses);
453 server = dbus_server_listen (link->data, error);
456 _DBUS_ASSERT_ERROR_IS_SET (error);
459 else if (!setup_server (context, server, auth_mechanisms, error))
461 _DBUS_ASSERT_ERROR_IS_SET (error);
465 if (!_dbus_list_append (&context->servers, server))
468 link = _dbus_list_get_next_link (addresses, link);
472 context->fork = bus_config_parser_get_fork (parser);
473 context->syslog = bus_config_parser_get_syslog (parser);
474 context->keep_umask = bus_config_parser_get_keep_umask (parser);
475 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
477 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
481 dbus_free_string_array (auth_mechanisms);
486 dbus_free_string_array (auth_mechanisms);
490 /* This code gets executed every time the config files
491 * are parsed: both during BusContext construction
492 * and on reloads. This function is slightly screwy
493 * since it can do a "half reload" in out-of-memory
494 * situations. Realistically, unlikely to ever matter.
497 process_config_every_time (BusContext *context,
498 BusConfigParser *parser,
499 dbus_bool_t is_reload,
502 DBusString full_address;
505 BusActivation *new_activation;
507 const char *servicehelper;
512 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
517 if (!_dbus_string_init (&full_address))
523 /* get our limits and timeout lengths */
524 bus_config_parser_get_limits (parser, &context->limits);
527 bus_policy_unref (context->policy);
528 context->policy = bus_config_parser_steal_policy (parser);
529 _dbus_assert (context->policy != NULL);
531 /* We have to build the address backward, so that
532 * <listen> later in the config file have priority
534 link = _dbus_list_get_last_link (&context->servers);
537 addr = dbus_server_get_address (link->data);
544 if (_dbus_string_get_length (&full_address) > 0)
546 if (!_dbus_string_append (&full_address, ";"))
553 if (!_dbus_string_append (&full_address, addr))
562 link = _dbus_list_get_prev_link (&context->servers, link);
566 dbus_free (context->address);
568 if (!_dbus_string_copy_data (&full_address, &context->address))
574 /* get the service directories */
575 dirs = bus_config_parser_get_service_dirs (parser);
577 /* and the service helper */
578 servicehelper = bus_config_parser_get_servicehelper (parser);
580 s = _dbus_strdup(servicehelper);
581 if (s == NULL && servicehelper != NULL)
588 dbus_free(context->servicehelper);
589 context->servicehelper = s;
592 /* Create activation subsystem */
593 if (context->activation)
595 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
600 context->activation = bus_activation_new (context, &full_address, dirs, error);
603 if (context->activation == NULL)
605 _DBUS_ASSERT_ERROR_IS_SET (error);
609 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
613 _dbus_string_free (&full_address);
622 list_concat_new (DBusList **a,
630 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
632 if (!_dbus_list_append (result, link->data))
635 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
637 if (!_dbus_list_append (result, link->data))
643 _dbus_list_clear (result);
648 process_config_postinit (BusContext *context,
649 BusConfigParser *parser,
652 DBusHashTable *service_context_table;
653 DBusList *watched_dirs = NULL;
655 service_context_table = bus_config_parser_steal_service_context_table (parser);
656 if (!bus_registry_set_service_context_table (context->registry,
657 service_context_table))
663 _dbus_hash_table_unref (service_context_table);
665 /* We need to monitor both the configuration directories and directories
666 * containing .service files.
668 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
669 bus_config_parser_get_service_dirs (parser),
676 bus_set_watched_dirs (context, &watched_dirs);
678 _dbus_list_clear (&watched_dirs);
684 bus_context_new (const DBusString *config_file,
685 ForceForkSetting force_fork,
686 DBusPipe *print_addr_pipe,
687 DBusPipe *print_pid_pipe,
688 const DBusString *address,
689 dbus_bool_t systemd_activation,
692 DBusString log_prefix;
694 BusConfigParser *parser;
696 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
701 if (!dbus_server_allocate_data_slot (&server_data_slot))
707 context = dbus_new0 (BusContext, 1);
713 context->refcount = 1;
715 _dbus_generate_uuid (&context->uuid);
717 if (!_dbus_string_copy_data (config_file, &context->config_file))
723 context->loop = _dbus_loop_new ();
724 if (context->loop == NULL)
730 context->registry = bus_registry_new (context);
731 if (context->registry == NULL)
737 parser = bus_config_load (config_file, TRUE, NULL, error);
740 _DBUS_ASSERT_ERROR_IS_SET (error);
744 if (!process_config_first_time_only (context, parser, address, systemd_activation, error))
746 _DBUS_ASSERT_ERROR_IS_SET (error);
749 if (!process_config_every_time (context, parser, FALSE, error))
751 _DBUS_ASSERT_ERROR_IS_SET (error);
755 /* we need another ref of the server data slot for the context
758 if (!dbus_server_allocate_data_slot (&server_data_slot))
759 _dbus_assert_not_reached ("second ref of server data slot failed");
761 /* Note that we don't know whether the print_addr_pipe is
762 * one of the sockets we're using to listen on, or some
763 * other random thing. But I think the answer is "don't do
766 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
769 const char *a = bus_context_get_address (context);
772 _dbus_assert (a != NULL);
773 if (!_dbus_string_init (&addr))
779 if (!_dbus_string_append (&addr, a) ||
780 !_dbus_string_append (&addr, "\n"))
782 _dbus_string_free (&addr);
787 bytes = _dbus_string_get_length (&addr);
788 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
790 /* pipe write returns an error on failure but not short write */
791 if (error != NULL && !dbus_error_is_set (error))
793 dbus_set_error (error, DBUS_ERROR_FAILED,
794 "Printing message bus address: did not write all bytes\n");
796 _dbus_string_free (&addr);
800 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
801 _dbus_pipe_close (print_addr_pipe, NULL);
803 _dbus_string_free (&addr);
806 context->connections = bus_connections_new (context);
807 if (context->connections == NULL)
813 context->matchmaker = bus_matchmaker_new ();
814 if (context->matchmaker == NULL)
820 /* check user before we fork */
821 if (context->user != NULL)
823 if (!_dbus_verify_daemon_user (context->user))
825 dbus_set_error (error, DBUS_ERROR_FAILED,
826 "Could not get UID and GID for username \"%s\"",
832 /* Now become a daemon if appropriate and write out pid file in any case */
836 if (context->pidfile)
837 _dbus_string_init_const (&u, context->pidfile);
839 if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
841 _dbus_verbose ("Forking and becoming daemon\n");
843 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
846 context->keep_umask))
848 _DBUS_ASSERT_ERROR_IS_SET (error);
854 _dbus_verbose ("Fork not requested\n");
856 /* Need to write PID file and to PID pipe for ourselves,
857 * not for the child process. This is a no-op if the pidfile
858 * is NULL and print_pid_pipe is NULL.
860 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
865 _DBUS_ASSERT_ERROR_IS_SET (error);
871 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
872 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
873 _dbus_pipe_close (print_pid_pipe, NULL);
875 if (!bus_selinux_full_init ())
877 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but AVC initialization failed; check system log\n");
880 if (!process_config_postinit (context, parser, error))
882 _DBUS_ASSERT_ERROR_IS_SET (error);
888 bus_config_parser_unref (parser);
892 /* Here we change our credentials if required,
893 * as soon as we've set up our sockets and pidfile
895 if (context->user != NULL)
897 if (!_dbus_change_to_daemon_user (context->user, error))
899 _DBUS_ASSERT_ERROR_IS_SET (error);
904 /* FIXME - why not just put this in full_init() below? */
905 bus_selinux_audit_init ();
909 dbus_server_free_data_slot (&server_data_slot);
915 bus_config_parser_unref (parser);
917 bus_context_unref (context);
919 if (server_data_slot >= 0)
920 dbus_server_free_data_slot (&server_data_slot);
926 bus_context_get_id (BusContext *context,
929 return _dbus_uuid_encode (&context->uuid, uuid);
933 bus_context_reload_config (BusContext *context,
936 BusConfigParser *parser;
937 DBusString config_file;
940 /* Flush the user database cache */
941 _dbus_flush_caches ();
944 _dbus_string_init_const (&config_file, context->config_file);
945 parser = bus_config_load (&config_file, TRUE, NULL, error);
948 _DBUS_ASSERT_ERROR_IS_SET (error);
952 if (!process_config_every_time (context, parser, TRUE, error))
954 _DBUS_ASSERT_ERROR_IS_SET (error);
957 if (!process_config_postinit (context, parser, error))
959 _DBUS_ASSERT_ERROR_IS_SET (error);
964 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
967 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
969 bus_config_parser_unref (parser);
974 shutdown_server (BusContext *context,
977 if (server == NULL ||
978 !dbus_server_get_is_connected (server))
981 if (!dbus_server_set_watch_functions (server,
985 _dbus_assert_not_reached ("setting watch functions to NULL failed");
987 if (!dbus_server_set_timeout_functions (server,
991 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
993 dbus_server_disconnect (server);
997 bus_context_shutdown (BusContext *context)
1001 link = _dbus_list_get_first_link (&context->servers);
1002 while (link != NULL)
1004 shutdown_server (context, link->data);
1006 link = _dbus_list_get_next_link (&context->servers, link);
1011 bus_context_ref (BusContext *context)
1013 _dbus_assert (context->refcount > 0);
1014 context->refcount += 1;
1020 bus_context_unref (BusContext *context)
1022 _dbus_assert (context->refcount > 0);
1023 context->refcount -= 1;
1025 if (context->refcount == 0)
1029 _dbus_verbose ("Finalizing bus context %p\n", context);
1031 bus_context_shutdown (context);
1033 if (context->connections)
1035 bus_connections_unref (context->connections);
1036 context->connections = NULL;
1039 if (context->registry)
1041 bus_registry_unref (context->registry);
1042 context->registry = NULL;
1045 if (context->activation)
1047 bus_activation_unref (context->activation);
1048 context->activation = NULL;
1051 link = _dbus_list_get_first_link (&context->servers);
1052 while (link != NULL)
1054 dbus_server_unref (link->data);
1056 link = _dbus_list_get_next_link (&context->servers, link);
1058 _dbus_list_clear (&context->servers);
1060 if (context->policy)
1062 bus_policy_unref (context->policy);
1063 context->policy = NULL;
1068 _dbus_loop_unref (context->loop);
1069 context->loop = NULL;
1072 if (context->matchmaker)
1074 bus_matchmaker_unref (context->matchmaker);
1075 context->matchmaker = NULL;
1078 dbus_free (context->config_file);
1079 dbus_free (context->log_prefix);
1080 dbus_free (context->type);
1081 dbus_free (context->address);
1082 dbus_free (context->user);
1083 dbus_free (context->servicehelper);
1085 if (context->pidfile)
1088 _dbus_string_init_const (&u, context->pidfile);
1090 /* Deliberately ignore errors here, since there's not much
1091 * we can do about it, and we're exiting anyways.
1093 _dbus_delete_file (&u, NULL);
1095 dbus_free (context->pidfile);
1097 dbus_free (context);
1099 dbus_server_free_data_slot (&server_data_slot);
1103 /* type may be NULL */
1105 bus_context_get_type (BusContext *context)
1107 return context->type;
1111 bus_context_get_address (BusContext *context)
1113 return context->address;
1117 bus_context_get_servicehelper (BusContext *context)
1119 return context->servicehelper;
1123 bus_context_get_systemd_activation (BusContext *context)
1125 return context->systemd_activation;
1129 bus_context_get_registry (BusContext *context)
1131 return context->registry;
1135 bus_context_get_connections (BusContext *context)
1137 return context->connections;
1141 bus_context_get_activation (BusContext *context)
1143 return context->activation;
1147 bus_context_get_matchmaker (BusContext *context)
1149 return context->matchmaker;
1153 bus_context_get_loop (BusContext *context)
1155 return context->loop;
1159 bus_context_allow_unix_user (BusContext *context,
1162 return bus_policy_allow_unix_user (context->policy,
1166 /* For now this is never actually called because the default
1167 * DBusConnection behavior of 'same user that owns the bus can connect'
1168 * is all it would do.
1171 bus_context_allow_windows_user (BusContext *context,
1172 const char *windows_sid)
1174 return bus_policy_allow_windows_user (context->policy,
1179 bus_context_get_policy (BusContext *context)
1181 return context->policy;
1185 bus_context_create_client_policy (BusContext *context,
1186 DBusConnection *connection,
1189 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1190 return bus_policy_create_client_policy (context->policy, connection,
1195 bus_context_get_activation_timeout (BusContext *context)
1198 return context->limits.activation_timeout;
1202 bus_context_get_auth_timeout (BusContext *context)
1204 return context->limits.auth_timeout;
1208 bus_context_get_max_completed_connections (BusContext *context)
1210 return context->limits.max_completed_connections;
1214 bus_context_get_max_incomplete_connections (BusContext *context)
1216 return context->limits.max_incomplete_connections;
1220 bus_context_get_max_connections_per_user (BusContext *context)
1222 return context->limits.max_connections_per_user;
1226 bus_context_get_max_pending_activations (BusContext *context)
1228 return context->limits.max_pending_activations;
1232 bus_context_get_max_services_per_connection (BusContext *context)
1234 return context->limits.max_services_per_connection;
1238 bus_context_get_max_match_rules_per_connection (BusContext *context)
1240 return context->limits.max_match_rules_per_connection;
1244 bus_context_get_max_replies_per_connection (BusContext *context)
1246 return context->limits.max_replies_per_connection;
1250 bus_context_get_reply_timeout (BusContext *context)
1252 return context->limits.reply_timeout;
1256 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
1259 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1263 if (!context->syslog)
1266 va_start (args, msg);
1268 if (context->log_prefix)
1270 DBusString full_msg;
1272 if (!_dbus_string_init (&full_msg))
1274 if (!_dbus_string_append (&full_msg, context->log_prefix))
1276 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1279 _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1281 _dbus_string_free (&full_msg);
1284 _dbus_system_logv (severity, msg, args);
1291 * addressed_recipient is the recipient specified in the message.
1293 * proposed_recipient is the recipient we're considering sending
1294 * to right this second, and may be an eavesdropper.
1296 * sender is the sender of the message.
1298 * NULL for proposed_recipient or sender definitely means the bus driver.
1300 * NULL for addressed_recipient may mean the bus driver, or may mean
1301 * no destination was specified in the message (e.g. a signal).
1304 bus_context_check_security_policy (BusContext *context,
1305 BusTransaction *transaction,
1306 DBusConnection *sender,
1307 DBusConnection *addressed_recipient,
1308 DBusConnection *proposed_recipient,
1309 DBusMessage *message,
1313 BusClientPolicy *sender_policy;
1314 BusClientPolicy *recipient_policy;
1315 dbus_int32_t toggles;
1318 dbus_bool_t requested_reply;
1319 const char *sender_name;
1320 const char *sender_loginfo;
1321 const char *proposed_recipient_loginfo;
1323 type = dbus_message_get_type (message);
1324 dest = dbus_message_get_destination (message);
1326 /* dispatch.c was supposed to ensure these invariants */
1327 _dbus_assert (dest != NULL ||
1328 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1329 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1330 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1331 addressed_recipient != NULL ||
1332 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1334 /* Used in logging below */
1337 sender_name = bus_connection_get_name (sender);
1338 sender_loginfo = bus_connection_get_loginfo (sender);
1343 sender_loginfo = "(bus)";
1346 if (proposed_recipient != NULL)
1347 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1349 proposed_recipient_loginfo = "bus";
1353 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1354 case DBUS_MESSAGE_TYPE_SIGNAL:
1355 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1356 case DBUS_MESSAGE_TYPE_ERROR:
1360 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1363 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1364 "Message bus will not accept messages of unknown type\n");
1369 requested_reply = FALSE;
1373 /* First verify the SELinux access controls. If allowed then
1374 * go on with the standard checks.
1376 if (!bus_selinux_allows_send (sender, proposed_recipient,
1377 dbus_message_type_to_string (dbus_message_get_type (message)),
1378 dbus_message_get_interface (message),
1379 dbus_message_get_member (message),
1380 dbus_message_get_error_name (message),
1381 dest ? dest : DBUS_SERVICE_DBUS, error))
1383 if (error != NULL && !dbus_error_is_set (error))
1385 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1386 "An SELinux policy prevents this sender "
1387 "from sending this message to this recipient "
1388 "(rejected message had sender \"%s\" interface \"%s\" "
1389 "member \"%s\" error name \"%s\" destination \"%s\")",
1390 sender_name ? sender_name : "(unset)",
1391 dbus_message_get_interface (message) ?
1392 dbus_message_get_interface (message) : "(unset)",
1393 dbus_message_get_member (message) ?
1394 dbus_message_get_member (message) : "(unset)",
1395 dbus_message_get_error_name (message) ?
1396 dbus_message_get_error_name (message) : "(unset)",
1397 dest ? dest : DBUS_SERVICE_DBUS);
1398 _dbus_verbose ("SELinux security check denying send to service\n");
1404 if (bus_connection_is_active (sender))
1406 sender_policy = bus_connection_get_policy (sender);
1407 _dbus_assert (sender_policy != NULL);
1409 /* Fill in requested_reply variable with TRUE if this is a
1410 * reply and the reply was pending.
1412 if (dbus_message_get_reply_serial (message) != 0)
1414 if (proposed_recipient != NULL /* not to the bus driver */ &&
1415 addressed_recipient == proposed_recipient /* not eavesdropping */)
1419 dbus_error_init (&error2);
1420 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1422 sender, addressed_recipient, message,
1424 if (dbus_error_is_set (&error2))
1426 dbus_move_error (&error2, error);
1434 /* Policy for inactive connections is that they can only send
1435 * the hello message to the bus driver
1437 if (proposed_recipient == NULL &&
1438 dbus_message_is_method_call (message,
1439 DBUS_INTERFACE_DBUS,
1442 _dbus_verbose ("security check allowing %s message\n",
1448 _dbus_verbose ("security check disallowing non-%s message\n",
1451 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1452 "Client tried to send a message other than %s without being registered",
1461 sender_policy = NULL;
1463 /* If the sender is the bus driver, we assume any reply was a
1464 * requested reply as bus driver won't send bogus ones
1466 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1467 dbus_message_get_reply_serial (message) != 0)
1468 requested_reply = TRUE;
1471 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1472 (sender == NULL && sender_policy == NULL));
1474 if (proposed_recipient != NULL)
1476 /* only the bus driver can send to an inactive recipient (as it
1477 * owns no services, so other apps can't address it). Inactive
1478 * recipients can receive any message.
1480 if (bus_connection_is_active (proposed_recipient))
1482 recipient_policy = bus_connection_get_policy (proposed_recipient);
1483 _dbus_assert (recipient_policy != NULL);
1485 else if (sender == NULL)
1487 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1488 recipient_policy = NULL;
1492 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1493 recipient_policy = NULL;
1497 recipient_policy = NULL;
1499 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1500 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1501 (proposed_recipient == NULL && recipient_policy == NULL));
1504 if (sender_policy &&
1505 !bus_client_policy_check_can_send (sender_policy,
1509 message, &toggles, &log))
1511 const char *msg = "Rejected send message, %d matched rules; "
1512 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))";
1514 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1516 dbus_message_type_to_string (dbus_message_get_type (message)),
1517 sender_name ? sender_name : "(unset)",
1519 dbus_message_get_interface (message) ?
1520 dbus_message_get_interface (message) : "(unset)",
1521 dbus_message_get_member (message) ?
1522 dbus_message_get_member (message) : "(unset)",
1523 dbus_message_get_error_name (message) ?
1524 dbus_message_get_error_name (message) : "(unset)",
1526 dest ? dest : DBUS_SERVICE_DBUS,
1527 proposed_recipient_loginfo);
1528 /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1529 if (addressed_recipient == proposed_recipient)
1530 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, msg,
1532 dbus_message_type_to_string (dbus_message_get_type (message)),
1533 sender_name ? sender_name : "(unset)",
1535 dbus_message_get_interface (message) ?
1536 dbus_message_get_interface (message) : "(unset)",
1537 dbus_message_get_member (message) ?
1538 dbus_message_get_member (message) : "(unset)",
1539 dbus_message_get_error_name (message) ?
1540 dbus_message_get_error_name (message) : "(unset)",
1542 dest ? dest : DBUS_SERVICE_DBUS,
1543 proposed_recipient_loginfo);
1544 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1549 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY,
1550 "Would reject message, %d matched rules; "
1551 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))",
1553 dbus_message_type_to_string (dbus_message_get_type (message)),
1554 sender_name ? sender_name : "(unset)",
1556 dbus_message_get_interface (message) ?
1557 dbus_message_get_interface (message) : "(unset)",
1558 dbus_message_get_member (message) ?
1559 dbus_message_get_member (message) : "(unset)",
1560 dbus_message_get_error_name (message) ?
1561 dbus_message_get_error_name (message) : "(unset)",
1563 dest ? dest : DBUS_SERVICE_DBUS,
1564 proposed_recipient_loginfo);
1566 if (recipient_policy &&
1567 !bus_client_policy_check_can_receive (recipient_policy,
1571 addressed_recipient, proposed_recipient,
1574 const char *msg = "Rejected receive message, %d matched rules; "
1575 "type=\"%s\" sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" reply serial=%u requested_reply=%d destination=\"%s\" (%s))";
1577 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1579 dbus_message_type_to_string (dbus_message_get_type (message)),
1580 sender_name ? sender_name : "(unset)",
1582 dbus_message_get_interface (message) ?
1583 dbus_message_get_interface (message) : "(unset)",
1584 dbus_message_get_member (message) ?
1585 dbus_message_get_member (message) : "(unset)",
1586 dbus_message_get_error_name (message) ?
1587 dbus_message_get_error_name (message) : "(unset)",
1588 dbus_message_get_reply_serial (message),
1590 dest ? dest : DBUS_SERVICE_DBUS,
1591 proposed_recipient_loginfo);
1592 /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1593 if (addressed_recipient == proposed_recipient)
1594 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, msg,
1596 dbus_message_type_to_string (dbus_message_get_type (message)),
1597 sender_name ? sender_name : "(unset)",
1599 dbus_message_get_interface (message) ?
1600 dbus_message_get_interface (message) : "(unset)",
1601 dbus_message_get_member (message) ?
1602 dbus_message_get_member (message) : "(unset)",
1603 dbus_message_get_error_name (message) ?
1604 dbus_message_get_error_name (message) : "(unset)",
1605 dbus_message_get_reply_serial (message),
1607 dest ? dest : DBUS_SERVICE_DBUS,
1608 proposed_recipient_loginfo);
1609 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1613 /* See if limits on size have been exceeded */
1614 if (proposed_recipient &&
1615 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1616 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1618 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1619 "The destination service \"%s\" has a full message queue",
1620 dest ? dest : (proposed_recipient ?
1621 bus_connection_get_name (proposed_recipient) :
1622 DBUS_SERVICE_DBUS));
1623 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1627 /* Record that we will allow a reply here in the future (don't
1628 * bother if the recipient is the bus or this is an eavesdropping
1629 * connection). Only the addressed recipient may reply.
1631 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1633 addressed_recipient &&
1634 addressed_recipient == proposed_recipient && /* not eavesdropping */
1635 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1637 sender, addressed_recipient,
1640 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1644 _dbus_verbose ("security policy allowing message\n");