1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* bus.c message bus context object
4 * Copyright (C) 2003, 2004 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "activation.h"
30 #include "connection.h"
34 #include "config-parser.h"
37 #include "dir-watch.h"
38 #include <dbus/dbus-list.h>
39 #include <dbus/dbus-hash.h>
40 #include <dbus/dbus-credentials.h>
41 #include <dbus/dbus-internals.h>
42 #include <dbus/dbus-server-protected.h>
61 BusConnections *connections;
62 BusActivation *activation;
63 BusRegistry *registry;
65 BusMatchmaker *matchmaker;
67 DBusRLimit *initial_fd_limit;
68 unsigned int fork : 1;
69 unsigned int syslog : 1;
70 unsigned int keep_umask : 1;
71 unsigned int allow_anonymous : 1;
72 unsigned int systemd_activation : 1;
73 dbus_bool_t watches_enabled;
76 static dbus_int32_t server_data_slot = -1;
83 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
86 server_get_context (DBusServer *server)
91 if (!dbus_server_allocate_data_slot (&server_data_slot))
94 bd = BUS_SERVER_DATA (server);
97 dbus_server_free_data_slot (&server_data_slot);
101 context = bd->context;
103 dbus_server_free_data_slot (&server_data_slot);
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 (!bus_connections_setup_connection (context->connections, new_connection))
177 _dbus_verbose ("No memory to setup new connection\n");
179 /* if we don't do this, it will get unref'd without
180 * being disconnected... kind of strange really
181 * that we have to do this, people won't get it right
184 dbus_connection_close (new_connection);
187 dbus_connection_set_max_received_size (new_connection,
188 context->limits.max_incoming_bytes);
190 dbus_connection_set_max_message_size (new_connection,
191 context->limits.max_message_size);
193 dbus_connection_set_max_received_unix_fds (new_connection,
194 context->limits.max_incoming_unix_fds);
196 dbus_connection_set_max_message_unix_fds (new_connection,
197 context->limits.max_message_unix_fds);
199 dbus_connection_set_allow_anonymous (new_connection,
200 context->allow_anonymous);
202 /* on OOM, we won't have ref'd the connection so it will die. */
206 free_server_data (void *data)
208 BusServerData *bd = data;
214 setup_server (BusContext *context,
216 char **auth_mechanisms,
221 bd = dbus_new0 (BusServerData, 1);
222 if (bd == NULL || !dbus_server_set_data (server,
224 bd, free_server_data))
231 bd->context = context;
233 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
239 dbus_server_set_new_connection_function (server,
240 new_connection_callback,
243 if (!dbus_server_set_watch_functions (server,
254 if (!dbus_server_set_timeout_functions (server,
256 remove_server_timeout,
267 /* This code only gets executed the first time the
268 * config files are parsed. It is not executed
269 * when config files are reloaded.
272 process_config_first_time_only (BusContext *context,
273 BusConfigParser *parser,
274 const DBusString *address,
275 BusContextFlags flags,
278 DBusString log_prefix;
280 DBusList **addresses;
281 const char *user, *pidfile;
282 char **auth_mechanisms;
283 DBusList **auth_mechanisms_list;
287 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
290 auth_mechanisms = NULL;
293 _dbus_init_system_log (TRUE);
295 if (flags & BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION)
296 context->systemd_activation = TRUE;
298 context->systemd_activation = FALSE;
300 /* Check for an existing pid file. Of course this is a race;
301 * we'd have to use fcntl() locks on the pid file to
302 * avoid that. But we want to check for the pid file
303 * before overwriting any existing sockets, etc.
306 if (flags & BUS_CONTEXT_FLAG_WRITE_PID_FILE)
307 pidfile = bus_config_parser_get_pidfile (parser);
314 _dbus_string_init_const (&u, pidfile);
316 if (_dbus_stat (&u, &stbuf, NULL))
322 _dbus_string_init (&p);
323 _dbus_file_get_contents(&p, &u, NULL);
324 _dbus_string_parse_int(&p, 0, &pid, NULL);
325 _dbus_string_free(&p);
327 if ((kill((int)pid, 0))) {
328 dbus_set_error(NULL, DBUS_ERROR_FILE_EXISTS,
329 "pid %ld not running, removing stale pid file\n",
331 _dbus_delete_file(&u, NULL);
334 dbus_set_error (error, DBUS_ERROR_FAILED,
335 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
344 /* keep around the pid filename so we can delete it later */
345 context->pidfile = _dbus_strdup (pidfile);
347 /* note that type may be NULL */
348 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
349 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
352 user = bus_config_parser_get_user (parser);
355 context->user = _dbus_strdup (user);
356 if (context->user == NULL)
360 /* Set up the prefix for syslog messages */
361 if (!_dbus_string_init (&log_prefix))
363 if (context->type && !strcmp (context->type, "system"))
365 if (!_dbus_string_append (&log_prefix, "[system] "))
368 else if (context->type && !strcmp (context->type, "session"))
370 DBusCredentials *credentials;
372 credentials = _dbus_credentials_new_from_current_process ();
375 if (!_dbus_string_append (&log_prefix, "[session "))
377 _dbus_credentials_unref (credentials);
380 if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
382 _dbus_credentials_unref (credentials);
385 if (!_dbus_string_append (&log_prefix, "] "))
387 _dbus_credentials_unref (credentials);
390 _dbus_credentials_unref (credentials);
392 if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
394 _dbus_string_free (&log_prefix);
396 /* Build an array of auth mechanisms */
398 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
399 len = _dbus_list_get_length (auth_mechanisms_list);
405 auth_mechanisms = dbus_new0 (char*, len + 1);
406 if (auth_mechanisms == NULL)
410 link = _dbus_list_get_first_link (auth_mechanisms_list);
413 auth_mechanisms[i] = _dbus_strdup (link->data);
414 if (auth_mechanisms[i] == NULL)
416 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
422 auth_mechanisms = NULL;
425 /* Listen on our addresses */
431 server = dbus_server_listen (_dbus_string_get_const_data(address), error);
434 _DBUS_ASSERT_ERROR_IS_SET (error);
437 else if (!setup_server (context, server, auth_mechanisms, error))
439 _DBUS_ASSERT_ERROR_IS_SET (error);
443 if (!_dbus_list_append (&context->servers, server))
448 addresses = bus_config_parser_get_addresses (parser);
450 link = _dbus_list_get_first_link (addresses);
455 server = dbus_server_listen (link->data, error);
458 _DBUS_ASSERT_ERROR_IS_SET (error);
461 else if (!setup_server (context, server, auth_mechanisms, error))
463 _DBUS_ASSERT_ERROR_IS_SET (error);
467 if (!_dbus_list_append (&context->servers, server))
470 link = _dbus_list_get_next_link (addresses, link);
474 context->fork = bus_config_parser_get_fork (parser);
475 context->syslog = bus_config_parser_get_syslog (parser);
476 context->keep_umask = bus_config_parser_get_keep_umask (parser);
477 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
479 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
483 dbus_free_string_array (auth_mechanisms);
488 dbus_free_string_array (auth_mechanisms);
492 /* This code gets executed every time the config files
493 * are parsed: both during BusContext construction
494 * and on reloads. This function is slightly screwy
495 * since it can do a "half reload" in out-of-memory
496 * situations. Realistically, unlikely to ever matter.
499 process_config_every_time (BusContext *context,
500 BusConfigParser *parser,
501 dbus_bool_t is_reload,
504 DBusString full_address;
508 const char *servicehelper;
513 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
518 if (!_dbus_string_init (&full_address))
524 /* get our limits and timeout lengths */
525 bus_config_parser_get_limits (parser, &context->limits);
528 bus_policy_unref (context->policy);
529 context->policy = bus_config_parser_steal_policy (parser);
530 _dbus_assert (context->policy != NULL);
532 /* context->connections is NULL when creating new BusContext */
533 if (context->connections)
535 _dbus_verbose ("Reload policy rules for completed connections\n");
536 retval = bus_connections_reload_policy (context->connections, error);
539 _DBUS_ASSERT_ERROR_IS_SET (error);
544 /* We have to build the address backward, so that
545 * <listen> later in the config file have priority
547 link = _dbus_list_get_last_link (&context->servers);
550 addr = dbus_server_get_address (link->data);
557 if (_dbus_string_get_length (&full_address) > 0)
559 if (!_dbus_string_append (&full_address, ";"))
566 if (!_dbus_string_append (&full_address, addr))
575 link = _dbus_list_get_prev_link (&context->servers, link);
579 dbus_free (context->address);
581 if (!_dbus_string_copy_data (&full_address, &context->address))
587 /* get the service directories */
588 dirs = bus_config_parser_get_service_dirs (parser);
590 /* and the service helper */
591 servicehelper = bus_config_parser_get_servicehelper (parser);
593 s = _dbus_strdup(servicehelper);
594 if (s == NULL && servicehelper != NULL)
601 dbus_free(context->servicehelper);
602 context->servicehelper = s;
605 /* Create activation subsystem */
606 if (context->activation)
608 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
613 context->activation = bus_activation_new (context, &full_address, dirs, error);
616 if (context->activation == NULL)
618 _DBUS_ASSERT_ERROR_IS_SET (error);
622 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
626 _dbus_string_free (&full_address);
635 list_concat_new (DBusList **a,
643 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
645 if (!_dbus_list_append (result, link->data))
648 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
650 if (!_dbus_list_append (result, link->data))
656 _dbus_list_clear (result);
661 raise_file_descriptor_limit (BusContext *context)
664 DBusError error = DBUS_ERROR_INIT;
666 /* we only do this once */
667 if (context->initial_fd_limit != NULL)
670 context->initial_fd_limit = _dbus_rlimit_save_fd_limit (&error);
672 if (context->initial_fd_limit == NULL)
674 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
675 "%s: %s", error.name, error.message);
676 dbus_error_free (&error);
680 /* We used to compute a suitable rlimit based on the configured number
681 * of connections, but that breaks down as soon as we allow fd-passing,
682 * because each connection is allowed to pass 64 fds to us, and if
683 * they all did, we'd hit kernel limits. We now hard-code 64k as a
684 * good limit, like systemd does: that's enough to avoid DoS from
685 * anything short of multiple uids conspiring against us.
687 if (!_dbus_rlimit_raise_fd_limit_if_privileged (65536, &error))
689 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
690 "%s: %s", error.name, error.message);
691 dbus_error_free (&error);
698 process_config_postinit (BusContext *context,
699 BusConfigParser *parser,
702 DBusHashTable *service_context_table;
703 DBusList *watched_dirs = NULL;
705 raise_file_descriptor_limit (context);
707 service_context_table = bus_config_parser_steal_service_context_table (parser);
708 if (!bus_registry_set_service_context_table (context->registry,
709 service_context_table))
715 _dbus_hash_table_unref (service_context_table);
717 /* We need to monitor both the configuration directories and directories
718 * containing .service files.
720 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
721 bus_config_parser_get_service_dirs (parser),
728 bus_set_watched_dirs (context, &watched_dirs);
730 _dbus_list_clear (&watched_dirs);
736 bus_context_new (const DBusString *config_file,
737 BusContextFlags flags,
738 DBusPipe *print_addr_pipe,
739 DBusPipe *print_pid_pipe,
740 const DBusString *address,
744 BusConfigParser *parser;
746 _dbus_assert ((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 ||
747 (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS) == 0);
749 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
754 if (!dbus_server_allocate_data_slot (&server_data_slot))
760 context = dbus_new0 (BusContext, 1);
766 context->refcount = 1;
768 _dbus_generate_uuid (&context->uuid);
770 if (!_dbus_string_copy_data (config_file, &context->config_file))
776 context->loop = _dbus_loop_new ();
777 if (context->loop == NULL)
783 context->watches_enabled = TRUE;
785 context->registry = bus_registry_new (context);
786 if (context->registry == NULL)
792 parser = bus_config_load (config_file, TRUE, NULL, error);
795 _DBUS_ASSERT_ERROR_IS_SET (error);
799 if (!process_config_first_time_only (context, parser, address, flags, error))
801 _DBUS_ASSERT_ERROR_IS_SET (error);
804 if (!process_config_every_time (context, parser, FALSE, error))
806 _DBUS_ASSERT_ERROR_IS_SET (error);
810 /* we need another ref of the server data slot for the context
813 if (!dbus_server_allocate_data_slot (&server_data_slot))
814 _dbus_assert_not_reached ("second ref of server data slot failed");
816 /* Note that we don't know whether the print_addr_pipe is
817 * one of the sockets we're using to listen on, or some
818 * other random thing. But I think the answer is "don't do
821 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
824 const char *a = bus_context_get_address (context);
827 _dbus_assert (a != NULL);
828 if (!_dbus_string_init (&addr))
834 if (!_dbus_string_append (&addr, a) ||
835 !_dbus_string_append (&addr, "\n"))
837 _dbus_string_free (&addr);
842 bytes = _dbus_string_get_length (&addr);
843 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
845 /* pipe write returns an error on failure but not short write */
846 if (error != NULL && !dbus_error_is_set (error))
848 dbus_set_error (error, DBUS_ERROR_FAILED,
849 "Printing message bus address: did not write all bytes\n");
851 _dbus_string_free (&addr);
855 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
856 _dbus_pipe_close (print_addr_pipe, NULL);
858 _dbus_string_free (&addr);
861 context->connections = bus_connections_new (context);
862 if (context->connections == NULL)
868 context->matchmaker = bus_matchmaker_new ();
869 if (context->matchmaker == NULL)
875 /* check user before we fork */
876 if (context->user != NULL)
878 if (!_dbus_verify_daemon_user (context->user))
880 dbus_set_error (error, DBUS_ERROR_FAILED,
881 "Could not get UID and GID for username \"%s\"",
887 /* Now become a daemon if appropriate and write out pid file in any case */
891 if (context->pidfile)
892 _dbus_string_init_const (&u, context->pidfile);
894 if (((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 && context->fork) ||
895 (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS))
897 _dbus_verbose ("Forking and becoming daemon\n");
899 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
902 context->keep_umask))
904 _DBUS_ASSERT_ERROR_IS_SET (error);
910 _dbus_verbose ("Fork not requested\n");
912 /* Need to write PID file and to PID pipe for ourselves,
913 * not for the child process. This is a no-op if the pidfile
914 * is NULL and print_pid_pipe is NULL.
916 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
921 _DBUS_ASSERT_ERROR_IS_SET (error);
927 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
928 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
929 _dbus_pipe_close (print_pid_pipe, NULL);
931 if (!bus_selinux_full_init ())
933 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but D-Bus initialization failed; check system log\n");
936 if (!process_config_postinit (context, parser, error))
938 _DBUS_ASSERT_ERROR_IS_SET (error);
944 bus_config_parser_unref (parser);
948 /* Here we change our credentials if required,
949 * as soon as we've set up our sockets and pidfile
951 if (context->user != NULL)
953 if (!_dbus_change_to_daemon_user (context->user, error))
955 _DBUS_ASSERT_ERROR_IS_SET (error);
960 /* FIXME - why not just put this in full_init() below? */
961 bus_selinux_audit_init ();
965 dbus_server_free_data_slot (&server_data_slot);
971 bus_config_parser_unref (parser);
973 bus_context_unref (context);
975 if (server_data_slot >= 0)
976 dbus_server_free_data_slot (&server_data_slot);
982 bus_context_get_id (BusContext *context,
985 return _dbus_uuid_encode (&context->uuid, uuid);
989 bus_context_reload_config (BusContext *context,
992 BusConfigParser *parser;
993 DBusString config_file;
996 /* Flush the user database cache */
997 _dbus_flush_caches ();
1000 _dbus_string_init_const (&config_file, context->config_file);
1001 parser = bus_config_load (&config_file, TRUE, NULL, error);
1004 _DBUS_ASSERT_ERROR_IS_SET (error);
1008 if (!process_config_every_time (context, parser, TRUE, error))
1010 _DBUS_ASSERT_ERROR_IS_SET (error);
1013 if (!process_config_postinit (context, parser, error))
1015 _DBUS_ASSERT_ERROR_IS_SET (error);
1020 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
1023 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
1025 bus_config_parser_unref (parser);
1030 shutdown_server (BusContext *context,
1033 if (server == NULL ||
1034 !dbus_server_get_is_connected (server))
1037 if (!dbus_server_set_watch_functions (server,
1041 _dbus_assert_not_reached ("setting watch functions to NULL failed");
1043 if (!dbus_server_set_timeout_functions (server,
1047 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
1049 dbus_server_disconnect (server);
1053 bus_context_shutdown (BusContext *context)
1057 link = _dbus_list_get_first_link (&context->servers);
1058 while (link != NULL)
1060 shutdown_server (context, link->data);
1062 link = _dbus_list_get_next_link (&context->servers, link);
1067 bus_context_ref (BusContext *context)
1069 _dbus_assert (context->refcount > 0);
1070 context->refcount += 1;
1076 bus_context_unref (BusContext *context)
1078 _dbus_assert (context->refcount > 0);
1079 context->refcount -= 1;
1081 if (context->refcount == 0)
1085 _dbus_verbose ("Finalizing bus context %p\n", context);
1087 bus_context_shutdown (context);
1089 if (context->connections)
1091 bus_connections_unref (context->connections);
1092 context->connections = NULL;
1095 if (context->registry)
1097 bus_registry_unref (context->registry);
1098 context->registry = NULL;
1101 if (context->activation)
1103 bus_activation_unref (context->activation);
1104 context->activation = NULL;
1107 link = _dbus_list_get_first_link (&context->servers);
1108 while (link != NULL)
1110 dbus_server_unref (link->data);
1112 link = _dbus_list_get_next_link (&context->servers, link);
1114 _dbus_list_clear (&context->servers);
1116 if (context->policy)
1118 bus_policy_unref (context->policy);
1119 context->policy = NULL;
1124 _dbus_loop_unref (context->loop);
1125 context->loop = NULL;
1128 if (context->matchmaker)
1130 bus_matchmaker_unref (context->matchmaker);
1131 context->matchmaker = NULL;
1134 dbus_free (context->config_file);
1135 dbus_free (context->log_prefix);
1136 dbus_free (context->type);
1137 dbus_free (context->address);
1138 dbus_free (context->user);
1139 dbus_free (context->servicehelper);
1141 if (context->pidfile)
1144 _dbus_string_init_const (&u, context->pidfile);
1146 /* Deliberately ignore errors here, since there's not much
1147 * we can do about it, and we're exiting anyways.
1149 _dbus_delete_file (&u, NULL);
1151 dbus_free (context->pidfile);
1154 if (context->initial_fd_limit)
1155 _dbus_rlimit_free (context->initial_fd_limit);
1157 dbus_free (context);
1159 dbus_server_free_data_slot (&server_data_slot);
1163 /* type may be NULL */
1165 bus_context_get_type (BusContext *context)
1167 return context->type;
1171 bus_context_get_address (BusContext *context)
1173 return context->address;
1177 bus_context_get_servicehelper (BusContext *context)
1179 return context->servicehelper;
1183 bus_context_get_systemd_activation (BusContext *context)
1185 return context->systemd_activation;
1189 bus_context_get_registry (BusContext *context)
1191 return context->registry;
1195 bus_context_get_connections (BusContext *context)
1197 return context->connections;
1201 bus_context_get_activation (BusContext *context)
1203 return context->activation;
1207 bus_context_get_matchmaker (BusContext *context)
1209 return context->matchmaker;
1213 bus_context_get_loop (BusContext *context)
1215 return context->loop;
1219 bus_context_allow_unix_user (BusContext *context,
1222 return bus_policy_allow_unix_user (context->policy,
1226 /* For now this is never actually called because the default
1227 * DBusConnection behavior of 'same user that owns the bus can connect'
1228 * is all it would do.
1231 bus_context_allow_windows_user (BusContext *context,
1232 const char *windows_sid)
1234 return bus_policy_allow_windows_user (context->policy,
1239 bus_context_get_policy (BusContext *context)
1241 return context->policy;
1245 bus_context_create_client_policy (BusContext *context,
1246 DBusConnection *connection,
1249 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1250 return bus_policy_create_client_policy (context->policy, connection,
1255 bus_context_get_activation_timeout (BusContext *context)
1258 return context->limits.activation_timeout;
1262 bus_context_get_auth_timeout (BusContext *context)
1264 return context->limits.auth_timeout;
1268 bus_context_get_pending_fd_timeout (BusContext *context)
1270 return context->limits.pending_fd_timeout;
1274 bus_context_get_max_completed_connections (BusContext *context)
1276 return context->limits.max_completed_connections;
1280 bus_context_get_max_incomplete_connections (BusContext *context)
1282 return context->limits.max_incomplete_connections;
1286 bus_context_get_max_connections_per_user (BusContext *context)
1288 return context->limits.max_connections_per_user;
1292 bus_context_get_max_pending_activations (BusContext *context)
1294 return context->limits.max_pending_activations;
1298 bus_context_get_max_services_per_connection (BusContext *context)
1300 return context->limits.max_services_per_connection;
1304 bus_context_get_max_match_rules_per_connection (BusContext *context)
1306 return context->limits.max_match_rules_per_connection;
1310 bus_context_get_max_replies_per_connection (BusContext *context)
1312 return context->limits.max_replies_per_connection;
1316 bus_context_get_reply_timeout (BusContext *context)
1318 return context->limits.reply_timeout;
1322 bus_context_get_initial_fd_limit (BusContext *context)
1324 return context->initial_fd_limit;
1328 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
1331 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1335 if (!context->syslog)
1337 /* we're not syslogging; just output to stderr */
1338 va_start (args, msg);
1339 vfprintf (stderr, msg, args);
1340 fprintf (stderr, "\n");
1343 if (severity == DBUS_SYSTEM_LOG_FATAL)
1349 va_start (args, msg);
1351 if (context->log_prefix)
1353 DBusString full_msg;
1355 if (!_dbus_string_init (&full_msg))
1357 if (!_dbus_string_append (&full_msg, context->log_prefix))
1359 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1362 _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1364 _dbus_string_free (&full_msg);
1367 _dbus_system_logv (severity, msg, args);
1373 static inline const char *
1374 nonnull (const char *maybe_null,
1375 const char *if_null)
1377 return (maybe_null ? maybe_null : if_null);
1381 * Log something about a message, usually that it was rejected.
1384 complain_about_message (BusContext *context,
1385 const char *error_name,
1386 const char *complaint,
1388 DBusMessage *message,
1389 DBusConnection *sender,
1390 DBusConnection *proposed_recipient,
1391 dbus_bool_t requested_reply,
1395 DBusError stack_error = DBUS_ERROR_INIT;
1396 const char *sender_name;
1397 const char *sender_loginfo;
1398 const char *proposed_recipient_loginfo;
1400 if (error == NULL && !log)
1405 sender_name = bus_connection_get_name (sender);
1406 sender_loginfo = bus_connection_get_loginfo (sender);
1410 sender_name = "(unset)";
1411 sender_loginfo = "(bus)";
1414 if (proposed_recipient != NULL)
1415 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1417 proposed_recipient_loginfo = "bus";
1419 dbus_set_error (&stack_error, error_name,
1420 "%s, %d matched rules; type=\"%s\", sender=\"%s\" (%s) "
1421 "interface=\"%s\" member=\"%s\" error name=\"%s\" "
1422 "requested_reply=\"%d\" destination=\"%s\" (%s)",
1425 dbus_message_type_to_string (dbus_message_get_type (message)),
1428 nonnull (dbus_message_get_interface (message), "(unset)"),
1429 nonnull (dbus_message_get_member (message), "(unset)"),
1430 nonnull (dbus_message_get_error_name (message), "(unset)"),
1432 nonnull (dbus_message_get_destination (message), DBUS_SERVICE_DBUS),
1433 proposed_recipient_loginfo);
1435 /* If we hit OOM while setting the error, this will syslog "out of memory"
1436 * which is itself an indication that something is seriously wrong */
1438 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, "%s",
1439 stack_error.message);
1441 dbus_move_error (&stack_error, error);
1445 * addressed_recipient is the recipient specified in the message.
1447 * proposed_recipient is the recipient we're considering sending
1448 * to right this second, and may be an eavesdropper.
1450 * sender is the sender of the message.
1452 * NULL for proposed_recipient or sender definitely means the bus driver.
1454 * NULL for addressed_recipient may mean the bus driver, or may mean
1455 * no destination was specified in the message (e.g. a signal).
1458 bus_context_check_security_policy (BusContext *context,
1459 BusTransaction *transaction,
1460 DBusConnection *sender,
1461 DBusConnection *addressed_recipient,
1462 DBusConnection *proposed_recipient,
1463 DBusMessage *message,
1467 BusClientPolicy *sender_policy;
1468 BusClientPolicy *recipient_policy;
1469 dbus_int32_t toggles;
1472 dbus_bool_t requested_reply;
1474 type = dbus_message_get_type (message);
1475 dest = dbus_message_get_destination (message);
1477 /* dispatch.c was supposed to ensure these invariants */
1478 _dbus_assert (dest != NULL ||
1479 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1480 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1481 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1482 addressed_recipient != NULL ||
1483 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1487 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1488 case DBUS_MESSAGE_TYPE_SIGNAL:
1489 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1490 case DBUS_MESSAGE_TYPE_ERROR:
1494 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1497 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1498 "Message bus will not accept messages of unknown type\n");
1503 requested_reply = FALSE;
1507 /* First verify the SELinux access controls. If allowed then
1508 * go on with the standard checks.
1510 if (!bus_selinux_allows_send (sender, proposed_recipient,
1511 dbus_message_type_to_string (dbus_message_get_type (message)),
1512 dbus_message_get_interface (message),
1513 dbus_message_get_member (message),
1514 dbus_message_get_error_name (message),
1515 dest ? dest : DBUS_SERVICE_DBUS, error))
1517 if (error != NULL && !dbus_error_is_set (error))
1519 /* don't syslog this, just set the error: avc_has_perm should
1520 * have already written to either the audit log or syslog */
1521 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1522 "An SELinux policy prevents this sender from sending this "
1523 "message to this recipient",
1524 0, message, sender, proposed_recipient, FALSE, FALSE, error);
1525 _dbus_verbose ("SELinux security check denying send to service\n");
1531 if (bus_connection_is_active (sender))
1533 sender_policy = bus_connection_get_policy (sender);
1534 _dbus_assert (sender_policy != NULL);
1536 /* Fill in requested_reply variable with TRUE if this is a
1537 * reply and the reply was pending.
1539 if (dbus_message_get_reply_serial (message) != 0)
1541 if (proposed_recipient != NULL /* not to the bus driver */ &&
1542 addressed_recipient == proposed_recipient /* not eavesdropping */)
1546 dbus_error_init (&error2);
1547 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1549 sender, addressed_recipient, message,
1551 if (dbus_error_is_set (&error2))
1553 dbus_move_error (&error2, error);
1561 /* Policy for inactive connections is that they can only send
1562 * the hello message to the bus driver
1564 if (proposed_recipient == NULL &&
1565 dbus_message_is_method_call (message,
1566 DBUS_INTERFACE_DBUS,
1569 _dbus_verbose ("security check allowing %s message\n",
1575 _dbus_verbose ("security check disallowing non-%s message\n",
1578 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1579 "Client tried to send a message other than %s without being registered",
1588 sender_policy = NULL;
1590 /* If the sender is the bus driver, we assume any reply was a
1591 * requested reply as bus driver won't send bogus ones
1593 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1594 dbus_message_get_reply_serial (message) != 0)
1595 requested_reply = TRUE;
1598 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1599 (sender == NULL && sender_policy == NULL));
1601 if (proposed_recipient != NULL)
1603 /* only the bus driver can send to an inactive recipient (as it
1604 * owns no services, so other apps can't address it). Inactive
1605 * recipients can receive any message.
1607 if (bus_connection_is_active (proposed_recipient))
1609 recipient_policy = bus_connection_get_policy (proposed_recipient);
1610 _dbus_assert (recipient_policy != NULL);
1612 else if (sender == NULL)
1614 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1615 recipient_policy = NULL;
1619 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1620 recipient_policy = NULL;
1624 recipient_policy = NULL;
1626 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1627 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1628 (proposed_recipient == NULL && recipient_policy == NULL));
1631 if (sender_policy &&
1632 !bus_client_policy_check_can_send (sender_policy,
1636 message, &toggles, &log))
1638 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1639 "Rejected send message", toggles,
1640 message, sender, proposed_recipient, requested_reply,
1641 (addressed_recipient == proposed_recipient), error);
1642 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1648 /* We want to drop this message, and are only not doing so for backwards
1650 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1651 "Would reject message", toggles,
1652 message, sender, proposed_recipient, requested_reply,
1656 if (recipient_policy &&
1657 !bus_client_policy_check_can_receive (recipient_policy,
1661 addressed_recipient, proposed_recipient,
1664 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1665 "Rejected receive message", toggles,
1666 message, sender, proposed_recipient, requested_reply,
1667 (addressed_recipient == proposed_recipient), error);
1668 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1672 /* See if limits on size have been exceeded */
1673 if (proposed_recipient &&
1674 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1675 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1677 complain_about_message (context, DBUS_ERROR_LIMITS_EXCEEDED,
1678 "Rejected: destination has a full message queue",
1679 0, message, sender, proposed_recipient, requested_reply, TRUE,
1681 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1685 /* Record that we will allow a reply here in the future (don't
1686 * bother if the recipient is the bus or this is an eavesdropping
1687 * connection). Only the addressed recipient may reply.
1689 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1691 addressed_recipient &&
1692 addressed_recipient == proposed_recipient && /* not eavesdropping */
1693 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1695 sender, addressed_recipient,
1698 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1702 _dbus_verbose ("security policy allowing message\n");
1707 bus_context_check_all_watches (BusContext *context)
1710 dbus_bool_t enabled = TRUE;
1712 if (bus_connections_get_n_incomplete (context->connections) >=
1713 bus_context_get_max_incomplete_connections (context))
1718 if (context->watches_enabled == enabled)
1721 context->watches_enabled = enabled;
1723 for (link = _dbus_list_get_first_link (&context->servers);
1725 link = _dbus_list_get_next_link (&context->servers, link))
1727 /* A BusContext might contains several DBusServer (if there are
1728 * several <listen> configuration items) and a DBusServer might
1729 * contain several DBusWatch in its DBusWatchList (if getaddrinfo
1730 * returns several addresses on a dual IPv4-IPv6 stack or if
1731 * systemd passes several fds).
1732 * We want to enable/disable them all.
1734 DBusServer *server = link->data;
1735 _dbus_server_toggle_all_watches (server, enabled);