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;
484 BusActivation *new_activation;
486 const char *servicehelper;
491 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
496 if (!_dbus_string_init (&full_address))
502 /* get our limits and timeout lengths */
503 bus_config_parser_get_limits (parser, &context->limits);
506 bus_policy_unref (context->policy);
507 context->policy = bus_config_parser_steal_policy (parser);
508 _dbus_assert (context->policy != NULL);
510 /* We have to build the address backward, so that
511 * <listen> later in the config file have priority
513 link = _dbus_list_get_last_link (&context->servers);
516 addr = dbus_server_get_address (link->data);
523 if (_dbus_string_get_length (&full_address) > 0)
525 if (!_dbus_string_append (&full_address, ";"))
532 if (!_dbus_string_append (&full_address, addr))
541 link = _dbus_list_get_prev_link (&context->servers, link);
545 dbus_free (context->address);
547 if (!_dbus_string_copy_data (&full_address, &context->address))
553 /* get the service directories */
554 dirs = bus_config_parser_get_service_dirs (parser);
556 /* and the service helper */
557 servicehelper = bus_config_parser_get_servicehelper (parser);
559 s = _dbus_strdup(servicehelper);
560 if (s == NULL && servicehelper != NULL)
567 dbus_free(context->servicehelper);
568 context->servicehelper = s;
571 /* Create activation subsystem */
572 if (context->activation)
574 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
579 context->activation = bus_activation_new (context, &full_address, dirs, error);
582 if (context->activation == NULL)
584 _DBUS_ASSERT_ERROR_IS_SET (error);
588 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
592 _dbus_string_free (&full_address);
601 list_concat_new (DBusList **a,
609 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
611 if (!_dbus_list_append (result, link->data))
614 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
616 if (!_dbus_list_append (result, link->data))
622 _dbus_list_clear (result);
627 raise_file_descriptor_limit (BusContext *context)
630 /* I just picked this out of thin air; we need some extra
631 * descriptors for things like any internal pipes we create,
632 * inotify, connections to SELinux, etc.
634 unsigned int arbitrary_extra_fds = 32;
637 limit = context->limits.max_completed_connections +
638 context->limits.max_incomplete_connections
639 + arbitrary_extra_fds;
641 _dbus_request_file_descriptor_limit (limit);
645 process_config_postinit (BusContext *context,
646 BusConfigParser *parser,
649 DBusHashTable *service_context_table;
650 DBusList *watched_dirs = NULL;
652 raise_file_descriptor_limit (context);
654 service_context_table = bus_config_parser_steal_service_context_table (parser);
655 if (!bus_registry_set_service_context_table (context->registry,
656 service_context_table))
662 _dbus_hash_table_unref (service_context_table);
664 /* We need to monitor both the configuration directories and directories
665 * containing .service files.
667 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
668 bus_config_parser_get_service_dirs (parser),
675 bus_set_watched_dirs (context, &watched_dirs);
677 _dbus_list_clear (&watched_dirs);
683 bus_context_new (const DBusString *config_file,
684 ForceForkSetting force_fork,
685 DBusPipe *print_addr_pipe,
686 DBusPipe *print_pid_pipe,
687 const DBusString *address,
688 dbus_bool_t systemd_activation,
691 DBusString log_prefix;
693 BusConfigParser *parser;
695 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
700 if (!dbus_server_allocate_data_slot (&server_data_slot))
706 context = dbus_new0 (BusContext, 1);
712 context->refcount = 1;
714 _dbus_generate_uuid (&context->uuid);
716 if (!_dbus_string_copy_data (config_file, &context->config_file))
722 context->loop = _dbus_loop_new ();
723 if (context->loop == NULL)
729 context->registry = bus_registry_new (context);
730 if (context->registry == NULL)
736 parser = bus_config_load (config_file, TRUE, NULL, error);
739 _DBUS_ASSERT_ERROR_IS_SET (error);
743 if (!process_config_first_time_only (context, parser, address, systemd_activation, error))
745 _DBUS_ASSERT_ERROR_IS_SET (error);
748 if (!process_config_every_time (context, parser, FALSE, error))
750 _DBUS_ASSERT_ERROR_IS_SET (error);
754 /* we need another ref of the server data slot for the context
757 if (!dbus_server_allocate_data_slot (&server_data_slot))
758 _dbus_assert_not_reached ("second ref of server data slot failed");
760 /* Note that we don't know whether the print_addr_pipe is
761 * one of the sockets we're using to listen on, or some
762 * other random thing. But I think the answer is "don't do
765 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
768 const char *a = bus_context_get_address (context);
771 _dbus_assert (a != NULL);
772 if (!_dbus_string_init (&addr))
778 if (!_dbus_string_append (&addr, a) ||
779 !_dbus_string_append (&addr, "\n"))
781 _dbus_string_free (&addr);
786 bytes = _dbus_string_get_length (&addr);
787 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
789 /* pipe write returns an error on failure but not short write */
790 if (error != NULL && !dbus_error_is_set (error))
792 dbus_set_error (error, DBUS_ERROR_FAILED,
793 "Printing message bus address: did not write all bytes\n");
795 _dbus_string_free (&addr);
799 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
800 _dbus_pipe_close (print_addr_pipe, NULL);
802 _dbus_string_free (&addr);
805 context->connections = bus_connections_new (context);
806 if (context->connections == NULL)
812 context->matchmaker = bus_matchmaker_new ();
813 if (context->matchmaker == NULL)
819 /* check user before we fork */
820 if (context->user != NULL)
822 if (!_dbus_verify_daemon_user (context->user))
824 dbus_set_error (error, DBUS_ERROR_FAILED,
825 "Could not get UID and GID for username \"%s\"",
831 /* Now become a daemon if appropriate and write out pid file in any case */
835 if (context->pidfile)
836 _dbus_string_init_const (&u, context->pidfile);
838 if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
840 _dbus_verbose ("Forking and becoming daemon\n");
842 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
845 context->keep_umask))
847 _DBUS_ASSERT_ERROR_IS_SET (error);
853 _dbus_verbose ("Fork not requested\n");
855 /* Need to write PID file and to PID pipe for ourselves,
856 * not for the child process. This is a no-op if the pidfile
857 * is NULL and print_pid_pipe is NULL.
859 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
864 _DBUS_ASSERT_ERROR_IS_SET (error);
870 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
871 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
872 _dbus_pipe_close (print_pid_pipe, NULL);
874 if (!bus_selinux_full_init ())
876 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but AVC initialization failed; check system log\n");
879 if (!process_config_postinit (context, parser, error))
881 _DBUS_ASSERT_ERROR_IS_SET (error);
887 bus_config_parser_unref (parser);
891 /* Here we change our credentials if required,
892 * as soon as we've set up our sockets and pidfile
894 if (context->user != NULL)
896 if (!_dbus_change_to_daemon_user (context->user, error))
898 _DBUS_ASSERT_ERROR_IS_SET (error);
903 /* FIXME - why not just put this in full_init() below? */
904 bus_selinux_audit_init ();
908 dbus_server_free_data_slot (&server_data_slot);
914 bus_config_parser_unref (parser);
916 bus_context_unref (context);
918 if (server_data_slot >= 0)
919 dbus_server_free_data_slot (&server_data_slot);
925 bus_context_get_id (BusContext *context,
928 return _dbus_uuid_encode (&context->uuid, uuid);
932 bus_context_reload_config (BusContext *context,
935 BusConfigParser *parser;
936 DBusString config_file;
939 /* Flush the user database cache */
940 _dbus_flush_caches ();
943 _dbus_string_init_const (&config_file, context->config_file);
944 parser = bus_config_load (&config_file, TRUE, NULL, error);
947 _DBUS_ASSERT_ERROR_IS_SET (error);
951 if (!process_config_every_time (context, parser, TRUE, error))
953 _DBUS_ASSERT_ERROR_IS_SET (error);
956 if (!process_config_postinit (context, parser, error))
958 _DBUS_ASSERT_ERROR_IS_SET (error);
963 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
966 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
968 bus_config_parser_unref (parser);
973 shutdown_server (BusContext *context,
976 if (server == NULL ||
977 !dbus_server_get_is_connected (server))
980 if (!dbus_server_set_watch_functions (server,
984 _dbus_assert_not_reached ("setting watch functions to NULL failed");
986 if (!dbus_server_set_timeout_functions (server,
990 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
992 dbus_server_disconnect (server);
996 bus_context_shutdown (BusContext *context)
1000 link = _dbus_list_get_first_link (&context->servers);
1001 while (link != NULL)
1003 shutdown_server (context, link->data);
1005 link = _dbus_list_get_next_link (&context->servers, link);
1010 bus_context_ref (BusContext *context)
1012 _dbus_assert (context->refcount > 0);
1013 context->refcount += 1;
1019 bus_context_unref (BusContext *context)
1021 _dbus_assert (context->refcount > 0);
1022 context->refcount -= 1;
1024 if (context->refcount == 0)
1028 _dbus_verbose ("Finalizing bus context %p\n", context);
1030 bus_context_shutdown (context);
1032 if (context->connections)
1034 bus_connections_unref (context->connections);
1035 context->connections = NULL;
1038 if (context->registry)
1040 bus_registry_unref (context->registry);
1041 context->registry = NULL;
1044 if (context->activation)
1046 bus_activation_unref (context->activation);
1047 context->activation = NULL;
1050 link = _dbus_list_get_first_link (&context->servers);
1051 while (link != NULL)
1053 dbus_server_unref (link->data);
1055 link = _dbus_list_get_next_link (&context->servers, link);
1057 _dbus_list_clear (&context->servers);
1059 if (context->policy)
1061 bus_policy_unref (context->policy);
1062 context->policy = NULL;
1067 _dbus_loop_unref (context->loop);
1068 context->loop = NULL;
1071 if (context->matchmaker)
1073 bus_matchmaker_unref (context->matchmaker);
1074 context->matchmaker = NULL;
1077 dbus_free (context->config_file);
1078 dbus_free (context->log_prefix);
1079 dbus_free (context->type);
1080 dbus_free (context->address);
1081 dbus_free (context->user);
1082 dbus_free (context->servicehelper);
1084 if (context->pidfile)
1087 _dbus_string_init_const (&u, context->pidfile);
1089 /* Deliberately ignore errors here, since there's not much
1090 * we can do about it, and we're exiting anyways.
1092 _dbus_delete_file (&u, NULL);
1094 dbus_free (context->pidfile);
1096 dbus_free (context);
1098 dbus_server_free_data_slot (&server_data_slot);
1102 /* type may be NULL */
1104 bus_context_get_type (BusContext *context)
1106 return context->type;
1110 bus_context_get_address (BusContext *context)
1112 return context->address;
1116 bus_context_get_servicehelper (BusContext *context)
1118 return context->servicehelper;
1122 bus_context_get_systemd_activation (BusContext *context)
1124 return context->systemd_activation;
1128 bus_context_get_registry (BusContext *context)
1130 return context->registry;
1134 bus_context_get_connections (BusContext *context)
1136 return context->connections;
1140 bus_context_get_activation (BusContext *context)
1142 return context->activation;
1146 bus_context_get_matchmaker (BusContext *context)
1148 return context->matchmaker;
1152 bus_context_get_loop (BusContext *context)
1154 return context->loop;
1158 bus_context_allow_unix_user (BusContext *context,
1161 return bus_policy_allow_unix_user (context->policy,
1165 /* For now this is never actually called because the default
1166 * DBusConnection behavior of 'same user that owns the bus can connect'
1167 * is all it would do.
1170 bus_context_allow_windows_user (BusContext *context,
1171 const char *windows_sid)
1173 return bus_policy_allow_windows_user (context->policy,
1178 bus_context_get_policy (BusContext *context)
1180 return context->policy;
1184 bus_context_create_client_policy (BusContext *context,
1185 DBusConnection *connection,
1188 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1189 return bus_policy_create_client_policy (context->policy, connection,
1194 bus_context_get_activation_timeout (BusContext *context)
1197 return context->limits.activation_timeout;
1201 bus_context_get_auth_timeout (BusContext *context)
1203 return context->limits.auth_timeout;
1207 bus_context_get_max_completed_connections (BusContext *context)
1209 return context->limits.max_completed_connections;
1213 bus_context_get_max_incomplete_connections (BusContext *context)
1215 return context->limits.max_incomplete_connections;
1219 bus_context_get_max_connections_per_user (BusContext *context)
1221 return context->limits.max_connections_per_user;
1225 bus_context_get_max_pending_activations (BusContext *context)
1227 return context->limits.max_pending_activations;
1231 bus_context_get_max_services_per_connection (BusContext *context)
1233 return context->limits.max_services_per_connection;
1237 bus_context_get_max_match_rules_per_connection (BusContext *context)
1239 return context->limits.max_match_rules_per_connection;
1243 bus_context_get_max_replies_per_connection (BusContext *context)
1245 return context->limits.max_replies_per_connection;
1249 bus_context_get_reply_timeout (BusContext *context)
1251 return context->limits.reply_timeout;
1255 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
1258 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1262 if (!context->syslog)
1264 /* we're not syslogging; just output to stderr */
1265 va_start (args, msg);
1266 vfprintf (stderr, msg, args);
1267 fprintf (stderr, "\n");
1272 va_start (args, msg);
1274 if (context->log_prefix)
1276 DBusString full_msg;
1278 if (!_dbus_string_init (&full_msg))
1280 if (!_dbus_string_append (&full_msg, context->log_prefix))
1282 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1285 _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1287 _dbus_string_free (&full_msg);
1290 _dbus_system_logv (severity, msg, args);
1296 static inline const char *
1297 nonnull (const char *maybe_null,
1298 const char *if_null)
1300 return (maybe_null ? maybe_null : if_null);
1304 * Log something about a message, usually that it was rejected.
1307 complain_about_message (BusContext *context,
1308 const char *error_name,
1309 const char *complaint,
1311 DBusMessage *message,
1312 DBusConnection *sender,
1313 DBusConnection *proposed_recipient,
1314 dbus_bool_t requested_reply,
1318 DBusError stack_error = DBUS_ERROR_INIT;
1319 const char *sender_name;
1320 const char *sender_loginfo;
1321 const char *proposed_recipient_loginfo;
1323 if (error == NULL && !log)
1328 sender_name = bus_connection_get_name (sender);
1329 sender_loginfo = bus_connection_get_loginfo (sender);
1333 sender_name = "(unset)";
1334 sender_loginfo = "(bus)";
1337 if (proposed_recipient != NULL)
1338 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1340 proposed_recipient_loginfo = "bus";
1342 dbus_set_error (&stack_error, error_name,
1343 "%s, %d matched rules; type=\"%s\", sender=\"%s\" (%s) "
1344 "interface=\"%s\" member=\"%s\" error name=\"%s\" "
1345 "requested_reply=\"%d\" destination=\"%s\" (%s)",
1348 dbus_message_type_to_string (dbus_message_get_type (message)),
1351 nonnull (dbus_message_get_interface (message), "(unset)"),
1352 nonnull (dbus_message_get_member (message), "(unset)"),
1353 nonnull (dbus_message_get_error_name (message), "(unset)"),
1355 nonnull (dbus_message_get_destination (message), DBUS_SERVICE_DBUS),
1356 proposed_recipient_loginfo);
1358 /* If we hit OOM while setting the error, this will syslog "out of memory"
1359 * which is itself an indication that something is seriously wrong */
1361 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, "%s",
1362 stack_error.message);
1364 dbus_move_error (&stack_error, error);
1368 * addressed_recipient is the recipient specified in the message.
1370 * proposed_recipient is the recipient we're considering sending
1371 * to right this second, and may be an eavesdropper.
1373 * sender is the sender of the message.
1375 * NULL for proposed_recipient or sender definitely means the bus driver.
1377 * NULL for addressed_recipient may mean the bus driver, or may mean
1378 * no destination was specified in the message (e.g. a signal).
1381 bus_context_check_security_policy (BusContext *context,
1382 BusTransaction *transaction,
1383 DBusConnection *sender,
1384 DBusConnection *addressed_recipient,
1385 DBusConnection *proposed_recipient,
1386 DBusMessage *message,
1390 BusClientPolicy *sender_policy;
1391 BusClientPolicy *recipient_policy;
1392 dbus_int32_t toggles;
1395 dbus_bool_t requested_reply;
1396 const char *sender_name;
1397 const char *sender_loginfo;
1398 const char *proposed_recipient_loginfo;
1400 type = dbus_message_get_type (message);
1401 dest = dbus_message_get_destination (message);
1403 /* dispatch.c was supposed to ensure these invariants */
1404 _dbus_assert (dest != NULL ||
1405 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1406 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1407 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1408 addressed_recipient != NULL ||
1409 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1413 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1414 case DBUS_MESSAGE_TYPE_SIGNAL:
1415 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1416 case DBUS_MESSAGE_TYPE_ERROR:
1420 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1423 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1424 "Message bus will not accept messages of unknown type\n");
1429 requested_reply = FALSE;
1433 /* First verify the SELinux access controls. If allowed then
1434 * go on with the standard checks.
1436 if (!bus_selinux_allows_send (sender, proposed_recipient,
1437 dbus_message_type_to_string (dbus_message_get_type (message)),
1438 dbus_message_get_interface (message),
1439 dbus_message_get_member (message),
1440 dbus_message_get_error_name (message),
1441 dest ? dest : DBUS_SERVICE_DBUS, error))
1443 if (error != NULL && !dbus_error_is_set (error))
1445 /* don't syslog this, just set the error: avc_has_perm should
1446 * have already written to either the audit log or syslog */
1447 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1448 "An SELinux policy prevents this sender from sending this "
1449 "message to this recipient",
1450 0, message, sender, proposed_recipient, FALSE, FALSE, error);
1451 _dbus_verbose ("SELinux security check denying send to service\n");
1457 if (bus_connection_is_active (sender))
1459 sender_policy = bus_connection_get_policy (sender);
1460 _dbus_assert (sender_policy != NULL);
1462 /* Fill in requested_reply variable with TRUE if this is a
1463 * reply and the reply was pending.
1465 if (dbus_message_get_reply_serial (message) != 0)
1467 if (proposed_recipient != NULL /* not to the bus driver */ &&
1468 addressed_recipient == proposed_recipient /* not eavesdropping */)
1472 dbus_error_init (&error2);
1473 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1475 sender, addressed_recipient, message,
1477 if (dbus_error_is_set (&error2))
1479 dbus_move_error (&error2, error);
1487 /* Policy for inactive connections is that they can only send
1488 * the hello message to the bus driver
1490 if (proposed_recipient == NULL &&
1491 dbus_message_is_method_call (message,
1492 DBUS_INTERFACE_DBUS,
1495 _dbus_verbose ("security check allowing %s message\n",
1501 _dbus_verbose ("security check disallowing non-%s message\n",
1504 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1505 "Client tried to send a message other than %s without being registered",
1514 sender_policy = NULL;
1516 /* If the sender is the bus driver, we assume any reply was a
1517 * requested reply as bus driver won't send bogus ones
1519 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1520 dbus_message_get_reply_serial (message) != 0)
1521 requested_reply = TRUE;
1524 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1525 (sender == NULL && sender_policy == NULL));
1527 if (proposed_recipient != NULL)
1529 /* only the bus driver can send to an inactive recipient (as it
1530 * owns no services, so other apps can't address it). Inactive
1531 * recipients can receive any message.
1533 if (bus_connection_is_active (proposed_recipient))
1535 recipient_policy = bus_connection_get_policy (proposed_recipient);
1536 _dbus_assert (recipient_policy != NULL);
1538 else if (sender == NULL)
1540 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1541 recipient_policy = NULL;
1545 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1546 recipient_policy = NULL;
1550 recipient_policy = NULL;
1552 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1553 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1554 (proposed_recipient == NULL && recipient_policy == NULL));
1557 if (sender_policy &&
1558 !bus_client_policy_check_can_send (sender_policy,
1562 message, &toggles, &log))
1564 const char *msg = "Rejected send message, %d matched rules; "
1565 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))";
1567 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1568 "Rejected send message", toggles,
1569 message, sender, proposed_recipient, requested_reply,
1570 (addressed_recipient == proposed_recipient), error);
1571 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1577 /* We want to drop this message, and are only not doing so for backwards
1579 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1580 "Would reject message", toggles,
1581 message, sender, proposed_recipient, requested_reply,
1585 if (recipient_policy &&
1586 !bus_client_policy_check_can_receive (recipient_policy,
1590 addressed_recipient, proposed_recipient,
1593 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1594 "Rejected receive message", toggles,
1595 message, sender, proposed_recipient, requested_reply,
1596 (addressed_recipient == proposed_recipient), NULL);
1597 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1601 /* See if limits on size have been exceeded */
1602 if (proposed_recipient &&
1603 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1604 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1606 complain_about_message (context, DBUS_ERROR_LIMITS_EXCEEDED,
1607 "Rejected: destination has a full message queue",
1608 0, message, sender, proposed_recipient, requested_reply, TRUE,
1610 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1614 /* Record that we will allow a reply here in the future (don't
1615 * bother if the recipient is the bus or this is an eavesdropping
1616 * connection). Only the addressed recipient may reply.
1618 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1620 addressed_recipient &&
1621 addressed_recipient == proposed_recipient && /* not eavesdropping */
1622 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1624 sender, addressed_recipient,
1627 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1631 _dbus_verbose ("security policy allowing message\n");