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
30 #include "activation.h"
31 #include "connection.h"
35 #include "config-parser.h"
40 #include "dir-watch.h"
41 #include <dbus/dbus-auth.h>
42 #include <dbus/dbus-list.h>
43 #include <dbus/dbus-hash.h>
44 #include <dbus/dbus-credentials.h>
45 #include <dbus/dbus-internals.h>
46 #include <dbus/dbus-server-protected.h>
65 BusConnections *connections;
66 BusActivation *activation;
67 BusRegistry *registry;
69 BusMatchmaker *matchmaker;
71 DBusRLimit *initial_fd_limit;
72 unsigned int fork : 1;
73 unsigned int syslog : 1;
74 unsigned int keep_umask : 1;
75 unsigned int allow_anonymous : 1;
76 unsigned int systemd_activation : 1;
77 dbus_bool_t watches_enabled;
80 static dbus_int32_t server_data_slot = -1;
87 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
90 server_get_context (DBusServer *server)
95 /* this data slot was allocated by the BusContext */
96 _dbus_assert (server_data_slot >= 0);
98 bd = BUS_SERVER_DATA (server);
100 /* every DBusServer in the dbus-daemon has gone through setup_server() */
101 _dbus_assert (bd != NULL);
103 context = bd->context;
109 add_server_watch (DBusWatch *watch,
112 DBusServer *server = data;
115 context = server_get_context (server);
117 return _dbus_loop_add_watch (context->loop, watch);
121 remove_server_watch (DBusWatch *watch,
124 DBusServer *server = data;
127 context = server_get_context (server);
129 _dbus_loop_remove_watch (context->loop, watch);
133 toggle_server_watch (DBusWatch *watch,
136 DBusServer *server = data;
139 context = server_get_context (server);
141 _dbus_loop_toggle_watch (context->loop, watch);
145 add_server_timeout (DBusTimeout *timeout,
148 DBusServer *server = data;
151 context = server_get_context (server);
153 return _dbus_loop_add_timeout (context->loop, timeout);
157 remove_server_timeout (DBusTimeout *timeout,
160 DBusServer *server = data;
163 context = server_get_context (server);
165 _dbus_loop_remove_timeout (context->loop, timeout);
169 new_connection_callback (DBusServer *server,
170 DBusConnection *new_connection,
173 BusContext *context = data;
175 /* If this fails it logs a warning, so we don't need to do that */
176 if (!bus_connections_setup_connection (context->connections, new_connection))
178 /* if we don't do this, it will get unref'd without
179 * being disconnected... kind of strange really
180 * that we have to do this, people won't get it right
183 dbus_connection_close (new_connection);
186 dbus_connection_set_max_received_size (new_connection,
187 context->limits.max_incoming_bytes);
189 dbus_connection_set_max_message_size (new_connection,
190 context->limits.max_message_size);
192 dbus_connection_set_max_received_unix_fds (new_connection,
193 context->limits.max_incoming_unix_fds);
195 dbus_connection_set_max_message_unix_fds (new_connection,
196 context->limits.max_message_unix_fds);
198 dbus_connection_set_allow_anonymous (new_connection,
199 context->allow_anonymous);
201 /* on OOM, we won't have ref'd the connection so it will die. */
205 free_server_data (void *data)
207 BusServerData *bd = data;
213 setup_server (BusContext *context,
215 char **auth_mechanisms,
220 bd = dbus_new0 (BusServerData, 1);
221 if (bd == NULL || !dbus_server_set_data (server,
223 bd, free_server_data))
230 bd->context = context;
232 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
238 dbus_server_set_new_connection_function (server,
239 new_connection_callback,
242 if (!dbus_server_set_watch_functions (server,
253 if (!dbus_server_set_timeout_functions (server,
255 remove_server_timeout,
266 /* This code only gets executed the first time the
267 * config files are parsed. It is not executed
268 * when config files are reloaded.
271 process_config_first_time_only (BusContext *context,
272 BusConfigParser *parser,
273 const DBusString *address,
274 BusContextFlags flags,
277 DBusString log_prefix;
279 DBusList **addresses;
280 const char *user, *pidfile;
281 char **auth_mechanisms;
282 DBusList **auth_mechanisms_list;
285 DBusLogFlags log_flags = DBUS_LOG_FLAGS_STDERR;
287 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
290 auth_mechanisms = NULL;
293 if (flags & BUS_CONTEXT_FLAG_SYSLOG_ALWAYS)
295 context->syslog = TRUE;
296 log_flags |= DBUS_LOG_FLAGS_SYSTEM_LOG;
298 if (flags & BUS_CONTEXT_FLAG_SYSLOG_ONLY)
299 log_flags &= ~DBUS_LOG_FLAGS_STDERR;
301 else if (flags & BUS_CONTEXT_FLAG_SYSLOG_NEVER)
303 context->syslog = FALSE;
307 context->syslog = bus_config_parser_get_syslog (parser);
310 log_flags |= DBUS_LOG_FLAGS_SYSTEM_LOG;
313 _dbus_init_system_log ("dbus-daemon", log_flags);
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);
434 _dbus_string_init_const (&name, link->data);
435 if (!_dbus_auth_is_supported_mechanism (&name))
438 if (!_dbus_string_init (&list))
441 if (!_dbus_auth_dump_supported_mechanisms (&list))
443 _dbus_string_free (&list);
446 dbus_set_error (error, DBUS_ERROR_FAILED,
447 "Unsupported auth mechanism \"%s\" in bus config file detected. Supported mechanisms are \"%s\".",
449 _dbus_string_get_const_data (&list));
450 _dbus_string_free (&list);
453 auth_mechanisms[i] = _dbus_strdup (link->data);
454 if (auth_mechanisms[i] == NULL)
456 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
462 auth_mechanisms = NULL;
465 /* Listen on our addresses */
471 server = dbus_server_listen (_dbus_string_get_const_data(address), error);
474 _DBUS_ASSERT_ERROR_IS_SET (error);
477 else if (!setup_server (context, server, auth_mechanisms, error))
479 _DBUS_ASSERT_ERROR_IS_SET (error);
483 if (!_dbus_list_append (&context->servers, server))
488 addresses = bus_config_parser_get_addresses (parser);
490 link = _dbus_list_get_first_link (addresses);
495 server = dbus_server_listen (link->data, error);
498 _DBUS_ASSERT_ERROR_IS_SET (error);
501 else if (!setup_server (context, server, auth_mechanisms, error))
503 _DBUS_ASSERT_ERROR_IS_SET (error);
507 if (!_dbus_list_append (&context->servers, server))
510 link = _dbus_list_get_next_link (addresses, link);
514 context->fork = bus_config_parser_get_fork (parser);
515 context->keep_umask = bus_config_parser_get_keep_umask (parser);
516 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
518 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
522 dbus_free_string_array (auth_mechanisms);
527 dbus_free_string_array (auth_mechanisms);
531 /* This code gets executed every time the config files
532 * are parsed: both during BusContext construction
533 * and on reloads. This function is slightly screwy
534 * since it can do a "half reload" in out-of-memory
535 * situations. Realistically, unlikely to ever matter.
538 process_config_every_time (BusContext *context,
539 BusConfigParser *parser,
540 dbus_bool_t is_reload,
543 DBusString full_address;
547 const char *servicehelper;
552 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
557 if (!_dbus_string_init (&full_address))
563 /* get our limits and timeout lengths */
564 bus_config_parser_get_limits (parser, &context->limits);
567 bus_policy_unref (context->policy);
568 context->policy = bus_config_parser_steal_policy (parser);
569 _dbus_assert (context->policy != NULL);
571 /* context->connections is NULL when creating new BusContext */
572 if (context->connections)
574 _dbus_verbose ("Reload policy rules for completed connections\n");
575 retval = bus_connections_reload_policy (context->connections, error);
578 _DBUS_ASSERT_ERROR_IS_SET (error);
583 /* We have to build the address backward, so that
584 * <listen> later in the config file have priority
586 link = _dbus_list_get_last_link (&context->servers);
589 addr = dbus_server_get_address (link->data);
596 if (_dbus_string_get_length (&full_address) > 0)
598 if (!_dbus_string_append (&full_address, ";"))
605 if (!_dbus_string_append (&full_address, addr))
614 link = _dbus_list_get_prev_link (&context->servers, link);
618 dbus_free (context->address);
620 if (!_dbus_string_copy_data (&full_address, &context->address))
626 /* get the service directories */
627 dirs = bus_config_parser_get_service_dirs (parser);
629 /* and the service helper */
630 servicehelper = bus_config_parser_get_servicehelper (parser);
632 s = _dbus_strdup(servicehelper);
633 if (s == NULL && servicehelper != NULL)
640 dbus_free(context->servicehelper);
641 context->servicehelper = s;
644 /* Create activation subsystem */
645 if (context->activation)
647 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
652 context->activation = bus_activation_new (context, &full_address, dirs, error);
655 if (context->activation == NULL)
657 _DBUS_ASSERT_ERROR_IS_SET (error);
661 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
665 _dbus_string_free (&full_address);
674 raise_file_descriptor_limit (BusContext *context)
677 DBusError error = DBUS_ERROR_INIT;
679 /* we only do this once */
680 if (context->initial_fd_limit != NULL)
683 context->initial_fd_limit = _dbus_rlimit_save_fd_limit (&error);
685 if (context->initial_fd_limit == NULL)
687 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
688 "%s: %s", error.name, error.message);
689 dbus_error_free (&error);
693 /* We used to compute a suitable rlimit based on the configured number
694 * of connections, but that breaks down as soon as we allow fd-passing,
695 * because each connection is allowed to pass 64 fds to us, and if
696 * they all did, we'd hit kernel limits. We now hard-code 64k as a
697 * good limit, like systemd does: that's enough to avoid DoS from
698 * anything short of multiple uids conspiring against us.
700 if (!_dbus_rlimit_raise_fd_limit_if_privileged (65536, &error))
702 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
703 "%s: %s", error.name, error.message);
704 dbus_error_free (&error);
711 process_config_postinit (BusContext *context,
712 BusConfigParser *parser,
715 DBusHashTable *service_context_table;
716 DBusList *watched_dirs = NULL;
718 raise_file_descriptor_limit (context);
720 service_context_table = bus_config_parser_steal_service_context_table (parser);
721 if (!bus_registry_set_service_context_table (context->registry,
722 service_context_table))
728 _dbus_hash_table_unref (service_context_table);
730 /* We need to monitor both the configuration directories and directories
731 * containing .service files.
733 if (!bus_config_parser_get_watched_dirs (parser, &watched_dirs))
739 bus_set_watched_dirs (context, &watched_dirs);
741 _dbus_list_clear (&watched_dirs);
747 bus_context_new (const DBusString *config_file,
748 BusContextFlags flags,
749 DBusPipe *print_addr_pipe,
750 DBusPipe *print_pid_pipe,
751 const DBusString *address,
755 BusConfigParser *parser;
757 _dbus_assert ((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 ||
758 (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS) == 0);
760 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
765 if (!dbus_server_allocate_data_slot (&server_data_slot))
771 context = dbus_new0 (BusContext, 1);
777 context->refcount = 1;
779 if (!_dbus_generate_uuid (&context->uuid, error))
782 if (!_dbus_string_copy_data (config_file, &context->config_file))
788 context->loop = _dbus_loop_new ();
789 if (context->loop == NULL)
795 context->watches_enabled = TRUE;
797 context->registry = bus_registry_new (context);
798 if (context->registry == NULL)
804 parser = bus_config_load (config_file, TRUE, NULL, error);
807 _DBUS_ASSERT_ERROR_IS_SET (error);
811 if (!process_config_first_time_only (context, parser, address, flags, error))
813 _DBUS_ASSERT_ERROR_IS_SET (error);
816 if (!process_config_every_time (context, parser, FALSE, error))
818 _DBUS_ASSERT_ERROR_IS_SET (error);
822 /* we need another ref of the server data slot for the context
825 if (!dbus_server_allocate_data_slot (&server_data_slot))
826 _dbus_assert_not_reached ("second ref of server data slot failed");
828 /* Note that we don't know whether the print_addr_pipe is
829 * one of the sockets we're using to listen on, or some
830 * other random thing. But I think the answer is "don't do
833 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
836 const char *a = bus_context_get_address (context);
839 _dbus_assert (a != NULL);
840 if (!_dbus_string_init (&addr))
846 if (!_dbus_string_append (&addr, a) ||
847 !_dbus_string_append (&addr, "\n"))
849 _dbus_string_free (&addr);
854 bytes = _dbus_string_get_length (&addr);
855 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
857 /* pipe write returns an error on failure but not short write */
858 if (error != NULL && !dbus_error_is_set (error))
860 dbus_set_error (error, DBUS_ERROR_FAILED,
861 "Printing message bus address: did not write all bytes\n");
863 _dbus_string_free (&addr);
867 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
868 _dbus_pipe_close (print_addr_pipe, NULL);
870 _dbus_string_free (&addr);
873 context->connections = bus_connections_new (context);
874 if (context->connections == NULL)
880 context->matchmaker = bus_matchmaker_new ();
881 if (context->matchmaker == NULL)
887 /* check user before we fork */
888 if (context->user != NULL)
890 if (!_dbus_verify_daemon_user (context->user))
892 dbus_set_error (error, DBUS_ERROR_FAILED,
893 "Could not get UID and GID for username \"%s\"",
899 /* Now become a daemon if appropriate and write out pid file in any case */
903 if (context->pidfile)
904 _dbus_string_init_const (&u, context->pidfile);
906 if (((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 && context->fork) ||
907 (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS))
909 _dbus_verbose ("Forking and becoming daemon\n");
911 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
914 context->keep_umask))
916 _DBUS_ASSERT_ERROR_IS_SET (error);
922 _dbus_verbose ("Fork not requested\n");
924 /* Need to write PID file and to PID pipe for ourselves,
925 * not for the child process. This is a no-op if the pidfile
926 * is NULL and print_pid_pipe is NULL.
928 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
933 _DBUS_ASSERT_ERROR_IS_SET (error);
939 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
940 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
941 _dbus_pipe_close (print_pid_pipe, NULL);
943 /* Here we change our credentials if required,
944 * as soon as we've set up our sockets and pidfile.
945 * This must be done before initializing LSMs, so that the netlink
946 * monitoring thread started by avc_init() will not lose CAP_AUDIT_WRITE
947 * when the main thread calls setuid().
948 * https://bugs.freedesktop.org/show_bug.cgi?id=92832
950 if (context->user != NULL)
952 if (!_dbus_change_to_daemon_user (context->user, error))
954 _DBUS_ASSERT_ERROR_IS_SET (error);
959 /* Auditing should be initialized before LSMs, so that the LSMs are able
960 * to log audit-events that happen during their initialization.
962 bus_audit_init (context);
964 if (!bus_selinux_full_init ())
966 bus_context_log (context, DBUS_SYSTEM_LOG_ERROR,
967 "SELinux enabled but D-Bus initialization failed; "
972 if (!bus_apparmor_full_init (error))
974 _DBUS_ASSERT_ERROR_IS_SET (error);
978 if (bus_apparmor_enabled ())
980 /* Only print AppArmor mediation message when syslog support is enabled */
982 bus_context_log (context, DBUS_SYSTEM_LOG_INFO,
983 "AppArmor D-Bus mediation is enabled\n");
986 /* When SELinux is used, this must happen after bus_selinux_full_init()
987 * so that it has access to the access vector cache, which is required
988 * to process <associate/> elements.
989 * http://lists.freedesktop.org/archives/dbus/2008-October/010491.html
991 if (!process_config_postinit (context, parser, error))
993 _DBUS_ASSERT_ERROR_IS_SET (error);
999 bus_config_parser_unref (parser);
1003 dbus_server_free_data_slot (&server_data_slot);
1009 bus_config_parser_unref (parser);
1010 if (context != NULL)
1011 bus_context_unref (context);
1013 if (server_data_slot >= 0)
1014 dbus_server_free_data_slot (&server_data_slot);
1020 bus_context_get_id (BusContext *context,
1023 return _dbus_uuid_encode (&context->uuid, uuid);
1027 bus_context_reload_config (BusContext *context,
1030 BusConfigParser *parser;
1031 DBusString config_file;
1034 /* Flush the user database cache */
1035 _dbus_flush_caches ();
1038 _dbus_string_init_const (&config_file, context->config_file);
1039 parser = bus_config_load (&config_file, TRUE, NULL, error);
1042 _DBUS_ASSERT_ERROR_IS_SET (error);
1046 if (!process_config_every_time (context, parser, TRUE, error))
1048 _DBUS_ASSERT_ERROR_IS_SET (error);
1051 if (!process_config_postinit (context, parser, error))
1053 _DBUS_ASSERT_ERROR_IS_SET (error);
1058 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
1061 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
1063 bus_config_parser_unref (parser);
1068 shutdown_server (BusContext *context,
1071 if (server == NULL ||
1072 !dbus_server_get_is_connected (server))
1075 if (!dbus_server_set_watch_functions (server,
1079 _dbus_assert_not_reached ("setting watch functions to NULL failed");
1081 if (!dbus_server_set_timeout_functions (server,
1085 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
1087 dbus_server_disconnect (server);
1091 bus_context_shutdown (BusContext *context)
1095 link = _dbus_list_get_first_link (&context->servers);
1096 while (link != NULL)
1098 shutdown_server (context, link->data);
1100 link = _dbus_list_get_next_link (&context->servers, link);
1105 bus_context_ref (BusContext *context)
1107 _dbus_assert (context->refcount > 0);
1108 context->refcount += 1;
1114 bus_context_unref (BusContext *context)
1116 _dbus_assert (context->refcount > 0);
1117 context->refcount -= 1;
1119 if (context->refcount == 0)
1123 _dbus_verbose ("Finalizing bus context %p\n", context);
1125 bus_context_shutdown (context);
1127 if (context->connections)
1129 bus_connections_unref (context->connections);
1130 context->connections = NULL;
1133 if (context->registry)
1135 bus_registry_unref (context->registry);
1136 context->registry = NULL;
1139 if (context->activation)
1141 bus_activation_unref (context->activation);
1142 context->activation = NULL;
1145 link = _dbus_list_get_first_link (&context->servers);
1146 while (link != NULL)
1148 dbus_server_unref (link->data);
1150 link = _dbus_list_get_next_link (&context->servers, link);
1152 _dbus_list_clear (&context->servers);
1154 if (context->policy)
1156 bus_policy_unref (context->policy);
1157 context->policy = NULL;
1162 _dbus_loop_unref (context->loop);
1163 context->loop = NULL;
1166 if (context->matchmaker)
1168 bus_matchmaker_unref (context->matchmaker);
1169 context->matchmaker = NULL;
1172 dbus_free (context->config_file);
1173 dbus_free (context->log_prefix);
1174 dbus_free (context->type);
1175 dbus_free (context->address);
1176 dbus_free (context->user);
1177 dbus_free (context->servicehelper);
1179 if (context->pidfile)
1182 _dbus_string_init_const (&u, context->pidfile);
1184 /* Deliberately ignore errors here, since there's not much
1185 * we can do about it, and we're exiting anyways.
1187 _dbus_delete_file (&u, NULL);
1189 dbus_free (context->pidfile);
1192 if (context->initial_fd_limit)
1193 _dbus_rlimit_free (context->initial_fd_limit);
1195 dbus_free (context);
1197 dbus_server_free_data_slot (&server_data_slot);
1201 /* type may be NULL */
1203 bus_context_get_type (BusContext *context)
1205 return context->type;
1209 bus_context_get_address (BusContext *context)
1211 return context->address;
1215 bus_context_get_servicehelper (BusContext *context)
1217 return context->servicehelper;
1221 bus_context_get_systemd_activation (BusContext *context)
1223 return context->systemd_activation;
1227 bus_context_get_registry (BusContext *context)
1229 return context->registry;
1233 bus_context_get_connections (BusContext *context)
1235 return context->connections;
1239 bus_context_get_activation (BusContext *context)
1241 return context->activation;
1245 bus_context_get_matchmaker (BusContext *context)
1247 return context->matchmaker;
1251 bus_context_get_loop (BusContext *context)
1253 return context->loop;
1257 bus_context_allow_unix_user (BusContext *context,
1260 return bus_policy_allow_unix_user (context->policy,
1264 /* For now this is never actually called because the default
1265 * DBusConnection behavior of 'same user that owns the bus can connect'
1266 * is all it would do.
1269 bus_context_allow_windows_user (BusContext *context,
1270 const char *windows_sid)
1272 return bus_policy_allow_windows_user (context->policy,
1277 bus_context_get_policy (BusContext *context)
1279 return context->policy;
1283 bus_context_create_client_policy (BusContext *context,
1284 DBusConnection *connection,
1287 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1288 return bus_policy_create_client_policy (context->policy, connection,
1293 bus_context_get_activation_timeout (BusContext *context)
1296 return context->limits.activation_timeout;
1300 bus_context_get_auth_timeout (BusContext *context)
1302 return context->limits.auth_timeout;
1306 bus_context_get_pending_fd_timeout (BusContext *context)
1308 return context->limits.pending_fd_timeout;
1312 bus_context_get_max_completed_connections (BusContext *context)
1314 return context->limits.max_completed_connections;
1318 bus_context_get_max_incomplete_connections (BusContext *context)
1320 return context->limits.max_incomplete_connections;
1324 bus_context_get_max_connections_per_user (BusContext *context)
1326 return context->limits.max_connections_per_user;
1330 bus_context_get_max_pending_activations (BusContext *context)
1332 return context->limits.max_pending_activations;
1336 bus_context_get_max_services_per_connection (BusContext *context)
1338 return context->limits.max_services_per_connection;
1342 bus_context_get_max_match_rules_per_connection (BusContext *context)
1344 return context->limits.max_match_rules_per_connection;
1348 bus_context_get_max_replies_per_connection (BusContext *context)
1350 return context->limits.max_replies_per_connection;
1354 bus_context_get_reply_timeout (BusContext *context)
1356 return context->limits.reply_timeout;
1360 bus_context_get_initial_fd_limit (BusContext *context)
1362 return context->initial_fd_limit;
1366 bus_context_get_using_syslog (BusContext *context)
1368 return context->syslog;
1372 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1376 va_start (args, msg);
1378 if (context->log_prefix)
1380 DBusString full_msg;
1382 if (!_dbus_string_init (&full_msg))
1384 if (!_dbus_string_append (&full_msg, context->log_prefix))
1386 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1389 _dbus_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1391 _dbus_string_free (&full_msg);
1394 _dbus_logv (severity, msg, args);
1400 static inline const char *
1401 nonnull (const char *maybe_null,
1402 const char *if_null)
1404 return (maybe_null ? maybe_null : if_null);
1408 bus_context_log_literal (BusContext *context,
1409 DBusSystemLogSeverity severity,
1412 _dbus_log (severity, "%s%s", nonnull (context->log_prefix, ""), msg);
1416 bus_context_log_and_set_error (BusContext *context,
1417 DBusSystemLogSeverity severity,
1423 DBusError stack_error = DBUS_ERROR_INIT;
1426 va_start (args, msg);
1427 _dbus_set_error_valist (&stack_error, name, msg, args);
1430 /* If we hit OOM while setting the error, this will syslog "out of memory"
1431 * which is itself an indication that something is seriously wrong */
1432 bus_context_log_literal (context, DBUS_SYSTEM_LOG_SECURITY,
1433 stack_error.message);
1435 dbus_move_error (&stack_error, error);
1439 * Log something about a message, usually that it was rejected.
1442 complain_about_message (BusContext *context,
1443 const char *error_name,
1444 const char *complaint,
1446 DBusMessage *message,
1447 DBusConnection *sender,
1448 DBusConnection *proposed_recipient,
1449 dbus_bool_t requested_reply,
1453 DBusError stack_error = DBUS_ERROR_INIT;
1454 const char *sender_name;
1455 const char *sender_loginfo;
1456 const char *proposed_recipient_loginfo;
1458 if (error == NULL && !log)
1463 sender_name = bus_connection_get_name (sender);
1464 sender_loginfo = bus_connection_get_loginfo (sender);
1468 sender_name = "(unset)";
1469 sender_loginfo = "(bus)";
1472 if (proposed_recipient != NULL)
1473 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1475 proposed_recipient_loginfo = "bus";
1477 dbus_set_error (&stack_error, error_name,
1478 "%s, %d matched rules; type=\"%s\", sender=\"%s\" (%s) "
1479 "interface=\"%s\" member=\"%s\" error name=\"%s\" "
1480 "requested_reply=\"%d\" destination=\"%s\" (%s)",
1483 dbus_message_type_to_string (dbus_message_get_type (message)),
1486 nonnull (dbus_message_get_interface (message), "(unset)"),
1487 nonnull (dbus_message_get_member (message), "(unset)"),
1488 nonnull (dbus_message_get_error_name (message), "(unset)"),
1490 nonnull (dbus_message_get_destination (message), DBUS_SERVICE_DBUS),
1491 proposed_recipient_loginfo);
1493 /* If we hit OOM while setting the error, this will syslog "out of memory"
1494 * which is itself an indication that something is seriously wrong */
1496 bus_context_log_literal (context, DBUS_SYSTEM_LOG_SECURITY,
1497 stack_error.message);
1499 dbus_move_error (&stack_error, error);
1503 * addressed_recipient is the recipient specified in the message.
1505 * proposed_recipient is the recipient we're considering sending
1506 * to right this second, and may be an eavesdropper.
1508 * sender is the sender of the message.
1510 * NULL for sender definitely means the bus driver.
1512 * NULL for proposed_recipient may mean the bus driver, or may mean
1513 * we are checking whether service-activation is allowed as a first
1514 * pass before all details of the activated service are known.
1516 * NULL for addressed_recipient may mean the bus driver, or may mean
1517 * no destination was specified in the message (e.g. a signal).
1520 bus_context_check_security_policy (BusContext *context,
1521 BusTransaction *transaction,
1522 DBusConnection *sender,
1523 DBusConnection *addressed_recipient,
1524 DBusConnection *proposed_recipient,
1525 DBusMessage *message,
1526 BusActivationEntry *activation_entry,
1529 const char *src, *dest;
1530 BusClientPolicy *sender_policy;
1531 BusClientPolicy *recipient_policy;
1532 dbus_int32_t toggles;
1535 dbus_bool_t requested_reply;
1537 type = dbus_message_get_type (message);
1538 src = dbus_message_get_sender (message);
1539 dest = dbus_message_get_destination (message);
1541 /* dispatch.c was supposed to ensure these invariants */
1542 _dbus_assert (dest != NULL ||
1543 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1544 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1545 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1546 addressed_recipient != NULL ||
1547 activation_entry != NULL ||
1548 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1552 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1553 case DBUS_MESSAGE_TYPE_SIGNAL:
1554 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1555 case DBUS_MESSAGE_TYPE_ERROR:
1559 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1562 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1563 "Message bus will not accept messages of unknown type\n");
1568 requested_reply = FALSE;
1572 if (bus_connection_is_active (sender))
1574 sender_policy = bus_connection_get_policy (sender);
1575 _dbus_assert (sender_policy != NULL);
1577 /* Fill in requested_reply variable with TRUE if this is a
1578 * reply and the reply was pending.
1580 if (dbus_message_get_reply_serial (message) != 0)
1582 if (proposed_recipient != NULL /* not to the bus driver */ &&
1583 addressed_recipient == proposed_recipient /* not eavesdropping */)
1587 dbus_error_init (&error2);
1588 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1590 sender, addressed_recipient, message,
1592 if (dbus_error_is_set (&error2))
1594 dbus_move_error (&error2, error);
1602 sender_policy = NULL;
1605 /* First verify the SELinux access controls. If allowed then
1606 * go on with the standard checks.
1608 if (!bus_selinux_allows_send (sender, proposed_recipient,
1609 dbus_message_type_to_string (dbus_message_get_type (message)),
1610 dbus_message_get_interface (message),
1611 dbus_message_get_member (message),
1612 dbus_message_get_error_name (message),
1613 dest ? dest : DBUS_SERVICE_DBUS,
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,
1648 if (!bus_connection_is_active (sender))
1650 /* Policy for inactive connections is that they can only send
1651 * the hello message to the bus driver
1653 if (proposed_recipient == NULL &&
1654 dbus_message_is_method_call (message,
1655 DBUS_INTERFACE_DBUS,
1658 _dbus_verbose ("security check allowing %s message\n",
1664 _dbus_verbose ("security check disallowing non-%s message\n",
1667 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1668 "Client tried to send a message other than %s without being registered",
1677 sender_policy = NULL;
1679 /* If the sender is the bus driver, we assume any reply was a
1680 * requested reply as bus driver won't send bogus ones
1682 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1683 dbus_message_get_reply_serial (message) != 0)
1684 requested_reply = TRUE;
1687 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1688 (sender == NULL && sender_policy == NULL));
1690 if (proposed_recipient != NULL)
1692 /* only the bus driver can send to an inactive recipient (as it
1693 * owns no services, so other apps can't address it). Inactive
1694 * recipients can receive any message.
1696 if (bus_connection_is_active (proposed_recipient))
1698 recipient_policy = bus_connection_get_policy (proposed_recipient);
1699 _dbus_assert (recipient_policy != NULL);
1701 else if (sender == NULL)
1703 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1704 recipient_policy = NULL;
1708 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus");
1709 recipient_policy = NULL;
1713 recipient_policy = NULL;
1715 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1716 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1717 (proposed_recipient == NULL && recipient_policy == NULL));
1720 if (sender_policy &&
1721 !bus_client_policy_check_can_send (sender_policy,
1725 message, &toggles, &log))
1727 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1728 "Rejected send message", toggles,
1729 message, sender, proposed_recipient, requested_reply,
1730 (addressed_recipient == proposed_recipient), error);
1731 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1737 /* We want to drop this message, and are only not doing so for backwards
1739 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1740 "Would reject message", toggles,
1741 message, sender, proposed_recipient, requested_reply,
1745 if (recipient_policy &&
1746 !bus_client_policy_check_can_receive (recipient_policy,
1750 addressed_recipient, proposed_recipient,
1753 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1754 "Rejected receive message", toggles,
1755 message, sender, proposed_recipient, requested_reply,
1756 (addressed_recipient == proposed_recipient), error);
1757 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1761 /* See if limits on size have been exceeded */
1762 if (proposed_recipient &&
1763 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1764 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1766 complain_about_message (context, DBUS_ERROR_LIMITS_EXCEEDED,
1767 "Rejected: destination has a full message queue",
1768 0, message, sender, proposed_recipient, requested_reply, TRUE,
1770 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1774 /* Record that we will allow a reply here in the future (don't
1775 * bother if the recipient is the bus or this is an eavesdropping
1776 * connection). Only the addressed recipient may reply.
1778 * This isn't done for activation attempts because they have no addressed
1779 * or proposed recipient; when we check whether to actually deliver the
1780 * message, later, we'll record the reply expectation at that point.
1782 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1784 addressed_recipient &&
1785 addressed_recipient == proposed_recipient && /* not eavesdropping */
1786 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1788 sender, addressed_recipient,
1791 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1795 _dbus_verbose ("security policy allowing message\n");
1800 bus_context_check_all_watches (BusContext *context)
1803 dbus_bool_t enabled = TRUE;
1805 if (bus_connections_get_n_incomplete (context->connections) >=
1806 bus_context_get_max_incomplete_connections (context))
1811 if (context->watches_enabled == enabled)
1814 context->watches_enabled = enabled;
1816 for (link = _dbus_list_get_first_link (&context->servers);
1818 link = _dbus_list_get_next_link (&context->servers, link))
1820 /* A BusContext might contains several DBusServer (if there are
1821 * several <listen> configuration items) and a DBusServer might
1822 * contain several DBusWatch in its DBusWatchList (if getaddrinfo
1823 * returns several addresses on a dual IPv4-IPv6 stack or if
1824 * systemd passes several fds).
1825 * We want to enable/disable them all.
1827 DBusServer *server = link->data;
1828 _dbus_server_toggle_all_watches (server, enabled);