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
29 #include "activation.h"
30 #include "connection.h"
34 #include "config-parser.h"
37 #include "dir-watch.h"
38 #include <dbus/dbus-list.h>
39 #include <dbus/dbus-hash.h>
40 #include <dbus/dbus-credentials.h>
41 #include <dbus/dbus-internals.h>
60 BusConnections *connections;
61 BusActivation *activation;
62 BusRegistry *registry;
64 BusMatchmaker *matchmaker;
66 unsigned int fork : 1;
67 unsigned int syslog : 1;
68 unsigned int keep_umask : 1;
69 unsigned int allow_anonymous : 1;
70 unsigned int systemd_activation : 1;
73 static dbus_int32_t server_data_slot = -1;
80 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
83 server_get_context (DBusServer *server)
88 if (!dbus_server_allocate_data_slot (&server_data_slot))
91 bd = BUS_SERVER_DATA (server);
94 dbus_server_free_data_slot (&server_data_slot);
98 context = bd->context;
100 dbus_server_free_data_slot (&server_data_slot);
106 add_server_watch (DBusWatch *watch,
109 DBusServer *server = data;
112 context = server_get_context (server);
114 return _dbus_loop_add_watch (context->loop, watch);
118 remove_server_watch (DBusWatch *watch,
121 DBusServer *server = data;
124 context = server_get_context (server);
126 _dbus_loop_remove_watch (context->loop, watch);
130 add_server_timeout (DBusTimeout *timeout,
133 DBusServer *server = data;
136 context = server_get_context (server);
138 return _dbus_loop_add_timeout (context->loop, timeout);
142 remove_server_timeout (DBusTimeout *timeout,
145 DBusServer *server = data;
148 context = server_get_context (server);
150 _dbus_loop_remove_timeout (context->loop, timeout);
154 new_connection_callback (DBusServer *server,
155 DBusConnection *new_connection,
158 BusContext *context = data;
160 if (!bus_connections_setup_connection (context->connections, new_connection))
162 _dbus_verbose ("No memory to setup new connection\n");
164 /* if we don't do this, it will get unref'd without
165 * being disconnected... kind of strange really
166 * that we have to do this, people won't get it right
169 dbus_connection_close (new_connection);
172 dbus_connection_set_max_received_size (new_connection,
173 context->limits.max_incoming_bytes);
175 dbus_connection_set_max_message_size (new_connection,
176 context->limits.max_message_size);
178 dbus_connection_set_max_received_unix_fds (new_connection,
179 context->limits.max_incoming_unix_fds);
181 dbus_connection_set_max_message_unix_fds (new_connection,
182 context->limits.max_message_unix_fds);
184 dbus_connection_set_allow_anonymous (new_connection,
185 context->allow_anonymous);
187 /* on OOM, we won't have ref'd the connection so it will die. */
191 free_server_data (void *data)
193 BusServerData *bd = data;
199 setup_server (BusContext *context,
201 char **auth_mechanisms,
206 bd = dbus_new0 (BusServerData, 1);
207 if (bd == NULL || !dbus_server_set_data (server,
209 bd, free_server_data))
216 bd->context = context;
218 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
224 dbus_server_set_new_connection_function (server,
225 new_connection_callback,
228 if (!dbus_server_set_watch_functions (server,
239 if (!dbus_server_set_timeout_functions (server,
241 remove_server_timeout,
252 /* This code only gets executed the first time the
253 * config files are parsed. It is not executed
254 * when config files are reloaded.
257 process_config_first_time_only (BusContext *context,
258 BusConfigParser *parser,
259 const DBusString *address,
260 dbus_bool_t systemd_activation,
263 DBusString log_prefix;
265 DBusList **addresses;
266 const char *user, *pidfile;
267 char **auth_mechanisms;
268 DBusList **auth_mechanisms_list;
272 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
275 auth_mechanisms = NULL;
277 _dbus_init_system_log ();
279 context->systemd_activation = systemd_activation;
281 /* Check for an existing pid file. Of course this is a race;
282 * we'd have to use fcntl() locks on the pid file to
283 * avoid that. But we want to check for the pid file
284 * before overwriting any existing sockets, etc.
286 pidfile = bus_config_parser_get_pidfile (parser);
292 _dbus_string_init_const (&u, pidfile);
294 if (_dbus_stat (&u, &stbuf, NULL))
300 _dbus_string_init (&p);
301 _dbus_file_get_contents(&p, &u, NULL);
302 _dbus_string_parse_int(&p, 0, &pid, NULL);
303 _dbus_string_free(&p);
305 if ((kill((int)pid, 0))) {
306 dbus_set_error(NULL, DBUS_ERROR_FILE_EXISTS,
307 "pid %ld not running, removing stale pid file\n",
309 _dbus_delete_file(&u, NULL);
312 dbus_set_error (error, DBUS_ERROR_FAILED,
313 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
322 /* keep around the pid filename so we can delete it later */
323 context->pidfile = _dbus_strdup (pidfile);
325 /* note that type may be NULL */
326 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
327 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
330 user = bus_config_parser_get_user (parser);
333 context->user = _dbus_strdup (user);
334 if (context->user == NULL)
338 /* Set up the prefix for syslog messages */
339 if (!_dbus_string_init (&log_prefix))
341 if (context->type && !strcmp (context->type, "system"))
343 if (!_dbus_string_append (&log_prefix, "[system] "))
346 else if (context->type && !strcmp (context->type, "session"))
348 DBusCredentials *credentials;
350 credentials = _dbus_credentials_new_from_current_process ();
353 if (!_dbus_string_append (&log_prefix, "[session "))
355 _dbus_credentials_unref (credentials);
358 if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
360 _dbus_credentials_unref (credentials);
363 if (!_dbus_string_append (&log_prefix, "] "))
365 _dbus_credentials_unref (credentials);
368 _dbus_credentials_unref (credentials);
370 if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
372 _dbus_string_free (&log_prefix);
374 /* Build an array of auth mechanisms */
376 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
377 len = _dbus_list_get_length (auth_mechanisms_list);
383 auth_mechanisms = dbus_new0 (char*, len + 1);
384 if (auth_mechanisms == NULL)
388 link = _dbus_list_get_first_link (auth_mechanisms_list);
391 auth_mechanisms[i] = _dbus_strdup (link->data);
392 if (auth_mechanisms[i] == NULL)
394 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
399 auth_mechanisms = NULL;
402 /* Listen on our addresses */
408 server = dbus_server_listen (_dbus_string_get_const_data(address), error);
411 _DBUS_ASSERT_ERROR_IS_SET (error);
414 else if (!setup_server (context, server, auth_mechanisms, error))
416 _DBUS_ASSERT_ERROR_IS_SET (error);
420 if (!_dbus_list_append (&context->servers, server))
425 addresses = bus_config_parser_get_addresses (parser);
427 link = _dbus_list_get_first_link (addresses);
432 server = dbus_server_listen (link->data, error);
435 _DBUS_ASSERT_ERROR_IS_SET (error);
438 else if (!setup_server (context, server, auth_mechanisms, error))
440 _DBUS_ASSERT_ERROR_IS_SET (error);
444 if (!_dbus_list_append (&context->servers, server))
447 link = _dbus_list_get_next_link (addresses, link);
451 context->fork = bus_config_parser_get_fork (parser);
452 context->syslog = bus_config_parser_get_syslog (parser);
453 context->keep_umask = bus_config_parser_get_keep_umask (parser);
454 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
456 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
460 dbus_free_string_array (auth_mechanisms);
465 dbus_free_string_array (auth_mechanisms);
469 /* This code gets executed every time the config files
470 * are parsed: both during BusContext construction
471 * and on reloads. This function is slightly screwy
472 * since it can do a "half reload" in out-of-memory
473 * situations. Realistically, unlikely to ever matter.
476 process_config_every_time (BusContext *context,
477 BusConfigParser *parser,
478 dbus_bool_t is_reload,
481 DBusString full_address;
485 const char *servicehelper;
490 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
495 if (!_dbus_string_init (&full_address))
501 /* get our limits and timeout lengths */
502 bus_config_parser_get_limits (parser, &context->limits);
505 bus_policy_unref (context->policy);
506 context->policy = bus_config_parser_steal_policy (parser);
507 _dbus_assert (context->policy != NULL);
509 /* We have to build the address backward, so that
510 * <listen> later in the config file have priority
512 link = _dbus_list_get_last_link (&context->servers);
515 addr = dbus_server_get_address (link->data);
522 if (_dbus_string_get_length (&full_address) > 0)
524 if (!_dbus_string_append (&full_address, ";"))
531 if (!_dbus_string_append (&full_address, addr))
540 link = _dbus_list_get_prev_link (&context->servers, link);
544 dbus_free (context->address);
546 if (!_dbus_string_copy_data (&full_address, &context->address))
552 /* get the service directories */
553 dirs = bus_config_parser_get_service_dirs (parser);
555 /* and the service helper */
556 servicehelper = bus_config_parser_get_servicehelper (parser);
558 s = _dbus_strdup(servicehelper);
559 if (s == NULL && servicehelper != NULL)
566 dbus_free(context->servicehelper);
567 context->servicehelper = s;
570 /* Create activation subsystem */
571 if (context->activation)
573 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
578 context->activation = bus_activation_new (context, &full_address, dirs, error);
581 if (context->activation == NULL)
583 _DBUS_ASSERT_ERROR_IS_SET (error);
587 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
591 _dbus_string_free (&full_address);
600 list_concat_new (DBusList **a,
608 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
610 if (!_dbus_list_append (result, link->data))
613 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
615 if (!_dbus_list_append (result, link->data))
621 _dbus_list_clear (result);
626 raise_file_descriptor_limit (BusContext *context)
629 /* I just picked this out of thin air; we need some extra
630 * descriptors for things like any internal pipes we create,
631 * inotify, connections to SELinux, etc.
633 unsigned int arbitrary_extra_fds = 32;
636 limit = context->limits.max_completed_connections +
637 context->limits.max_incomplete_connections
638 + arbitrary_extra_fds;
640 _dbus_request_file_descriptor_limit (limit);
644 process_config_postinit (BusContext *context,
645 BusConfigParser *parser,
648 DBusHashTable *service_context_table;
649 DBusList *watched_dirs = NULL;
651 raise_file_descriptor_limit (context);
653 service_context_table = bus_config_parser_steal_service_context_table (parser);
654 if (!bus_registry_set_service_context_table (context->registry,
655 service_context_table))
661 _dbus_hash_table_unref (service_context_table);
663 /* We need to monitor both the configuration directories and directories
664 * containing .service files.
666 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
667 bus_config_parser_get_service_dirs (parser),
674 bus_set_watched_dirs (context, &watched_dirs);
676 _dbus_list_clear (&watched_dirs);
682 bus_context_new (const DBusString *config_file,
683 ForceForkSetting force_fork,
684 DBusPipe *print_addr_pipe,
685 DBusPipe *print_pid_pipe,
686 const DBusString *address,
687 dbus_bool_t systemd_activation,
691 BusConfigParser *parser;
693 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
698 if (!dbus_server_allocate_data_slot (&server_data_slot))
704 context = dbus_new0 (BusContext, 1);
710 context->refcount = 1;
712 _dbus_generate_uuid (&context->uuid);
714 if (!_dbus_string_copy_data (config_file, &context->config_file))
720 context->loop = _dbus_loop_new ();
721 if (context->loop == NULL)
727 context->registry = bus_registry_new (context);
728 if (context->registry == NULL)
734 parser = bus_config_load (config_file, TRUE, NULL, error);
737 _DBUS_ASSERT_ERROR_IS_SET (error);
741 if (!process_config_first_time_only (context, parser, address, systemd_activation, error))
743 _DBUS_ASSERT_ERROR_IS_SET (error);
746 if (!process_config_every_time (context, parser, FALSE, error))
748 _DBUS_ASSERT_ERROR_IS_SET (error);
752 /* we need another ref of the server data slot for the context
755 if (!dbus_server_allocate_data_slot (&server_data_slot))
756 _dbus_assert_not_reached ("second ref of server data slot failed");
758 /* Note that we don't know whether the print_addr_pipe is
759 * one of the sockets we're using to listen on, or some
760 * other random thing. But I think the answer is "don't do
763 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
766 const char *a = bus_context_get_address (context);
769 _dbus_assert (a != NULL);
770 if (!_dbus_string_init (&addr))
776 if (!_dbus_string_append (&addr, a) ||
777 !_dbus_string_append (&addr, "\n"))
779 _dbus_string_free (&addr);
784 bytes = _dbus_string_get_length (&addr);
785 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
787 /* pipe write returns an error on failure but not short write */
788 if (error != NULL && !dbus_error_is_set (error))
790 dbus_set_error (error, DBUS_ERROR_FAILED,
791 "Printing message bus address: did not write all bytes\n");
793 _dbus_string_free (&addr);
797 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
798 _dbus_pipe_close (print_addr_pipe, NULL);
800 _dbus_string_free (&addr);
803 context->connections = bus_connections_new (context);
804 if (context->connections == NULL)
810 context->matchmaker = bus_matchmaker_new ();
811 if (context->matchmaker == NULL)
817 /* check user before we fork */
818 if (context->user != NULL)
820 if (!_dbus_verify_daemon_user (context->user))
822 dbus_set_error (error, DBUS_ERROR_FAILED,
823 "Could not get UID and GID for username \"%s\"",
829 /* Now become a daemon if appropriate and write out pid file in any case */
833 if (context->pidfile)
834 _dbus_string_init_const (&u, context->pidfile);
836 if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
838 _dbus_verbose ("Forking and becoming daemon\n");
840 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
843 context->keep_umask))
845 _DBUS_ASSERT_ERROR_IS_SET (error);
851 _dbus_verbose ("Fork not requested\n");
853 /* Need to write PID file and to PID pipe for ourselves,
854 * not for the child process. This is a no-op if the pidfile
855 * is NULL and print_pid_pipe is NULL.
857 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
862 _DBUS_ASSERT_ERROR_IS_SET (error);
868 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
869 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
870 _dbus_pipe_close (print_pid_pipe, NULL);
872 if (!bus_selinux_full_init ())
874 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but AVC initialization failed; check system log\n");
877 if (!process_config_postinit (context, parser, error))
879 _DBUS_ASSERT_ERROR_IS_SET (error);
885 bus_config_parser_unref (parser);
889 /* Here we change our credentials if required,
890 * as soon as we've set up our sockets and pidfile
892 if (context->user != NULL)
894 if (!_dbus_change_to_daemon_user (context->user, error))
896 _DBUS_ASSERT_ERROR_IS_SET (error);
901 /* FIXME - why not just put this in full_init() below? */
902 bus_selinux_audit_init ();
906 dbus_server_free_data_slot (&server_data_slot);
912 bus_config_parser_unref (parser);
914 bus_context_unref (context);
916 if (server_data_slot >= 0)
917 dbus_server_free_data_slot (&server_data_slot);
923 bus_context_get_id (BusContext *context,
926 return _dbus_uuid_encode (&context->uuid, uuid);
930 bus_context_reload_config (BusContext *context,
933 BusConfigParser *parser;
934 DBusString config_file;
937 /* Flush the user database cache */
938 _dbus_flush_caches ();
941 _dbus_string_init_const (&config_file, context->config_file);
942 parser = bus_config_load (&config_file, TRUE, NULL, error);
945 _DBUS_ASSERT_ERROR_IS_SET (error);
949 if (!process_config_every_time (context, parser, TRUE, error))
951 _DBUS_ASSERT_ERROR_IS_SET (error);
954 if (!process_config_postinit (context, parser, error))
956 _DBUS_ASSERT_ERROR_IS_SET (error);
961 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
964 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
966 bus_config_parser_unref (parser);
971 shutdown_server (BusContext *context,
974 if (server == NULL ||
975 !dbus_server_get_is_connected (server))
978 if (!dbus_server_set_watch_functions (server,
982 _dbus_assert_not_reached ("setting watch functions to NULL failed");
984 if (!dbus_server_set_timeout_functions (server,
988 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
990 dbus_server_disconnect (server);
994 bus_context_shutdown (BusContext *context)
998 link = _dbus_list_get_first_link (&context->servers);
1001 shutdown_server (context, link->data);
1003 link = _dbus_list_get_next_link (&context->servers, link);
1008 bus_context_ref (BusContext *context)
1010 _dbus_assert (context->refcount > 0);
1011 context->refcount += 1;
1017 bus_context_unref (BusContext *context)
1019 _dbus_assert (context->refcount > 0);
1020 context->refcount -= 1;
1022 if (context->refcount == 0)
1026 _dbus_verbose ("Finalizing bus context %p\n", context);
1028 bus_context_shutdown (context);
1030 if (context->connections)
1032 bus_connections_unref (context->connections);
1033 context->connections = NULL;
1036 if (context->registry)
1038 bus_registry_unref (context->registry);
1039 context->registry = NULL;
1042 if (context->activation)
1044 bus_activation_unref (context->activation);
1045 context->activation = NULL;
1048 link = _dbus_list_get_first_link (&context->servers);
1049 while (link != NULL)
1051 dbus_server_unref (link->data);
1053 link = _dbus_list_get_next_link (&context->servers, link);
1055 _dbus_list_clear (&context->servers);
1057 if (context->policy)
1059 bus_policy_unref (context->policy);
1060 context->policy = NULL;
1065 _dbus_loop_unref (context->loop);
1066 context->loop = NULL;
1069 if (context->matchmaker)
1071 bus_matchmaker_unref (context->matchmaker);
1072 context->matchmaker = NULL;
1075 dbus_free (context->config_file);
1076 dbus_free (context->log_prefix);
1077 dbus_free (context->type);
1078 dbus_free (context->address);
1079 dbus_free (context->user);
1080 dbus_free (context->servicehelper);
1082 if (context->pidfile)
1085 _dbus_string_init_const (&u, context->pidfile);
1087 /* Deliberately ignore errors here, since there's not much
1088 * we can do about it, and we're exiting anyways.
1090 _dbus_delete_file (&u, NULL);
1092 dbus_free (context->pidfile);
1094 dbus_free (context);
1096 dbus_server_free_data_slot (&server_data_slot);
1100 /* type may be NULL */
1102 bus_context_get_type (BusContext *context)
1104 return context->type;
1108 bus_context_get_address (BusContext *context)
1110 return context->address;
1114 bus_context_get_servicehelper (BusContext *context)
1116 return context->servicehelper;
1120 bus_context_get_systemd_activation (BusContext *context)
1122 return context->systemd_activation;
1126 bus_context_get_registry (BusContext *context)
1128 return context->registry;
1132 bus_context_get_connections (BusContext *context)
1134 return context->connections;
1138 bus_context_get_activation (BusContext *context)
1140 return context->activation;
1144 bus_context_get_matchmaker (BusContext *context)
1146 return context->matchmaker;
1150 bus_context_get_loop (BusContext *context)
1152 return context->loop;
1156 bus_context_allow_unix_user (BusContext *context,
1159 return bus_policy_allow_unix_user (context->policy,
1163 /* For now this is never actually called because the default
1164 * DBusConnection behavior of 'same user that owns the bus can connect'
1165 * is all it would do.
1168 bus_context_allow_windows_user (BusContext *context,
1169 const char *windows_sid)
1171 return bus_policy_allow_windows_user (context->policy,
1176 bus_context_get_policy (BusContext *context)
1178 return context->policy;
1182 bus_context_create_client_policy (BusContext *context,
1183 DBusConnection *connection,
1186 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1187 return bus_policy_create_client_policy (context->policy, connection,
1192 bus_context_get_activation_timeout (BusContext *context)
1195 return context->limits.activation_timeout;
1199 bus_context_get_auth_timeout (BusContext *context)
1201 return context->limits.auth_timeout;
1205 bus_context_get_max_completed_connections (BusContext *context)
1207 return context->limits.max_completed_connections;
1211 bus_context_get_max_incomplete_connections (BusContext *context)
1213 return context->limits.max_incomplete_connections;
1217 bus_context_get_max_connections_per_user (BusContext *context)
1219 return context->limits.max_connections_per_user;
1223 bus_context_get_max_pending_activations (BusContext *context)
1225 return context->limits.max_pending_activations;
1229 bus_context_get_max_services_per_connection (BusContext *context)
1231 return context->limits.max_services_per_connection;
1235 bus_context_get_max_match_rules_per_connection (BusContext *context)
1237 return context->limits.max_match_rules_per_connection;
1241 bus_context_get_max_replies_per_connection (BusContext *context)
1243 return context->limits.max_replies_per_connection;
1247 bus_context_get_reply_timeout (BusContext *context)
1249 return context->limits.reply_timeout;
1253 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
1256 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1260 if (!context->syslog)
1262 /* we're not syslogging; just output to stderr */
1263 va_start (args, msg);
1264 vfprintf (stderr, msg, args);
1265 fprintf (stderr, "\n");
1270 va_start (args, msg);
1272 if (context->log_prefix)
1274 DBusString full_msg;
1276 if (!_dbus_string_init (&full_msg))
1278 if (!_dbus_string_append (&full_msg, context->log_prefix))
1280 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1283 _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1285 _dbus_string_free (&full_msg);
1288 _dbus_system_logv (severity, msg, args);
1294 static inline const char *
1295 nonnull (const char *maybe_null,
1296 const char *if_null)
1298 return (maybe_null ? maybe_null : if_null);
1302 * Log something about a message, usually that it was rejected.
1305 complain_about_message (BusContext *context,
1306 const char *error_name,
1307 const char *complaint,
1309 DBusMessage *message,
1310 DBusConnection *sender,
1311 DBusConnection *proposed_recipient,
1312 dbus_bool_t requested_reply,
1316 DBusError stack_error = DBUS_ERROR_INIT;
1317 const char *sender_name;
1318 const char *sender_loginfo;
1319 const char *proposed_recipient_loginfo;
1321 if (error == NULL && !log)
1326 sender_name = bus_connection_get_name (sender);
1327 sender_loginfo = bus_connection_get_loginfo (sender);
1331 sender_name = "(unset)";
1332 sender_loginfo = "(bus)";
1335 if (proposed_recipient != NULL)
1336 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1338 proposed_recipient_loginfo = "bus";
1340 dbus_set_error (&stack_error, error_name,
1341 "%s, %d matched rules; type=\"%s\", sender=\"%s\" (%s) "
1342 "interface=\"%s\" member=\"%s\" error name=\"%s\" "
1343 "requested_reply=\"%d\" destination=\"%s\" (%s)",
1346 dbus_message_type_to_string (dbus_message_get_type (message)),
1349 nonnull (dbus_message_get_interface (message), "(unset)"),
1350 nonnull (dbus_message_get_member (message), "(unset)"),
1351 nonnull (dbus_message_get_error_name (message), "(unset)"),
1353 nonnull (dbus_message_get_destination (message), DBUS_SERVICE_DBUS),
1354 proposed_recipient_loginfo);
1356 /* If we hit OOM while setting the error, this will syslog "out of memory"
1357 * which is itself an indication that something is seriously wrong */
1359 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, "%s",
1360 stack_error.message);
1362 dbus_move_error (&stack_error, error);
1366 * addressed_recipient is the recipient specified in the message.
1368 * proposed_recipient is the recipient we're considering sending
1369 * to right this second, and may be an eavesdropper.
1371 * sender is the sender of the message.
1373 * NULL for proposed_recipient or sender definitely means the bus driver.
1375 * NULL for addressed_recipient may mean the bus driver, or may mean
1376 * no destination was specified in the message (e.g. a signal).
1379 bus_context_check_security_policy (BusContext *context,
1380 BusTransaction *transaction,
1381 DBusConnection *sender,
1382 DBusConnection *addressed_recipient,
1383 DBusConnection *proposed_recipient,
1384 DBusMessage *message,
1388 BusClientPolicy *sender_policy;
1389 BusClientPolicy *recipient_policy;
1390 dbus_int32_t toggles;
1393 dbus_bool_t requested_reply;
1395 type = dbus_message_get_type (message);
1396 dest = dbus_message_get_destination (message);
1398 /* dispatch.c was supposed to ensure these invariants */
1399 _dbus_assert (dest != NULL ||
1400 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1401 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1402 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1403 addressed_recipient != NULL ||
1404 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1408 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1409 case DBUS_MESSAGE_TYPE_SIGNAL:
1410 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1411 case DBUS_MESSAGE_TYPE_ERROR:
1415 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1418 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1419 "Message bus will not accept messages of unknown type\n");
1424 requested_reply = FALSE;
1428 /* First verify the SELinux access controls. If allowed then
1429 * go on with the standard checks.
1431 if (!bus_selinux_allows_send (sender, proposed_recipient,
1432 dbus_message_type_to_string (dbus_message_get_type (message)),
1433 dbus_message_get_interface (message),
1434 dbus_message_get_member (message),
1435 dbus_message_get_error_name (message),
1436 dest ? dest : DBUS_SERVICE_DBUS, error))
1438 if (error != NULL && !dbus_error_is_set (error))
1440 /* don't syslog this, just set the error: avc_has_perm should
1441 * have already written to either the audit log or syslog */
1442 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1443 "An SELinux policy prevents this sender from sending this "
1444 "message to this recipient",
1445 0, message, sender, proposed_recipient, FALSE, FALSE, error);
1446 _dbus_verbose ("SELinux security check denying send to service\n");
1452 if (bus_connection_is_active (sender))
1454 sender_policy = bus_connection_get_policy (sender);
1455 _dbus_assert (sender_policy != NULL);
1457 /* Fill in requested_reply variable with TRUE if this is a
1458 * reply and the reply was pending.
1460 if (dbus_message_get_reply_serial (message) != 0)
1462 if (proposed_recipient != NULL /* not to the bus driver */ &&
1463 addressed_recipient == proposed_recipient /* not eavesdropping */)
1467 dbus_error_init (&error2);
1468 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1470 sender, addressed_recipient, message,
1472 if (dbus_error_is_set (&error2))
1474 dbus_move_error (&error2, error);
1482 /* Policy for inactive connections is that they can only send
1483 * the hello message to the bus driver
1485 if (proposed_recipient == NULL &&
1486 dbus_message_is_method_call (message,
1487 DBUS_INTERFACE_DBUS,
1490 _dbus_verbose ("security check allowing %s message\n",
1496 _dbus_verbose ("security check disallowing non-%s message\n",
1499 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1500 "Client tried to send a message other than %s without being registered",
1509 sender_policy = NULL;
1511 /* If the sender is the bus driver, we assume any reply was a
1512 * requested reply as bus driver won't send bogus ones
1514 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1515 dbus_message_get_reply_serial (message) != 0)
1516 requested_reply = TRUE;
1519 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1520 (sender == NULL && sender_policy == NULL));
1522 if (proposed_recipient != NULL)
1524 /* only the bus driver can send to an inactive recipient (as it
1525 * owns no services, so other apps can't address it). Inactive
1526 * recipients can receive any message.
1528 if (bus_connection_is_active (proposed_recipient))
1530 recipient_policy = bus_connection_get_policy (proposed_recipient);
1531 _dbus_assert (recipient_policy != NULL);
1533 else if (sender == NULL)
1535 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1536 recipient_policy = NULL;
1540 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1541 recipient_policy = NULL;
1545 recipient_policy = NULL;
1547 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1548 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1549 (proposed_recipient == NULL && recipient_policy == NULL));
1552 if (sender_policy &&
1553 !bus_client_policy_check_can_send (sender_policy,
1557 message, &toggles, &log))
1559 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1560 "Rejected send message", toggles,
1561 message, sender, proposed_recipient, requested_reply,
1562 (addressed_recipient == proposed_recipient), error);
1563 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1569 /* We want to drop this message, and are only not doing so for backwards
1571 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1572 "Would reject message", toggles,
1573 message, sender, proposed_recipient, requested_reply,
1577 if (recipient_policy &&
1578 !bus_client_policy_check_can_receive (recipient_policy,
1582 addressed_recipient, proposed_recipient,
1585 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1586 "Rejected receive message", toggles,
1587 message, sender, proposed_recipient, requested_reply,
1588 (addressed_recipient == proposed_recipient), NULL);
1589 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1593 /* See if limits on size have been exceeded */
1594 if (proposed_recipient &&
1595 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1596 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1598 complain_about_message (context, DBUS_ERROR_LIMITS_EXCEEDED,
1599 "Rejected: destination has a full message queue",
1600 0, message, sender, proposed_recipient, requested_reply, TRUE,
1602 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1606 /* Record that we will allow a reply here in the future (don't
1607 * bother if the recipient is the bus or this is an eavesdropping
1608 * connection). Only the addressed recipient may reply.
1610 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1612 addressed_recipient &&
1613 addressed_recipient == proposed_recipient && /* not eavesdropping */
1614 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1616 sender, addressed_recipient,
1619 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1623 _dbus_verbose ("security policy allowing message\n");