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"
39 #include "dir-watch.h"
40 #include <dbus/dbus-list.h>
41 #include <dbus/dbus-hash.h>
42 #include <dbus/dbus-credentials.h>
43 #include <dbus/dbus-internals.h>
44 #include <dbus/dbus-server-protected.h>
51 #include <systemd/sd-daemon.h>
67 BusConnections *connections;
68 BusActivation *activation;
69 BusRegistry *registry;
71 BusMatchmaker *matchmaker;
73 DBusRLimit *initial_fd_limit;
74 unsigned int fork : 1;
75 unsigned int syslog : 1;
76 unsigned int keep_umask : 1;
77 unsigned int allow_anonymous : 1;
78 unsigned int systemd_activation : 1;
79 dbus_bool_t watches_enabled;
82 static dbus_int32_t server_data_slot = -1;
89 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
92 server_get_context (DBusServer *server)
97 /* this data slot was allocated by the BusContext */
98 _dbus_assert (server_data_slot >= 0);
100 bd = BUS_SERVER_DATA (server);
102 /* every DBusServer in the dbus-daemon has gone through setup_server() */
103 _dbus_assert (bd != NULL);
105 context = bd->context;
111 add_server_watch (DBusWatch *watch,
114 DBusServer *server = data;
117 context = server_get_context (server);
119 return _dbus_loop_add_watch (context->loop, watch);
123 remove_server_watch (DBusWatch *watch,
126 DBusServer *server = data;
129 context = server_get_context (server);
131 _dbus_loop_remove_watch (context->loop, watch);
135 toggle_server_watch (DBusWatch *watch,
138 DBusServer *server = data;
141 context = server_get_context (server);
143 _dbus_loop_toggle_watch (context->loop, watch);
147 add_server_timeout (DBusTimeout *timeout,
150 DBusServer *server = data;
153 context = server_get_context (server);
155 return _dbus_loop_add_timeout (context->loop, timeout);
159 remove_server_timeout (DBusTimeout *timeout,
162 DBusServer *server = data;
165 context = server_get_context (server);
167 _dbus_loop_remove_timeout (context->loop, timeout);
171 new_connection_callback (DBusServer *server,
172 DBusConnection *new_connection,
175 BusContext *context = data;
177 if (!bus_connections_setup_connection (context->connections, new_connection))
179 _dbus_verbose ("No memory to setup new connection\n");
181 /* if we don't do this, it will get unref'd without
182 * being disconnected... kind of strange really
183 * that we have to do this, people won't get it right
186 dbus_connection_close (new_connection);
189 dbus_connection_set_max_received_size (new_connection,
190 context->limits.max_incoming_bytes);
192 dbus_connection_set_max_message_size (new_connection,
193 context->limits.max_message_size);
195 dbus_connection_set_max_received_unix_fds (new_connection,
196 context->limits.max_incoming_unix_fds);
198 dbus_connection_set_max_message_unix_fds (new_connection,
199 context->limits.max_message_unix_fds);
201 dbus_connection_set_allow_anonymous (new_connection,
202 context->allow_anonymous);
204 /* on OOM, we won't have ref'd the connection so it will die. */
208 free_server_data (void *data)
210 BusServerData *bd = data;
216 setup_server (BusContext *context,
218 char **auth_mechanisms,
223 bd = dbus_new0 (BusServerData, 1);
224 if (bd == NULL || !dbus_server_set_data (server,
226 bd, free_server_data))
233 bd->context = context;
235 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
241 dbus_server_set_new_connection_function (server,
242 new_connection_callback,
245 if (!dbus_server_set_watch_functions (server,
256 if (!dbus_server_set_timeout_functions (server,
258 remove_server_timeout,
269 /* This code only gets executed the first time the
270 * config files are parsed. It is not executed
271 * when config files are reloaded.
274 process_config_first_time_only (BusContext *context,
275 BusConfigParser *parser,
276 const DBusString *address,
277 BusContextFlags flags,
280 DBusString log_prefix;
282 DBusList **addresses;
283 const char *user, *pidfile;
284 char **auth_mechanisms;
285 DBusList **auth_mechanisms_list;
289 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
292 auth_mechanisms = NULL;
295 context->syslog = bus_config_parser_get_syslog (parser);
300 /* If running under systemd, we don't want to log to both stderr and
301 * syslog, because our stderr is probably connected to journald too,
302 * so we'd duplicate all our messages. */
303 if (sd_booted () > 0)
304 _dbus_init_system_log ("dbus-daemon", DBUS_LOG_FLAGS_SYSTEM_LOG);
307 _dbus_init_system_log ("dbus-daemon",
308 DBUS_LOG_FLAGS_SYSTEM_LOG | DBUS_LOG_FLAGS_STDERR);
312 _dbus_init_system_log ("dbus-daemon", DBUS_LOG_FLAGS_STDERR);
315 if (flags & BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION)
316 context->systemd_activation = TRUE;
318 context->systemd_activation = FALSE;
320 /* Check for an existing pid file. Of course this is a race;
321 * we'd have to use fcntl() locks on the pid file to
322 * avoid that. But we want to check for the pid file
323 * before overwriting any existing sockets, etc.
326 if (flags & BUS_CONTEXT_FLAG_WRITE_PID_FILE)
327 pidfile = bus_config_parser_get_pidfile (parser);
334 _dbus_string_init_const (&u, pidfile);
336 if (_dbus_stat (&u, &stbuf, NULL))
342 _dbus_string_init (&p);
343 _dbus_file_get_contents(&p, &u, NULL);
344 _dbus_string_parse_int(&p, 0, &pid, NULL);
345 _dbus_string_free(&p);
347 if ((kill((int)pid, 0))) {
348 dbus_set_error(NULL, DBUS_ERROR_FILE_EXISTS,
349 "pid %ld not running, removing stale pid file\n",
351 _dbus_delete_file(&u, NULL);
354 dbus_set_error (error, DBUS_ERROR_FAILED,
355 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
364 /* keep around the pid filename so we can delete it later */
365 context->pidfile = _dbus_strdup (pidfile);
367 /* note that type may be NULL */
368 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
369 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
372 user = bus_config_parser_get_user (parser);
375 context->user = _dbus_strdup (user);
376 if (context->user == NULL)
380 /* Set up the prefix for syslog messages */
381 if (!_dbus_string_init (&log_prefix))
383 if (context->type && !strcmp (context->type, "system"))
385 if (!_dbus_string_append (&log_prefix, "[system] "))
388 else if (context->type && !strcmp (context->type, "session"))
390 DBusCredentials *credentials;
392 credentials = _dbus_credentials_new_from_current_process ();
395 if (!_dbus_string_append (&log_prefix, "[session "))
397 _dbus_credentials_unref (credentials);
400 if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
402 _dbus_credentials_unref (credentials);
405 if (!_dbus_string_append (&log_prefix, "] "))
407 _dbus_credentials_unref (credentials);
410 _dbus_credentials_unref (credentials);
412 if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
414 _dbus_string_free (&log_prefix);
416 /* Build an array of auth mechanisms */
418 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
419 len = _dbus_list_get_length (auth_mechanisms_list);
425 auth_mechanisms = dbus_new0 (char*, len + 1);
426 if (auth_mechanisms == NULL)
430 link = _dbus_list_get_first_link (auth_mechanisms_list);
433 auth_mechanisms[i] = _dbus_strdup (link->data);
434 if (auth_mechanisms[i] == NULL)
436 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
442 auth_mechanisms = NULL;
445 /* Listen on our addresses */
451 server = dbus_server_listen (_dbus_string_get_const_data(address), error);
454 _DBUS_ASSERT_ERROR_IS_SET (error);
457 else if (!setup_server (context, server, auth_mechanisms, error))
459 _DBUS_ASSERT_ERROR_IS_SET (error);
463 if (!_dbus_list_append (&context->servers, server))
468 addresses = bus_config_parser_get_addresses (parser);
470 link = _dbus_list_get_first_link (addresses);
475 server = dbus_server_listen (link->data, error);
478 _DBUS_ASSERT_ERROR_IS_SET (error);
481 else if (!setup_server (context, server, auth_mechanisms, error))
483 _DBUS_ASSERT_ERROR_IS_SET (error);
487 if (!_dbus_list_append (&context->servers, server))
490 link = _dbus_list_get_next_link (addresses, link);
494 context->fork = bus_config_parser_get_fork (parser);
495 context->keep_umask = bus_config_parser_get_keep_umask (parser);
496 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
498 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
502 dbus_free_string_array (auth_mechanisms);
507 dbus_free_string_array (auth_mechanisms);
511 /* This code gets executed every time the config files
512 * are parsed: both during BusContext construction
513 * and on reloads. This function is slightly screwy
514 * since it can do a "half reload" in out-of-memory
515 * situations. Realistically, unlikely to ever matter.
518 process_config_every_time (BusContext *context,
519 BusConfigParser *parser,
520 dbus_bool_t is_reload,
523 DBusString full_address;
527 const char *servicehelper;
532 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
537 if (!_dbus_string_init (&full_address))
543 /* get our limits and timeout lengths */
544 bus_config_parser_get_limits (parser, &context->limits);
547 bus_policy_unref (context->policy);
548 context->policy = bus_config_parser_steal_policy (parser);
549 _dbus_assert (context->policy != NULL);
551 /* context->connections is NULL when creating new BusContext */
552 if (context->connections)
554 _dbus_verbose ("Reload policy rules for completed connections\n");
555 retval = bus_connections_reload_policy (context->connections, error);
558 _DBUS_ASSERT_ERROR_IS_SET (error);
563 /* We have to build the address backward, so that
564 * <listen> later in the config file have priority
566 link = _dbus_list_get_last_link (&context->servers);
569 addr = dbus_server_get_address (link->data);
576 if (_dbus_string_get_length (&full_address) > 0)
578 if (!_dbus_string_append (&full_address, ";"))
585 if (!_dbus_string_append (&full_address, addr))
594 link = _dbus_list_get_prev_link (&context->servers, link);
598 dbus_free (context->address);
600 if (!_dbus_string_copy_data (&full_address, &context->address))
606 /* get the service directories */
607 dirs = bus_config_parser_get_service_dirs (parser);
609 /* and the service helper */
610 servicehelper = bus_config_parser_get_servicehelper (parser);
612 s = _dbus_strdup(servicehelper);
613 if (s == NULL && servicehelper != NULL)
620 dbus_free(context->servicehelper);
621 context->servicehelper = s;
624 /* Create activation subsystem */
625 if (context->activation)
627 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
632 context->activation = bus_activation_new (context, &full_address, dirs, error);
635 if (context->activation == NULL)
637 _DBUS_ASSERT_ERROR_IS_SET (error);
641 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
645 _dbus_string_free (&full_address);
654 list_concat_new (DBusList **a,
662 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
664 if (!_dbus_list_append (result, link->data))
667 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
669 if (!_dbus_list_append (result, link->data))
675 _dbus_list_clear (result);
680 raise_file_descriptor_limit (BusContext *context)
683 DBusError error = DBUS_ERROR_INIT;
685 /* we only do this once */
686 if (context->initial_fd_limit != NULL)
689 context->initial_fd_limit = _dbus_rlimit_save_fd_limit (&error);
691 if (context->initial_fd_limit == NULL)
693 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
694 "%s: %s", error.name, error.message);
695 dbus_error_free (&error);
699 /* We used to compute a suitable rlimit based on the configured number
700 * of connections, but that breaks down as soon as we allow fd-passing,
701 * because each connection is allowed to pass 64 fds to us, and if
702 * they all did, we'd hit kernel limits. We now hard-code 64k as a
703 * good limit, like systemd does: that's enough to avoid DoS from
704 * anything short of multiple uids conspiring against us.
706 if (!_dbus_rlimit_raise_fd_limit_if_privileged (65536, &error))
708 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
709 "%s: %s", error.name, error.message);
710 dbus_error_free (&error);
717 process_config_postinit (BusContext *context,
718 BusConfigParser *parser,
721 DBusHashTable *service_context_table;
722 DBusList *watched_dirs = NULL;
724 raise_file_descriptor_limit (context);
726 service_context_table = bus_config_parser_steal_service_context_table (parser);
727 if (!bus_registry_set_service_context_table (context->registry,
728 service_context_table))
734 _dbus_hash_table_unref (service_context_table);
736 /* We need to monitor both the configuration directories and directories
737 * containing .service files.
739 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
740 bus_config_parser_get_service_dirs (parser),
747 bus_set_watched_dirs (context, &watched_dirs);
749 _dbus_list_clear (&watched_dirs);
755 bus_context_new (const DBusString *config_file,
756 BusContextFlags flags,
757 DBusPipe *print_addr_pipe,
758 DBusPipe *print_pid_pipe,
759 const DBusString *address,
763 BusConfigParser *parser;
765 _dbus_assert ((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 ||
766 (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS) == 0);
768 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
773 if (!dbus_server_allocate_data_slot (&server_data_slot))
779 context = dbus_new0 (BusContext, 1);
785 context->refcount = 1;
787 if (!_dbus_generate_uuid (&context->uuid, error))
790 if (!_dbus_string_copy_data (config_file, &context->config_file))
796 context->loop = _dbus_loop_new ();
797 if (context->loop == NULL)
803 context->watches_enabled = TRUE;
805 context->registry = bus_registry_new (context);
806 if (context->registry == NULL)
812 parser = bus_config_load (config_file, TRUE, NULL, error);
815 _DBUS_ASSERT_ERROR_IS_SET (error);
819 if (!process_config_first_time_only (context, parser, address, flags, error))
821 _DBUS_ASSERT_ERROR_IS_SET (error);
824 if (!process_config_every_time (context, parser, FALSE, error))
826 _DBUS_ASSERT_ERROR_IS_SET (error);
830 /* we need another ref of the server data slot for the context
833 if (!dbus_server_allocate_data_slot (&server_data_slot))
834 _dbus_assert_not_reached ("second ref of server data slot failed");
836 /* Note that we don't know whether the print_addr_pipe is
837 * one of the sockets we're using to listen on, or some
838 * other random thing. But I think the answer is "don't do
841 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
844 const char *a = bus_context_get_address (context);
847 _dbus_assert (a != NULL);
848 if (!_dbus_string_init (&addr))
854 if (!_dbus_string_append (&addr, a) ||
855 !_dbus_string_append (&addr, "\n"))
857 _dbus_string_free (&addr);
862 bytes = _dbus_string_get_length (&addr);
863 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
865 /* pipe write returns an error on failure but not short write */
866 if (error != NULL && !dbus_error_is_set (error))
868 dbus_set_error (error, DBUS_ERROR_FAILED,
869 "Printing message bus address: did not write all bytes\n");
871 _dbus_string_free (&addr);
875 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
876 _dbus_pipe_close (print_addr_pipe, NULL);
878 _dbus_string_free (&addr);
881 context->connections = bus_connections_new (context);
882 if (context->connections == NULL)
888 context->matchmaker = bus_matchmaker_new ();
889 if (context->matchmaker == NULL)
895 /* check user before we fork */
896 if (context->user != NULL)
898 if (!_dbus_verify_daemon_user (context->user))
900 dbus_set_error (error, DBUS_ERROR_FAILED,
901 "Could not get UID and GID for username \"%s\"",
907 /* Now become a daemon if appropriate and write out pid file in any case */
911 if (context->pidfile)
912 _dbus_string_init_const (&u, context->pidfile);
914 if (((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 && context->fork) ||
915 (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS))
917 _dbus_verbose ("Forking and becoming daemon\n");
919 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
922 context->keep_umask))
924 _DBUS_ASSERT_ERROR_IS_SET (error);
930 _dbus_verbose ("Fork not requested\n");
932 /* Need to write PID file and to PID pipe for ourselves,
933 * not for the child process. This is a no-op if the pidfile
934 * is NULL and print_pid_pipe is NULL.
936 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
941 _DBUS_ASSERT_ERROR_IS_SET (error);
947 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
948 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
949 _dbus_pipe_close (print_pid_pipe, NULL);
951 /* Here we change our credentials if required,
952 * as soon as we've set up our sockets and pidfile.
953 * This must be done before initializing LSMs, so that the netlink
954 * monitoring thread started by avc_init() will not lose CAP_AUDIT_WRITE
955 * when the main thread calls setuid().
956 * https://bugs.freedesktop.org/show_bug.cgi?id=92832
958 if (context->user != NULL)
960 if (!_dbus_change_to_daemon_user (context->user, error))
962 _DBUS_ASSERT_ERROR_IS_SET (error);
967 /* Auditing should be initialized before LSMs, so that the LSMs are able
968 * to log audit-events that happen during their initialization.
970 bus_audit_init (context);
972 if (!bus_selinux_full_init ())
974 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but D-Bus initialization failed; check system log\n");
977 if (!bus_apparmor_full_init (error))
979 _DBUS_ASSERT_ERROR_IS_SET (error);
983 if (bus_apparmor_enabled ())
985 /* Only print AppArmor mediation message when syslog support is enabled */
987 bus_context_log (context, DBUS_SYSTEM_LOG_INFO,
988 "AppArmor D-Bus mediation is enabled\n");
991 /* When SELinux is used, this must happen after bus_selinux_full_init()
992 * so that it has access to the access vector cache, which is required
993 * to process <associate/> elements.
994 * http://lists.freedesktop.org/archives/dbus/2008-October/010491.html
996 if (!process_config_postinit (context, parser, error))
998 _DBUS_ASSERT_ERROR_IS_SET (error);
1004 bus_config_parser_unref (parser);
1008 dbus_server_free_data_slot (&server_data_slot);
1014 bus_config_parser_unref (parser);
1015 if (context != NULL)
1016 bus_context_unref (context);
1018 if (server_data_slot >= 0)
1019 dbus_server_free_data_slot (&server_data_slot);
1025 bus_context_get_id (BusContext *context,
1028 return _dbus_uuid_encode (&context->uuid, uuid);
1032 bus_context_reload_config (BusContext *context,
1035 BusConfigParser *parser;
1036 DBusString config_file;
1039 /* Flush the user database cache */
1040 _dbus_flush_caches ();
1043 _dbus_string_init_const (&config_file, context->config_file);
1044 parser = bus_config_load (&config_file, TRUE, NULL, error);
1047 _DBUS_ASSERT_ERROR_IS_SET (error);
1051 if (!process_config_every_time (context, parser, TRUE, error))
1053 _DBUS_ASSERT_ERROR_IS_SET (error);
1056 if (!process_config_postinit (context, parser, error))
1058 _DBUS_ASSERT_ERROR_IS_SET (error);
1063 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
1066 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
1068 bus_config_parser_unref (parser);
1073 shutdown_server (BusContext *context,
1076 if (server == NULL ||
1077 !dbus_server_get_is_connected (server))
1080 if (!dbus_server_set_watch_functions (server,
1084 _dbus_assert_not_reached ("setting watch functions to NULL failed");
1086 if (!dbus_server_set_timeout_functions (server,
1090 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
1092 dbus_server_disconnect (server);
1096 bus_context_shutdown (BusContext *context)
1100 link = _dbus_list_get_first_link (&context->servers);
1101 while (link != NULL)
1103 shutdown_server (context, link->data);
1105 link = _dbus_list_get_next_link (&context->servers, link);
1110 bus_context_ref (BusContext *context)
1112 _dbus_assert (context->refcount > 0);
1113 context->refcount += 1;
1119 bus_context_unref (BusContext *context)
1121 _dbus_assert (context->refcount > 0);
1122 context->refcount -= 1;
1124 if (context->refcount == 0)
1128 _dbus_verbose ("Finalizing bus context %p\n", context);
1130 bus_context_shutdown (context);
1132 if (context->connections)
1134 bus_connections_unref (context->connections);
1135 context->connections = NULL;
1138 if (context->registry)
1140 bus_registry_unref (context->registry);
1141 context->registry = NULL;
1144 if (context->activation)
1146 bus_activation_unref (context->activation);
1147 context->activation = NULL;
1150 link = _dbus_list_get_first_link (&context->servers);
1151 while (link != NULL)
1153 dbus_server_unref (link->data);
1155 link = _dbus_list_get_next_link (&context->servers, link);
1157 _dbus_list_clear (&context->servers);
1159 if (context->policy)
1161 bus_policy_unref (context->policy);
1162 context->policy = NULL;
1167 _dbus_loop_unref (context->loop);
1168 context->loop = NULL;
1171 if (context->matchmaker)
1173 bus_matchmaker_unref (context->matchmaker);
1174 context->matchmaker = NULL;
1177 dbus_free (context->config_file);
1178 dbus_free (context->log_prefix);
1179 dbus_free (context->type);
1180 dbus_free (context->address);
1181 dbus_free (context->user);
1182 dbus_free (context->servicehelper);
1184 if (context->pidfile)
1187 _dbus_string_init_const (&u, context->pidfile);
1189 /* Deliberately ignore errors here, since there's not much
1190 * we can do about it, and we're exiting anyways.
1192 _dbus_delete_file (&u, NULL);
1194 dbus_free (context->pidfile);
1197 if (context->initial_fd_limit)
1198 _dbus_rlimit_free (context->initial_fd_limit);
1200 dbus_free (context);
1202 dbus_server_free_data_slot (&server_data_slot);
1206 /* type may be NULL */
1208 bus_context_get_type (BusContext *context)
1210 return context->type;
1214 bus_context_get_address (BusContext *context)
1216 return context->address;
1220 bus_context_get_servicehelper (BusContext *context)
1222 return context->servicehelper;
1226 bus_context_get_systemd_activation (BusContext *context)
1228 return context->systemd_activation;
1232 bus_context_get_registry (BusContext *context)
1234 return context->registry;
1238 bus_context_get_connections (BusContext *context)
1240 return context->connections;
1244 bus_context_get_activation (BusContext *context)
1246 return context->activation;
1250 bus_context_get_matchmaker (BusContext *context)
1252 return context->matchmaker;
1256 bus_context_get_loop (BusContext *context)
1258 return context->loop;
1262 bus_context_allow_unix_user (BusContext *context,
1265 return bus_policy_allow_unix_user (context->policy,
1269 /* For now this is never actually called because the default
1270 * DBusConnection behavior of 'same user that owns the bus can connect'
1271 * is all it would do.
1274 bus_context_allow_windows_user (BusContext *context,
1275 const char *windows_sid)
1277 return bus_policy_allow_windows_user (context->policy,
1282 bus_context_get_policy (BusContext *context)
1284 return context->policy;
1288 bus_context_create_client_policy (BusContext *context,
1289 DBusConnection *connection,
1292 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1293 return bus_policy_create_client_policy (context->policy, connection,
1298 bus_context_get_activation_timeout (BusContext *context)
1301 return context->limits.activation_timeout;
1305 bus_context_get_auth_timeout (BusContext *context)
1307 return context->limits.auth_timeout;
1311 bus_context_get_pending_fd_timeout (BusContext *context)
1313 return context->limits.pending_fd_timeout;
1317 bus_context_get_max_completed_connections (BusContext *context)
1319 return context->limits.max_completed_connections;
1323 bus_context_get_max_incomplete_connections (BusContext *context)
1325 return context->limits.max_incomplete_connections;
1329 bus_context_get_max_connections_per_user (BusContext *context)
1331 return context->limits.max_connections_per_user;
1335 bus_context_get_max_pending_activations (BusContext *context)
1337 return context->limits.max_pending_activations;
1341 bus_context_get_max_services_per_connection (BusContext *context)
1343 return context->limits.max_services_per_connection;
1347 bus_context_get_max_match_rules_per_connection (BusContext *context)
1349 return context->limits.max_match_rules_per_connection;
1353 bus_context_get_max_replies_per_connection (BusContext *context)
1355 return context->limits.max_replies_per_connection;
1359 bus_context_get_reply_timeout (BusContext *context)
1361 return context->limits.reply_timeout;
1365 bus_context_get_initial_fd_limit (BusContext *context)
1367 return context->initial_fd_limit;
1371 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1375 va_start (args, msg);
1377 if (context->log_prefix)
1379 DBusString full_msg;
1381 if (!_dbus_string_init (&full_msg))
1383 if (!_dbus_string_append (&full_msg, context->log_prefix))
1385 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1388 _dbus_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1390 _dbus_string_free (&full_msg);
1393 _dbus_logv (severity, msg, args);
1399 static inline const char *
1400 nonnull (const char *maybe_null,
1401 const char *if_null)
1403 return (maybe_null ? maybe_null : if_null);
1407 bus_context_log_literal (BusContext *context,
1408 DBusSystemLogSeverity severity,
1411 _dbus_log (severity, "%s%s", nonnull (context->log_prefix, ""), msg);
1415 bus_context_log_and_set_error (BusContext *context,
1416 DBusSystemLogSeverity severity,
1422 DBusError stack_error = DBUS_ERROR_INIT;
1425 va_start (args, msg);
1426 _dbus_set_error_valist (&stack_error, name, msg, args);
1429 /* If we hit OOM while setting the error, this will syslog "out of memory"
1430 * which is itself an indication that something is seriously wrong */
1431 bus_context_log_literal (context, DBUS_SYSTEM_LOG_SECURITY,
1432 stack_error.message);
1434 dbus_move_error (&stack_error, error);
1438 * Log something about a message, usually that it was rejected.
1441 complain_about_message (BusContext *context,
1442 const char *error_name,
1443 const char *complaint,
1445 DBusMessage *message,
1446 DBusConnection *sender,
1447 DBusConnection *proposed_recipient,
1448 dbus_bool_t requested_reply,
1452 DBusError stack_error = DBUS_ERROR_INIT;
1453 const char *sender_name;
1454 const char *sender_loginfo;
1455 const char *proposed_recipient_loginfo;
1457 if (error == NULL && !log)
1462 sender_name = bus_connection_get_name (sender);
1463 sender_loginfo = bus_connection_get_loginfo (sender);
1467 sender_name = "(unset)";
1468 sender_loginfo = "(bus)";
1471 if (proposed_recipient != NULL)
1472 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1474 proposed_recipient_loginfo = "bus";
1476 dbus_set_error (&stack_error, error_name,
1477 "%s, %d matched rules; type=\"%s\", sender=\"%s\" (%s) "
1478 "interface=\"%s\" member=\"%s\" error name=\"%s\" "
1479 "requested_reply=\"%d\" destination=\"%s\" (%s)",
1482 dbus_message_type_to_string (dbus_message_get_type (message)),
1485 nonnull (dbus_message_get_interface (message), "(unset)"),
1486 nonnull (dbus_message_get_member (message), "(unset)"),
1487 nonnull (dbus_message_get_error_name (message), "(unset)"),
1489 nonnull (dbus_message_get_destination (message), DBUS_SERVICE_DBUS),
1490 proposed_recipient_loginfo);
1492 /* If we hit OOM while setting the error, this will syslog "out of memory"
1493 * which is itself an indication that something is seriously wrong */
1495 bus_context_log_literal (context, DBUS_SYSTEM_LOG_SECURITY,
1496 stack_error.message);
1498 dbus_move_error (&stack_error, error);
1502 * addressed_recipient is the recipient specified in the message.
1504 * proposed_recipient is the recipient we're considering sending
1505 * to right this second, and may be an eavesdropper.
1507 * sender is the sender of the message.
1509 * NULL for proposed_recipient or sender definitely means the bus driver.
1511 * NULL for addressed_recipient may mean the bus driver, or may mean
1512 * no destination was specified in the message (e.g. a signal).
1515 bus_context_check_security_policy (BusContext *context,
1516 BusTransaction *transaction,
1517 DBusConnection *sender,
1518 DBusConnection *addressed_recipient,
1519 DBusConnection *proposed_recipient,
1520 DBusMessage *message,
1523 const char *src, *dest;
1524 BusClientPolicy *sender_policy;
1525 BusClientPolicy *recipient_policy;
1526 dbus_int32_t toggles;
1529 dbus_bool_t requested_reply;
1531 type = dbus_message_get_type (message);
1532 src = dbus_message_get_sender (message);
1533 dest = dbus_message_get_destination (message);
1535 /* dispatch.c was supposed to ensure these invariants */
1536 _dbus_assert (dest != NULL ||
1537 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1538 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1539 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1540 addressed_recipient != NULL ||
1541 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1545 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1546 case DBUS_MESSAGE_TYPE_SIGNAL:
1547 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1548 case DBUS_MESSAGE_TYPE_ERROR:
1552 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1555 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1556 "Message bus will not accept messages of unknown type\n");
1561 requested_reply = FALSE;
1565 if (bus_connection_is_active (sender))
1567 sender_policy = bus_connection_get_policy (sender);
1568 _dbus_assert (sender_policy != NULL);
1570 /* Fill in requested_reply variable with TRUE if this is a
1571 * reply and the reply was pending.
1573 if (dbus_message_get_reply_serial (message) != 0)
1575 if (proposed_recipient != NULL /* not to the bus driver */ &&
1576 addressed_recipient == proposed_recipient /* not eavesdropping */)
1580 dbus_error_init (&error2);
1581 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1583 sender, addressed_recipient, message,
1585 if (dbus_error_is_set (&error2))
1587 dbus_move_error (&error2, error);
1595 sender_policy = NULL;
1598 /* First verify the SELinux access controls. If allowed then
1599 * go on with the standard checks.
1601 if (!bus_selinux_allows_send (sender, proposed_recipient,
1602 dbus_message_type_to_string (dbus_message_get_type (message)),
1603 dbus_message_get_interface (message),
1604 dbus_message_get_member (message),
1605 dbus_message_get_error_name (message),
1606 dest ? dest : DBUS_SERVICE_DBUS, error))
1608 if (error != NULL && !dbus_error_is_set (error))
1610 /* don't syslog this, just set the error: avc_has_perm should
1611 * have already written to either the audit log or syslog */
1612 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1613 "An SELinux policy prevents this sender from sending this "
1614 "message to this recipient",
1615 0, message, sender, proposed_recipient, FALSE, FALSE, error);
1616 _dbus_verbose ("SELinux security check denying send to service\n");
1622 /* next verify AppArmor access controls. If allowed then
1623 * go on with the standard checks.
1625 if (!bus_apparmor_allows_send (sender, proposed_recipient,
1627 bus_context_get_type (context),
1628 dbus_message_get_type (message),
1629 dbus_message_get_path (message),
1630 dbus_message_get_interface (message),
1631 dbus_message_get_member (message),
1632 dbus_message_get_error_name (message),
1633 dest ? dest : DBUS_SERVICE_DBUS,
1634 src ? src : DBUS_SERVICE_DBUS,
1638 if (!bus_connection_is_active (sender))
1640 /* Policy for inactive connections is that they can only send
1641 * the hello message to the bus driver
1643 if (proposed_recipient == NULL &&
1644 dbus_message_is_method_call (message,
1645 DBUS_INTERFACE_DBUS,
1648 _dbus_verbose ("security check allowing %s message\n",
1654 _dbus_verbose ("security check disallowing non-%s message\n",
1657 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1658 "Client tried to send a message other than %s without being registered",
1667 sender_policy = NULL;
1669 /* If the sender is the bus driver, we assume any reply was a
1670 * requested reply as bus driver won't send bogus ones
1672 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1673 dbus_message_get_reply_serial (message) != 0)
1674 requested_reply = TRUE;
1677 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1678 (sender == NULL && sender_policy == NULL));
1680 if (proposed_recipient != NULL)
1682 /* only the bus driver can send to an inactive recipient (as it
1683 * owns no services, so other apps can't address it). Inactive
1684 * recipients can receive any message.
1686 if (bus_connection_is_active (proposed_recipient))
1688 recipient_policy = bus_connection_get_policy (proposed_recipient);
1689 _dbus_assert (recipient_policy != NULL);
1691 else if (sender == NULL)
1693 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1694 recipient_policy = NULL;
1698 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus");
1699 recipient_policy = NULL;
1703 recipient_policy = NULL;
1705 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1706 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1707 (proposed_recipient == NULL && recipient_policy == NULL));
1710 if (sender_policy &&
1711 !bus_client_policy_check_can_send (sender_policy,
1715 message, &toggles, &log))
1717 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1718 "Rejected send message", toggles,
1719 message, sender, proposed_recipient, requested_reply,
1720 (addressed_recipient == proposed_recipient), error);
1721 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1727 /* We want to drop this message, and are only not doing so for backwards
1729 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1730 "Would reject message", toggles,
1731 message, sender, proposed_recipient, requested_reply,
1735 if (recipient_policy &&
1736 !bus_client_policy_check_can_receive (recipient_policy,
1740 addressed_recipient, proposed_recipient,
1743 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1744 "Rejected receive message", toggles,
1745 message, sender, proposed_recipient, requested_reply,
1746 (addressed_recipient == proposed_recipient), error);
1747 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1751 /* See if limits on size have been exceeded */
1752 if (proposed_recipient &&
1753 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1754 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1756 complain_about_message (context, DBUS_ERROR_LIMITS_EXCEEDED,
1757 "Rejected: destination has a full message queue",
1758 0, message, sender, proposed_recipient, requested_reply, TRUE,
1760 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1764 /* Record that we will allow a reply here in the future (don't
1765 * bother if the recipient is the bus or this is an eavesdropping
1766 * connection). Only the addressed recipient may reply.
1768 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1770 addressed_recipient &&
1771 addressed_recipient == proposed_recipient && /* not eavesdropping */
1772 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1774 sender, addressed_recipient,
1777 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1781 _dbus_verbose ("security policy allowing message\n");
1786 bus_context_check_all_watches (BusContext *context)
1789 dbus_bool_t enabled = TRUE;
1791 if (bus_connections_get_n_incomplete (context->connections) >=
1792 bus_context_get_max_incomplete_connections (context))
1797 if (context->watches_enabled == enabled)
1800 context->watches_enabled = enabled;
1802 for (link = _dbus_list_get_first_link (&context->servers);
1804 link = _dbus_list_get_next_link (&context->servers, link))
1806 /* A BusContext might contains several DBusServer (if there are
1807 * several <listen> configuration items) and a DBusServer might
1808 * contain several DBusWatch in its DBusWatchList (if getaddrinfo
1809 * returns several addresses on a dual IPv4-IPv6 stack or if
1810 * systemd passes several fds).
1811 * We want to enable/disable them all.
1813 DBusServer *server = link->data;
1814 _dbus_server_toggle_all_watches (server, enabled);