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>
63 BusConnections *connections;
64 BusActivation *activation;
65 BusRegistry *registry;
67 BusMatchmaker *matchmaker;
69 DBusRLimit *initial_fd_limit;
70 unsigned int fork : 1;
71 unsigned int syslog : 1;
72 unsigned int keep_umask : 1;
73 unsigned int allow_anonymous : 1;
74 unsigned int systemd_activation : 1;
75 dbus_bool_t watches_enabled;
78 static dbus_int32_t server_data_slot = -1;
85 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
88 server_get_context (DBusServer *server)
93 if (!dbus_server_allocate_data_slot (&server_data_slot))
96 bd = BUS_SERVER_DATA (server);
99 dbus_server_free_data_slot (&server_data_slot);
103 context = bd->context;
105 dbus_server_free_data_slot (&server_data_slot);
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 _dbus_init_system_log (TRUE);
297 if (flags & BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION)
298 context->systemd_activation = TRUE;
300 context->systemd_activation = FALSE;
302 /* Check for an existing pid file. Of course this is a race;
303 * we'd have to use fcntl() locks on the pid file to
304 * avoid that. But we want to check for the pid file
305 * before overwriting any existing sockets, etc.
308 if (flags & BUS_CONTEXT_FLAG_WRITE_PID_FILE)
309 pidfile = bus_config_parser_get_pidfile (parser);
316 _dbus_string_init_const (&u, pidfile);
318 if (_dbus_stat (&u, &stbuf, NULL))
324 _dbus_string_init (&p);
325 _dbus_file_get_contents(&p, &u, NULL);
326 _dbus_string_parse_int(&p, 0, &pid, NULL);
327 _dbus_string_free(&p);
329 if ((kill((int)pid, 0))) {
330 dbus_set_error(NULL, DBUS_ERROR_FILE_EXISTS,
331 "pid %ld not running, removing stale pid file\n",
333 _dbus_delete_file(&u, NULL);
336 dbus_set_error (error, DBUS_ERROR_FAILED,
337 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
346 /* keep around the pid filename so we can delete it later */
347 context->pidfile = _dbus_strdup (pidfile);
349 /* note that type may be NULL */
350 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
351 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
354 user = bus_config_parser_get_user (parser);
357 context->user = _dbus_strdup (user);
358 if (context->user == NULL)
362 /* Set up the prefix for syslog messages */
363 if (!_dbus_string_init (&log_prefix))
365 if (context->type && !strcmp (context->type, "system"))
367 if (!_dbus_string_append (&log_prefix, "[system] "))
370 else if (context->type && !strcmp (context->type, "session"))
372 DBusCredentials *credentials;
374 credentials = _dbus_credentials_new_from_current_process ();
377 if (!_dbus_string_append (&log_prefix, "[session "))
379 _dbus_credentials_unref (credentials);
382 if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
384 _dbus_credentials_unref (credentials);
387 if (!_dbus_string_append (&log_prefix, "] "))
389 _dbus_credentials_unref (credentials);
392 _dbus_credentials_unref (credentials);
394 if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
396 _dbus_string_free (&log_prefix);
398 /* Build an array of auth mechanisms */
400 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
401 len = _dbus_list_get_length (auth_mechanisms_list);
407 auth_mechanisms = dbus_new0 (char*, len + 1);
408 if (auth_mechanisms == NULL)
412 link = _dbus_list_get_first_link (auth_mechanisms_list);
415 auth_mechanisms[i] = _dbus_strdup (link->data);
416 if (auth_mechanisms[i] == NULL)
418 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
424 auth_mechanisms = NULL;
427 /* Listen on our addresses */
433 server = dbus_server_listen (_dbus_string_get_const_data(address), error);
436 _DBUS_ASSERT_ERROR_IS_SET (error);
439 else if (!setup_server (context, server, auth_mechanisms, error))
441 _DBUS_ASSERT_ERROR_IS_SET (error);
445 if (!_dbus_list_append (&context->servers, server))
450 addresses = bus_config_parser_get_addresses (parser);
452 link = _dbus_list_get_first_link (addresses);
457 server = dbus_server_listen (link->data, error);
460 _DBUS_ASSERT_ERROR_IS_SET (error);
463 else if (!setup_server (context, server, auth_mechanisms, error))
465 _DBUS_ASSERT_ERROR_IS_SET (error);
469 if (!_dbus_list_append (&context->servers, server))
472 link = _dbus_list_get_next_link (addresses, link);
476 context->fork = bus_config_parser_get_fork (parser);
477 context->syslog = bus_config_parser_get_syslog (parser);
478 context->keep_umask = bus_config_parser_get_keep_umask (parser);
479 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
481 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
485 dbus_free_string_array (auth_mechanisms);
490 dbus_free_string_array (auth_mechanisms);
494 /* This code gets executed every time the config files
495 * are parsed: both during BusContext construction
496 * and on reloads. This function is slightly screwy
497 * since it can do a "half reload" in out-of-memory
498 * situations. Realistically, unlikely to ever matter.
501 process_config_every_time (BusContext *context,
502 BusConfigParser *parser,
503 dbus_bool_t is_reload,
506 DBusString full_address;
510 const char *servicehelper;
515 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
520 if (!_dbus_string_init (&full_address))
526 /* get our limits and timeout lengths */
527 bus_config_parser_get_limits (parser, &context->limits);
530 bus_policy_unref (context->policy);
531 context->policy = bus_config_parser_steal_policy (parser);
532 _dbus_assert (context->policy != NULL);
534 /* context->connections is NULL when creating new BusContext */
535 if (context->connections)
537 _dbus_verbose ("Reload policy rules for completed connections\n");
538 retval = bus_connections_reload_policy (context->connections, error);
541 _DBUS_ASSERT_ERROR_IS_SET (error);
546 /* We have to build the address backward, so that
547 * <listen> later in the config file have priority
549 link = _dbus_list_get_last_link (&context->servers);
552 addr = dbus_server_get_address (link->data);
559 if (_dbus_string_get_length (&full_address) > 0)
561 if (!_dbus_string_append (&full_address, ";"))
568 if (!_dbus_string_append (&full_address, addr))
577 link = _dbus_list_get_prev_link (&context->servers, link);
581 dbus_free (context->address);
583 if (!_dbus_string_copy_data (&full_address, &context->address))
589 /* get the service directories */
590 dirs = bus_config_parser_get_service_dirs (parser);
592 /* and the service helper */
593 servicehelper = bus_config_parser_get_servicehelper (parser);
595 s = _dbus_strdup(servicehelper);
596 if (s == NULL && servicehelper != NULL)
603 dbus_free(context->servicehelper);
604 context->servicehelper = s;
607 /* Create activation subsystem */
608 if (context->activation)
610 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
615 context->activation = bus_activation_new (context, &full_address, dirs, error);
618 if (context->activation == NULL)
620 _DBUS_ASSERT_ERROR_IS_SET (error);
624 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
628 _dbus_string_free (&full_address);
637 list_concat_new (DBusList **a,
645 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
647 if (!_dbus_list_append (result, link->data))
650 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
652 if (!_dbus_list_append (result, link->data))
658 _dbus_list_clear (result);
663 raise_file_descriptor_limit (BusContext *context)
666 DBusError error = DBUS_ERROR_INIT;
668 /* we only do this once */
669 if (context->initial_fd_limit != NULL)
672 context->initial_fd_limit = _dbus_rlimit_save_fd_limit (&error);
674 if (context->initial_fd_limit == NULL)
676 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
677 "%s: %s", error.name, error.message);
678 dbus_error_free (&error);
682 /* We used to compute a suitable rlimit based on the configured number
683 * of connections, but that breaks down as soon as we allow fd-passing,
684 * because each connection is allowed to pass 64 fds to us, and if
685 * they all did, we'd hit kernel limits. We now hard-code 64k as a
686 * good limit, like systemd does: that's enough to avoid DoS from
687 * anything short of multiple uids conspiring against us.
689 if (!_dbus_rlimit_raise_fd_limit_if_privileged (65536, &error))
691 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
692 "%s: %s", error.name, error.message);
693 dbus_error_free (&error);
700 process_config_postinit (BusContext *context,
701 BusConfigParser *parser,
704 DBusHashTable *service_context_table;
705 DBusList *watched_dirs = NULL;
707 raise_file_descriptor_limit (context);
709 service_context_table = bus_config_parser_steal_service_context_table (parser);
710 if (!bus_registry_set_service_context_table (context->registry,
711 service_context_table))
717 _dbus_hash_table_unref (service_context_table);
719 /* We need to monitor both the configuration directories and directories
720 * containing .service files.
722 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
723 bus_config_parser_get_service_dirs (parser),
730 bus_set_watched_dirs (context, &watched_dirs);
732 _dbus_list_clear (&watched_dirs);
738 bus_context_new (const DBusString *config_file,
739 BusContextFlags flags,
740 DBusPipe *print_addr_pipe,
741 DBusPipe *print_pid_pipe,
742 const DBusString *address,
746 BusConfigParser *parser;
748 _dbus_assert ((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 ||
749 (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS) == 0);
751 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
756 if (!dbus_server_allocate_data_slot (&server_data_slot))
762 context = dbus_new0 (BusContext, 1);
768 context->refcount = 1;
770 if (!_dbus_generate_uuid (&context->uuid, error))
773 if (!_dbus_string_copy_data (config_file, &context->config_file))
779 context->loop = _dbus_loop_new ();
780 if (context->loop == NULL)
786 context->watches_enabled = TRUE;
788 context->registry = bus_registry_new (context);
789 if (context->registry == NULL)
795 parser = bus_config_load (config_file, TRUE, NULL, error);
798 _DBUS_ASSERT_ERROR_IS_SET (error);
802 if (!process_config_first_time_only (context, parser, address, flags, error))
804 _DBUS_ASSERT_ERROR_IS_SET (error);
807 if (!process_config_every_time (context, parser, FALSE, error))
809 _DBUS_ASSERT_ERROR_IS_SET (error);
813 /* we need another ref of the server data slot for the context
816 if (!dbus_server_allocate_data_slot (&server_data_slot))
817 _dbus_assert_not_reached ("second ref of server data slot failed");
819 /* Note that we don't know whether the print_addr_pipe is
820 * one of the sockets we're using to listen on, or some
821 * other random thing. But I think the answer is "don't do
824 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
827 const char *a = bus_context_get_address (context);
830 _dbus_assert (a != NULL);
831 if (!_dbus_string_init (&addr))
837 if (!_dbus_string_append (&addr, a) ||
838 !_dbus_string_append (&addr, "\n"))
840 _dbus_string_free (&addr);
845 bytes = _dbus_string_get_length (&addr);
846 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
848 /* pipe write returns an error on failure but not short write */
849 if (error != NULL && !dbus_error_is_set (error))
851 dbus_set_error (error, DBUS_ERROR_FAILED,
852 "Printing message bus address: did not write all bytes\n");
854 _dbus_string_free (&addr);
858 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
859 _dbus_pipe_close (print_addr_pipe, NULL);
861 _dbus_string_free (&addr);
864 context->connections = bus_connections_new (context);
865 if (context->connections == NULL)
871 context->matchmaker = bus_matchmaker_new ();
872 if (context->matchmaker == NULL)
878 /* check user before we fork */
879 if (context->user != NULL)
881 if (!_dbus_verify_daemon_user (context->user))
883 dbus_set_error (error, DBUS_ERROR_FAILED,
884 "Could not get UID and GID for username \"%s\"",
890 /* Now become a daemon if appropriate and write out pid file in any case */
894 if (context->pidfile)
895 _dbus_string_init_const (&u, context->pidfile);
897 if (((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 && context->fork) ||
898 (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS))
900 _dbus_verbose ("Forking and becoming daemon\n");
902 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
905 context->keep_umask))
907 _DBUS_ASSERT_ERROR_IS_SET (error);
913 _dbus_verbose ("Fork not requested\n");
915 /* Need to write PID file and to PID pipe for ourselves,
916 * not for the child process. This is a no-op if the pidfile
917 * is NULL and print_pid_pipe is NULL.
919 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
924 _DBUS_ASSERT_ERROR_IS_SET (error);
930 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
931 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
932 _dbus_pipe_close (print_pid_pipe, NULL);
934 /* Here we change our credentials if required,
935 * as soon as we've set up our sockets and pidfile.
936 * This must be done before initializing LSMs, so that the netlink
937 * monitoring thread started by avc_init() will not lose CAP_AUDIT_WRITE
938 * when the main thread calls setuid().
939 * https://bugs.freedesktop.org/show_bug.cgi?id=92832
941 if (context->user != NULL)
943 if (!_dbus_change_to_daemon_user (context->user, error))
945 _DBUS_ASSERT_ERROR_IS_SET (error);
950 /* Auditing should be initialized before LSMs, so that the LSMs are able
951 * to log audit-events that happen during their initialization.
953 bus_audit_init (context);
955 if (!bus_selinux_full_init ())
957 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but D-Bus initialization failed; check system log\n");
960 if (!bus_apparmor_full_init (error))
962 _DBUS_ASSERT_ERROR_IS_SET (error);
966 if (bus_apparmor_enabled ())
968 /* Only print AppArmor mediation message when syslog support is enabled */
970 bus_context_log (context, DBUS_SYSTEM_LOG_INFO,
971 "AppArmor D-Bus mediation is enabled\n");
974 /* When SELinux is used, this must happen after bus_selinux_full_init()
975 * so that it has access to the access vector cache, which is required
976 * to process <associate/> elements.
977 * http://lists.freedesktop.org/archives/dbus/2008-October/010491.html
979 if (!process_config_postinit (context, parser, error))
981 _DBUS_ASSERT_ERROR_IS_SET (error);
987 bus_config_parser_unref (parser);
991 dbus_server_free_data_slot (&server_data_slot);
997 bus_config_parser_unref (parser);
999 bus_context_unref (context);
1001 if (server_data_slot >= 0)
1002 dbus_server_free_data_slot (&server_data_slot);
1008 bus_context_get_id (BusContext *context,
1011 return _dbus_uuid_encode (&context->uuid, uuid);
1015 bus_context_reload_config (BusContext *context,
1018 BusConfigParser *parser;
1019 DBusString config_file;
1022 /* Flush the user database cache */
1023 _dbus_flush_caches ();
1026 _dbus_string_init_const (&config_file, context->config_file);
1027 parser = bus_config_load (&config_file, TRUE, NULL, error);
1030 _DBUS_ASSERT_ERROR_IS_SET (error);
1034 if (!process_config_every_time (context, parser, TRUE, error))
1036 _DBUS_ASSERT_ERROR_IS_SET (error);
1039 if (!process_config_postinit (context, parser, error))
1041 _DBUS_ASSERT_ERROR_IS_SET (error);
1046 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
1049 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
1051 bus_config_parser_unref (parser);
1056 shutdown_server (BusContext *context,
1059 if (server == NULL ||
1060 !dbus_server_get_is_connected (server))
1063 if (!dbus_server_set_watch_functions (server,
1067 _dbus_assert_not_reached ("setting watch functions to NULL failed");
1069 if (!dbus_server_set_timeout_functions (server,
1073 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
1075 dbus_server_disconnect (server);
1079 bus_context_shutdown (BusContext *context)
1083 link = _dbus_list_get_first_link (&context->servers);
1084 while (link != NULL)
1086 shutdown_server (context, link->data);
1088 link = _dbus_list_get_next_link (&context->servers, link);
1093 bus_context_ref (BusContext *context)
1095 _dbus_assert (context->refcount > 0);
1096 context->refcount += 1;
1102 bus_context_unref (BusContext *context)
1104 _dbus_assert (context->refcount > 0);
1105 context->refcount -= 1;
1107 if (context->refcount == 0)
1111 _dbus_verbose ("Finalizing bus context %p\n", context);
1113 bus_context_shutdown (context);
1115 if (context->connections)
1117 bus_connections_unref (context->connections);
1118 context->connections = NULL;
1121 if (context->registry)
1123 bus_registry_unref (context->registry);
1124 context->registry = NULL;
1127 if (context->activation)
1129 bus_activation_unref (context->activation);
1130 context->activation = NULL;
1133 link = _dbus_list_get_first_link (&context->servers);
1134 while (link != NULL)
1136 dbus_server_unref (link->data);
1138 link = _dbus_list_get_next_link (&context->servers, link);
1140 _dbus_list_clear (&context->servers);
1142 if (context->policy)
1144 bus_policy_unref (context->policy);
1145 context->policy = NULL;
1150 _dbus_loop_unref (context->loop);
1151 context->loop = NULL;
1154 if (context->matchmaker)
1156 bus_matchmaker_unref (context->matchmaker);
1157 context->matchmaker = NULL;
1160 dbus_free (context->config_file);
1161 dbus_free (context->log_prefix);
1162 dbus_free (context->type);
1163 dbus_free (context->address);
1164 dbus_free (context->user);
1165 dbus_free (context->servicehelper);
1167 if (context->pidfile)
1170 _dbus_string_init_const (&u, context->pidfile);
1172 /* Deliberately ignore errors here, since there's not much
1173 * we can do about it, and we're exiting anyways.
1175 _dbus_delete_file (&u, NULL);
1177 dbus_free (context->pidfile);
1180 if (context->initial_fd_limit)
1181 _dbus_rlimit_free (context->initial_fd_limit);
1183 dbus_free (context);
1185 dbus_server_free_data_slot (&server_data_slot);
1189 /* type may be NULL */
1191 bus_context_get_type (BusContext *context)
1193 return context->type;
1197 bus_context_get_address (BusContext *context)
1199 return context->address;
1203 bus_context_get_servicehelper (BusContext *context)
1205 return context->servicehelper;
1209 bus_context_get_systemd_activation (BusContext *context)
1211 return context->systemd_activation;
1215 bus_context_get_registry (BusContext *context)
1217 return context->registry;
1221 bus_context_get_connections (BusContext *context)
1223 return context->connections;
1227 bus_context_get_activation (BusContext *context)
1229 return context->activation;
1233 bus_context_get_matchmaker (BusContext *context)
1235 return context->matchmaker;
1239 bus_context_get_loop (BusContext *context)
1241 return context->loop;
1245 bus_context_allow_unix_user (BusContext *context,
1248 return bus_policy_allow_unix_user (context->policy,
1252 /* For now this is never actually called because the default
1253 * DBusConnection behavior of 'same user that owns the bus can connect'
1254 * is all it would do.
1257 bus_context_allow_windows_user (BusContext *context,
1258 const char *windows_sid)
1260 return bus_policy_allow_windows_user (context->policy,
1265 bus_context_get_policy (BusContext *context)
1267 return context->policy;
1271 bus_context_create_client_policy (BusContext *context,
1272 DBusConnection *connection,
1275 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1276 return bus_policy_create_client_policy (context->policy, connection,
1281 bus_context_get_activation_timeout (BusContext *context)
1284 return context->limits.activation_timeout;
1288 bus_context_get_auth_timeout (BusContext *context)
1290 return context->limits.auth_timeout;
1294 bus_context_get_pending_fd_timeout (BusContext *context)
1296 return context->limits.pending_fd_timeout;
1300 bus_context_get_max_completed_connections (BusContext *context)
1302 return context->limits.max_completed_connections;
1306 bus_context_get_max_incomplete_connections (BusContext *context)
1308 return context->limits.max_incomplete_connections;
1312 bus_context_get_max_connections_per_user (BusContext *context)
1314 return context->limits.max_connections_per_user;
1318 bus_context_get_max_pending_activations (BusContext *context)
1320 return context->limits.max_pending_activations;
1324 bus_context_get_max_services_per_connection (BusContext *context)
1326 return context->limits.max_services_per_connection;
1330 bus_context_get_max_match_rules_per_connection (BusContext *context)
1332 return context->limits.max_match_rules_per_connection;
1336 bus_context_get_max_replies_per_connection (BusContext *context)
1338 return context->limits.max_replies_per_connection;
1342 bus_context_get_reply_timeout (BusContext *context)
1344 return context->limits.reply_timeout;
1348 bus_context_get_initial_fd_limit (BusContext *context)
1350 return context->initial_fd_limit;
1354 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1358 if (!context->syslog)
1360 /* we're not syslogging; just output to stderr */
1361 va_start (args, msg);
1362 vfprintf (stderr, msg, args);
1363 fprintf (stderr, "\n");
1366 if (severity == DBUS_SYSTEM_LOG_FATAL)
1372 va_start (args, msg);
1374 if (context->log_prefix)
1376 DBusString full_msg;
1378 if (!_dbus_string_init (&full_msg))
1380 if (!_dbus_string_append (&full_msg, context->log_prefix))
1382 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1385 _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1387 _dbus_string_free (&full_msg);
1390 _dbus_system_logv (severity, msg, args);
1396 static inline const char *
1397 nonnull (const char *maybe_null,
1398 const char *if_null)
1400 return (maybe_null ? maybe_null : if_null);
1404 bus_context_log_literal (BusContext *context,
1405 DBusSystemLogSeverity severity,
1408 if (!context->syslog)
1410 fputs (msg, stderr);
1411 fputc ('\n', stderr);
1413 if (severity == DBUS_SYSTEM_LOG_FATAL)
1418 _dbus_system_log (severity, "%s%s", nonnull (context->log_prefix, ""),
1424 bus_context_log_and_set_error (BusContext *context,
1425 DBusSystemLogSeverity severity,
1431 DBusError stack_error = DBUS_ERROR_INIT;
1434 va_start (args, msg);
1435 _dbus_set_error_valist (&stack_error, name, msg, args);
1438 /* If we hit OOM while setting the error, this will syslog "out of memory"
1439 * which is itself an indication that something is seriously wrong */
1440 bus_context_log_literal (context, DBUS_SYSTEM_LOG_SECURITY,
1441 stack_error.message);
1443 dbus_move_error (&stack_error, error);
1447 * Log something about a message, usually that it was rejected.
1450 complain_about_message (BusContext *context,
1451 const char *error_name,
1452 const char *complaint,
1454 DBusMessage *message,
1455 DBusConnection *sender,
1456 DBusConnection *proposed_recipient,
1457 dbus_bool_t requested_reply,
1461 DBusError stack_error = DBUS_ERROR_INIT;
1462 const char *sender_name;
1463 const char *sender_loginfo;
1464 const char *proposed_recipient_loginfo;
1466 if (error == NULL && !log)
1471 sender_name = bus_connection_get_name (sender);
1472 sender_loginfo = bus_connection_get_loginfo (sender);
1476 sender_name = "(unset)";
1477 sender_loginfo = "(bus)";
1480 if (proposed_recipient != NULL)
1481 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1483 proposed_recipient_loginfo = "bus";
1485 dbus_set_error (&stack_error, error_name,
1486 "%s, %d matched rules; type=\"%s\", sender=\"%s\" (%s) "
1487 "interface=\"%s\" member=\"%s\" error name=\"%s\" "
1488 "requested_reply=\"%d\" destination=\"%s\" (%s)",
1491 dbus_message_type_to_string (dbus_message_get_type (message)),
1494 nonnull (dbus_message_get_interface (message), "(unset)"),
1495 nonnull (dbus_message_get_member (message), "(unset)"),
1496 nonnull (dbus_message_get_error_name (message), "(unset)"),
1498 nonnull (dbus_message_get_destination (message), DBUS_SERVICE_DBUS),
1499 proposed_recipient_loginfo);
1501 /* If we hit OOM while setting the error, this will syslog "out of memory"
1502 * which is itself an indication that something is seriously wrong */
1504 bus_context_log_literal (context, DBUS_SYSTEM_LOG_SECURITY,
1505 stack_error.message);
1507 dbus_move_error (&stack_error, error);
1511 * addressed_recipient is the recipient specified in the message.
1513 * proposed_recipient is the recipient we're considering sending
1514 * to right this second, and may be an eavesdropper.
1516 * sender is the sender of the message.
1518 * NULL for proposed_recipient or sender definitely means the bus driver.
1520 * NULL for addressed_recipient may mean the bus driver, or may mean
1521 * no destination was specified in the message (e.g. a signal).
1524 bus_context_check_security_policy (BusContext *context,
1525 BusTransaction *transaction,
1526 DBusConnection *sender,
1527 DBusConnection *addressed_recipient,
1528 DBusConnection *proposed_recipient,
1529 DBusMessage *message,
1532 const char *src, *dest;
1533 BusClientPolicy *sender_policy;
1534 BusClientPolicy *recipient_policy;
1535 dbus_int32_t toggles;
1538 dbus_bool_t requested_reply;
1540 type = dbus_message_get_type (message);
1541 src = dbus_message_get_sender (message);
1542 dest = dbus_message_get_destination (message);
1544 /* dispatch.c was supposed to ensure these invariants */
1545 _dbus_assert (dest != NULL ||
1546 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1547 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1548 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1549 addressed_recipient != NULL ||
1550 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1554 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1555 case DBUS_MESSAGE_TYPE_SIGNAL:
1556 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1557 case DBUS_MESSAGE_TYPE_ERROR:
1561 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1564 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1565 "Message bus will not accept messages of unknown type\n");
1570 requested_reply = FALSE;
1574 if (bus_connection_is_active (sender))
1576 sender_policy = bus_connection_get_policy (sender);
1577 _dbus_assert (sender_policy != NULL);
1579 /* Fill in requested_reply variable with TRUE if this is a
1580 * reply and the reply was pending.
1582 if (dbus_message_get_reply_serial (message) != 0)
1584 if (proposed_recipient != NULL /* not to the bus driver */ &&
1585 addressed_recipient == proposed_recipient /* not eavesdropping */)
1589 dbus_error_init (&error2);
1590 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1592 sender, addressed_recipient, message,
1594 if (dbus_error_is_set (&error2))
1596 dbus_move_error (&error2, error);
1604 sender_policy = NULL;
1607 /* First verify the SELinux access controls. If allowed then
1608 * go on with the standard checks.
1610 if (!bus_selinux_allows_send (sender, proposed_recipient,
1611 dbus_message_type_to_string (dbus_message_get_type (message)),
1612 dbus_message_get_interface (message),
1613 dbus_message_get_member (message),
1614 dbus_message_get_error_name (message),
1615 dest ? dest : DBUS_SERVICE_DBUS, error))
1617 if (error != NULL && !dbus_error_is_set (error))
1619 /* don't syslog this, just set the error: avc_has_perm should
1620 * have already written to either the audit log or syslog */
1621 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1622 "An SELinux policy prevents this sender from sending this "
1623 "message to this recipient",
1624 0, message, sender, proposed_recipient, FALSE, FALSE, error);
1625 _dbus_verbose ("SELinux security check denying send to service\n");
1631 /* next verify AppArmor access controls. If allowed then
1632 * go on with the standard checks.
1634 if (!bus_apparmor_allows_send (sender, proposed_recipient,
1636 bus_context_get_type (context),
1637 dbus_message_get_type (message),
1638 dbus_message_get_path (message),
1639 dbus_message_get_interface (message),
1640 dbus_message_get_member (message),
1641 dbus_message_get_error_name (message),
1642 dest ? dest : DBUS_SERVICE_DBUS,
1643 src ? src : DBUS_SERVICE_DBUS,
1647 if (!bus_connection_is_active (sender))
1649 /* Policy for inactive connections is that they can only send
1650 * the hello message to the bus driver
1652 if (proposed_recipient == NULL &&
1653 dbus_message_is_method_call (message,
1654 DBUS_INTERFACE_DBUS,
1657 _dbus_verbose ("security check allowing %s message\n",
1663 _dbus_verbose ("security check disallowing non-%s message\n",
1666 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1667 "Client tried to send a message other than %s without being registered",
1676 sender_policy = NULL;
1678 /* If the sender is the bus driver, we assume any reply was a
1679 * requested reply as bus driver won't send bogus ones
1681 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1682 dbus_message_get_reply_serial (message) != 0)
1683 requested_reply = TRUE;
1686 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1687 (sender == NULL && sender_policy == NULL));
1689 if (proposed_recipient != NULL)
1691 /* only the bus driver can send to an inactive recipient (as it
1692 * owns no services, so other apps can't address it). Inactive
1693 * recipients can receive any message.
1695 if (bus_connection_is_active (proposed_recipient))
1697 recipient_policy = bus_connection_get_policy (proposed_recipient);
1698 _dbus_assert (recipient_policy != NULL);
1700 else if (sender == NULL)
1702 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1703 recipient_policy = NULL;
1707 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1708 recipient_policy = NULL;
1712 recipient_policy = NULL;
1714 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1715 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1716 (proposed_recipient == NULL && recipient_policy == NULL));
1719 if (sender_policy &&
1720 !bus_client_policy_check_can_send (sender_policy,
1724 message, &toggles, &log))
1726 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1727 "Rejected send message", toggles,
1728 message, sender, proposed_recipient, requested_reply,
1729 (addressed_recipient == proposed_recipient), error);
1730 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1736 /* We want to drop this message, and are only not doing so for backwards
1738 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1739 "Would reject message", toggles,
1740 message, sender, proposed_recipient, requested_reply,
1744 if (recipient_policy &&
1745 !bus_client_policy_check_can_receive (recipient_policy,
1749 addressed_recipient, proposed_recipient,
1752 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1753 "Rejected receive message", toggles,
1754 message, sender, proposed_recipient, requested_reply,
1755 (addressed_recipient == proposed_recipient), error);
1756 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1760 /* See if limits on size have been exceeded */
1761 if (proposed_recipient &&
1762 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1763 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1765 complain_about_message (context, DBUS_ERROR_LIMITS_EXCEEDED,
1766 "Rejected: destination has a full message queue",
1767 0, message, sender, proposed_recipient, requested_reply, TRUE,
1769 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1773 /* Record that we will allow a reply here in the future (don't
1774 * bother if the recipient is the bus or this is an eavesdropping
1775 * connection). Only the addressed recipient may reply.
1777 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1779 addressed_recipient &&
1780 addressed_recipient == proposed_recipient && /* not eavesdropping */
1781 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1783 sender, addressed_recipient,
1786 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1790 _dbus_verbose ("security policy allowing message\n");
1795 bus_context_check_all_watches (BusContext *context)
1798 dbus_bool_t enabled = TRUE;
1800 if (bus_connections_get_n_incomplete (context->connections) >=
1801 bus_context_get_max_incomplete_connections (context))
1806 if (context->watches_enabled == enabled)
1809 context->watches_enabled = enabled;
1811 for (link = _dbus_list_get_first_link (&context->servers);
1813 link = _dbus_list_get_next_link (&context->servers, link))
1815 /* A BusContext might contains several DBusServer (if there are
1816 * several <listen> configuration items) and a DBusServer might
1817 * contain several DBusWatch in its DBusWatchList (if getaddrinfo
1818 * returns several addresses on a dual IPv4-IPv6 stack or if
1819 * systemd passes several fds).
1820 * We want to enable/disable them all.
1822 DBusServer *server = link->data;
1823 _dbus_server_toggle_all_watches (server, enabled);