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"
38 #include "dir-watch.h"
39 #include <dbus/dbus-list.h>
40 #include <dbus/dbus-hash.h>
41 #include <dbus/dbus-credentials.h>
42 #include <dbus/dbus-internals.h>
43 #include <dbus/dbus-server-protected.h>
62 BusConnections *connections;
63 BusActivation *activation;
64 BusRegistry *registry;
66 BusMatchmaker *matchmaker;
68 DBusRLimit *initial_fd_limit;
69 unsigned int fork : 1;
70 unsigned int syslog : 1;
71 unsigned int keep_umask : 1;
72 unsigned int allow_anonymous : 1;
73 unsigned int systemd_activation : 1;
74 dbus_bool_t watches_enabled;
77 static dbus_int32_t server_data_slot = -1;
84 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
87 server_get_context (DBusServer *server)
92 if (!dbus_server_allocate_data_slot (&server_data_slot))
95 bd = BUS_SERVER_DATA (server);
98 dbus_server_free_data_slot (&server_data_slot);
102 context = bd->context;
104 dbus_server_free_data_slot (&server_data_slot);
110 add_server_watch (DBusWatch *watch,
113 DBusServer *server = data;
116 context = server_get_context (server);
118 return _dbus_loop_add_watch (context->loop, watch);
122 remove_server_watch (DBusWatch *watch,
125 DBusServer *server = data;
128 context = server_get_context (server);
130 _dbus_loop_remove_watch (context->loop, watch);
134 toggle_server_watch (DBusWatch *watch,
137 DBusServer *server = data;
140 context = server_get_context (server);
142 _dbus_loop_toggle_watch (context->loop, watch);
146 add_server_timeout (DBusTimeout *timeout,
149 DBusServer *server = data;
152 context = server_get_context (server);
154 return _dbus_loop_add_timeout (context->loop, timeout);
158 remove_server_timeout (DBusTimeout *timeout,
161 DBusServer *server = data;
164 context = server_get_context (server);
166 _dbus_loop_remove_timeout (context->loop, timeout);
170 new_connection_callback (DBusServer *server,
171 DBusConnection *new_connection,
174 BusContext *context = data;
176 if (!bus_connections_setup_connection (context->connections, new_connection))
178 _dbus_verbose ("No memory to setup new connection\n");
180 /* if we don't do this, it will get unref'd without
181 * being disconnected... kind of strange really
182 * that we have to do this, people won't get it right
185 dbus_connection_close (new_connection);
188 dbus_connection_set_max_received_size (new_connection,
189 context->limits.max_incoming_bytes);
191 dbus_connection_set_max_message_size (new_connection,
192 context->limits.max_message_size);
194 dbus_connection_set_max_received_unix_fds (new_connection,
195 context->limits.max_incoming_unix_fds);
197 dbus_connection_set_max_message_unix_fds (new_connection,
198 context->limits.max_message_unix_fds);
200 dbus_connection_set_allow_anonymous (new_connection,
201 context->allow_anonymous);
203 /* on OOM, we won't have ref'd the connection so it will die. */
207 free_server_data (void *data)
209 BusServerData *bd = data;
215 setup_server (BusContext *context,
217 char **auth_mechanisms,
222 bd = dbus_new0 (BusServerData, 1);
223 if (bd == NULL || !dbus_server_set_data (server,
225 bd, free_server_data))
232 bd->context = context;
234 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
240 dbus_server_set_new_connection_function (server,
241 new_connection_callback,
244 if (!dbus_server_set_watch_functions (server,
255 if (!dbus_server_set_timeout_functions (server,
257 remove_server_timeout,
268 /* This code only gets executed the first time the
269 * config files are parsed. It is not executed
270 * when config files are reloaded.
273 process_config_first_time_only (BusContext *context,
274 BusConfigParser *parser,
275 const DBusString *address,
276 BusContextFlags flags,
279 DBusString log_prefix;
281 DBusList **addresses;
282 const char *user, *pidfile;
283 char **auth_mechanisms;
284 DBusList **auth_mechanisms_list;
288 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
291 auth_mechanisms = NULL;
294 _dbus_init_system_log (TRUE);
296 if (flags & BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION)
297 context->systemd_activation = TRUE;
299 context->systemd_activation = FALSE;
301 /* Check for an existing pid file. Of course this is a race;
302 * we'd have to use fcntl() locks on the pid file to
303 * avoid that. But we want to check for the pid file
304 * before overwriting any existing sockets, etc.
307 if (flags & BUS_CONTEXT_FLAG_WRITE_PID_FILE)
308 pidfile = bus_config_parser_get_pidfile (parser);
315 _dbus_string_init_const (&u, pidfile);
317 if (_dbus_stat (&u, &stbuf, NULL))
323 _dbus_string_init (&p);
324 _dbus_file_get_contents(&p, &u, NULL);
325 _dbus_string_parse_int(&p, 0, &pid, NULL);
326 _dbus_string_free(&p);
328 if ((kill((int)pid, 0))) {
329 dbus_set_error(NULL, DBUS_ERROR_FILE_EXISTS,
330 "pid %ld not running, removing stale pid file\n",
332 _dbus_delete_file(&u, NULL);
335 dbus_set_error (error, DBUS_ERROR_FAILED,
336 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
345 /* keep around the pid filename so we can delete it later */
346 context->pidfile = _dbus_strdup (pidfile);
348 /* note that type may be NULL */
349 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
350 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
353 user = bus_config_parser_get_user (parser);
356 context->user = _dbus_strdup (user);
357 if (context->user == NULL)
361 /* Set up the prefix for syslog messages */
362 if (!_dbus_string_init (&log_prefix))
364 if (context->type && !strcmp (context->type, "system"))
366 if (!_dbus_string_append (&log_prefix, "[system] "))
369 else if (context->type && !strcmp (context->type, "session"))
371 DBusCredentials *credentials;
373 credentials = _dbus_credentials_new_from_current_process ();
376 if (!_dbus_string_append (&log_prefix, "[session "))
378 _dbus_credentials_unref (credentials);
381 if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
383 _dbus_credentials_unref (credentials);
386 if (!_dbus_string_append (&log_prefix, "] "))
388 _dbus_credentials_unref (credentials);
391 _dbus_credentials_unref (credentials);
393 if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
395 _dbus_string_free (&log_prefix);
397 /* Build an array of auth mechanisms */
399 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
400 len = _dbus_list_get_length (auth_mechanisms_list);
406 auth_mechanisms = dbus_new0 (char*, len + 1);
407 if (auth_mechanisms == NULL)
411 link = _dbus_list_get_first_link (auth_mechanisms_list);
414 auth_mechanisms[i] = _dbus_strdup (link->data);
415 if (auth_mechanisms[i] == NULL)
417 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
423 auth_mechanisms = NULL;
426 /* Listen on our addresses */
432 server = dbus_server_listen (_dbus_string_get_const_data(address), error);
435 _DBUS_ASSERT_ERROR_IS_SET (error);
438 else if (!setup_server (context, server, auth_mechanisms, error))
440 _DBUS_ASSERT_ERROR_IS_SET (error);
444 if (!_dbus_list_append (&context->servers, server))
449 addresses = bus_config_parser_get_addresses (parser);
451 link = _dbus_list_get_first_link (addresses);
456 server = dbus_server_listen (link->data, error);
459 _DBUS_ASSERT_ERROR_IS_SET (error);
462 else if (!setup_server (context, server, auth_mechanisms, error))
464 _DBUS_ASSERT_ERROR_IS_SET (error);
468 if (!_dbus_list_append (&context->servers, server))
471 link = _dbus_list_get_next_link (addresses, link);
475 context->fork = bus_config_parser_get_fork (parser);
476 context->syslog = bus_config_parser_get_syslog (parser);
477 context->keep_umask = bus_config_parser_get_keep_umask (parser);
478 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
480 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
484 dbus_free_string_array (auth_mechanisms);
489 dbus_free_string_array (auth_mechanisms);
493 /* This code gets executed every time the config files
494 * are parsed: both during BusContext construction
495 * and on reloads. This function is slightly screwy
496 * since it can do a "half reload" in out-of-memory
497 * situations. Realistically, unlikely to ever matter.
500 process_config_every_time (BusContext *context,
501 BusConfigParser *parser,
502 dbus_bool_t is_reload,
505 DBusString full_address;
509 const char *servicehelper;
514 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
519 if (!_dbus_string_init (&full_address))
525 /* get our limits and timeout lengths */
526 bus_config_parser_get_limits (parser, &context->limits);
529 bus_policy_unref (context->policy);
530 context->policy = bus_config_parser_steal_policy (parser);
531 _dbus_assert (context->policy != NULL);
533 /* context->connections is NULL when creating new BusContext */
534 if (context->connections)
536 _dbus_verbose ("Reload policy rules for completed connections\n");
537 retval = bus_connections_reload_policy (context->connections, error);
540 _DBUS_ASSERT_ERROR_IS_SET (error);
545 /* We have to build the address backward, so that
546 * <listen> later in the config file have priority
548 link = _dbus_list_get_last_link (&context->servers);
551 addr = dbus_server_get_address (link->data);
558 if (_dbus_string_get_length (&full_address) > 0)
560 if (!_dbus_string_append (&full_address, ";"))
567 if (!_dbus_string_append (&full_address, addr))
576 link = _dbus_list_get_prev_link (&context->servers, link);
580 dbus_free (context->address);
582 if (!_dbus_string_copy_data (&full_address, &context->address))
588 /* get the service directories */
589 dirs = bus_config_parser_get_service_dirs (parser);
591 /* and the service helper */
592 servicehelper = bus_config_parser_get_servicehelper (parser);
594 s = _dbus_strdup(servicehelper);
595 if (s == NULL && servicehelper != NULL)
602 dbus_free(context->servicehelper);
603 context->servicehelper = s;
606 /* Create activation subsystem */
607 if (context->activation)
609 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
614 context->activation = bus_activation_new (context, &full_address, dirs, error);
617 if (context->activation == NULL)
619 _DBUS_ASSERT_ERROR_IS_SET (error);
623 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
627 _dbus_string_free (&full_address);
636 list_concat_new (DBusList **a,
644 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
646 if (!_dbus_list_append (result, link->data))
649 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
651 if (!_dbus_list_append (result, link->data))
657 _dbus_list_clear (result);
662 raise_file_descriptor_limit (BusContext *context)
665 DBusError error = DBUS_ERROR_INIT;
667 /* we only do this once */
668 if (context->initial_fd_limit != NULL)
671 context->initial_fd_limit = _dbus_rlimit_save_fd_limit (&error);
673 if (context->initial_fd_limit == NULL)
675 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
676 "%s: %s", error.name, error.message);
677 dbus_error_free (&error);
681 /* We used to compute a suitable rlimit based on the configured number
682 * of connections, but that breaks down as soon as we allow fd-passing,
683 * because each connection is allowed to pass 64 fds to us, and if
684 * they all did, we'd hit kernel limits. We now hard-code 64k as a
685 * good limit, like systemd does: that's enough to avoid DoS from
686 * anything short of multiple uids conspiring against us.
688 if (!_dbus_rlimit_raise_fd_limit_if_privileged (65536, &error))
690 bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
691 "%s: %s", error.name, error.message);
692 dbus_error_free (&error);
699 process_config_postinit (BusContext *context,
700 BusConfigParser *parser,
703 DBusHashTable *service_context_table;
704 DBusList *watched_dirs = NULL;
706 raise_file_descriptor_limit (context);
708 service_context_table = bus_config_parser_steal_service_context_table (parser);
709 if (!bus_registry_set_service_context_table (context->registry,
710 service_context_table))
716 _dbus_hash_table_unref (service_context_table);
718 /* We need to monitor both the configuration directories and directories
719 * containing .service files.
721 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
722 bus_config_parser_get_service_dirs (parser),
729 bus_set_watched_dirs (context, &watched_dirs);
731 _dbus_list_clear (&watched_dirs);
737 bus_context_new (const DBusString *config_file,
738 BusContextFlags flags,
739 DBusPipe *print_addr_pipe,
740 DBusPipe *print_pid_pipe,
741 const DBusString *address,
745 BusConfigParser *parser;
747 _dbus_assert ((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 ||
748 (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS) == 0);
750 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
755 if (!dbus_server_allocate_data_slot (&server_data_slot))
761 context = dbus_new0 (BusContext, 1);
767 context->refcount = 1;
769 _dbus_generate_uuid (&context->uuid);
771 if (!_dbus_string_copy_data (config_file, &context->config_file))
777 context->loop = _dbus_loop_new ();
778 if (context->loop == NULL)
784 context->watches_enabled = TRUE;
786 context->registry = bus_registry_new (context);
787 if (context->registry == NULL)
793 parser = bus_config_load (config_file, TRUE, NULL, error);
796 _DBUS_ASSERT_ERROR_IS_SET (error);
800 if (!process_config_first_time_only (context, parser, address, flags, error))
802 _DBUS_ASSERT_ERROR_IS_SET (error);
805 if (!process_config_every_time (context, parser, FALSE, error))
807 _DBUS_ASSERT_ERROR_IS_SET (error);
811 /* we need another ref of the server data slot for the context
814 if (!dbus_server_allocate_data_slot (&server_data_slot))
815 _dbus_assert_not_reached ("second ref of server data slot failed");
817 /* Note that we don't know whether the print_addr_pipe is
818 * one of the sockets we're using to listen on, or some
819 * other random thing. But I think the answer is "don't do
822 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
825 const char *a = bus_context_get_address (context);
828 _dbus_assert (a != NULL);
829 if (!_dbus_string_init (&addr))
835 if (!_dbus_string_append (&addr, a) ||
836 !_dbus_string_append (&addr, "\n"))
838 _dbus_string_free (&addr);
843 bytes = _dbus_string_get_length (&addr);
844 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
846 /* pipe write returns an error on failure but not short write */
847 if (error != NULL && !dbus_error_is_set (error))
849 dbus_set_error (error, DBUS_ERROR_FAILED,
850 "Printing message bus address: did not write all bytes\n");
852 _dbus_string_free (&addr);
856 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
857 _dbus_pipe_close (print_addr_pipe, NULL);
859 _dbus_string_free (&addr);
862 context->connections = bus_connections_new (context);
863 if (context->connections == NULL)
869 context->matchmaker = bus_matchmaker_new ();
870 if (context->matchmaker == NULL)
876 /* check user before we fork */
877 if (context->user != NULL)
879 if (!_dbus_verify_daemon_user (context->user))
881 dbus_set_error (error, DBUS_ERROR_FAILED,
882 "Could not get UID and GID for username \"%s\"",
888 /* Now become a daemon if appropriate and write out pid file in any case */
892 if (context->pidfile)
893 _dbus_string_init_const (&u, context->pidfile);
895 if (((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 && context->fork) ||
896 (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS))
898 _dbus_verbose ("Forking and becoming daemon\n");
900 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
903 context->keep_umask))
905 _DBUS_ASSERT_ERROR_IS_SET (error);
911 _dbus_verbose ("Fork not requested\n");
913 /* Need to write PID file and to PID pipe for ourselves,
914 * not for the child process. This is a no-op if the pidfile
915 * is NULL and print_pid_pipe is NULL.
917 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
922 _DBUS_ASSERT_ERROR_IS_SET (error);
928 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
929 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
930 _dbus_pipe_close (print_pid_pipe, NULL);
932 if (!bus_selinux_full_init ())
934 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but D-Bus initialization failed; check system log\n");
937 if (!bus_apparmor_full_init (error))
939 _DBUS_ASSERT_ERROR_IS_SET (error);
943 if (bus_apparmor_enabled ())
945 /* Only print AppArmor mediation message when syslog support is enabled */
947 bus_context_log (context, DBUS_SYSTEM_LOG_INFO,
948 "AppArmor D-Bus mediation is enabled\n");
951 if (!process_config_postinit (context, parser, error))
953 _DBUS_ASSERT_ERROR_IS_SET (error);
959 bus_config_parser_unref (parser);
963 /* Here we change our credentials if required,
964 * as soon as we've set up our sockets and pidfile
966 if (context->user != NULL)
968 if (!_dbus_change_to_daemon_user (context->user, error))
970 _DBUS_ASSERT_ERROR_IS_SET (error);
975 /* FIXME - why not just put this in full_init() below? */
976 bus_selinux_audit_init ();
979 bus_apparmor_audit_init ();
983 dbus_server_free_data_slot (&server_data_slot);
989 bus_config_parser_unref (parser);
991 bus_context_unref (context);
993 if (server_data_slot >= 0)
994 dbus_server_free_data_slot (&server_data_slot);
1000 bus_context_get_id (BusContext *context,
1003 return _dbus_uuid_encode (&context->uuid, uuid);
1007 bus_context_reload_config (BusContext *context,
1010 BusConfigParser *parser;
1011 DBusString config_file;
1014 /* Flush the user database cache */
1015 _dbus_flush_caches ();
1018 _dbus_string_init_const (&config_file, context->config_file);
1019 parser = bus_config_load (&config_file, TRUE, NULL, error);
1022 _DBUS_ASSERT_ERROR_IS_SET (error);
1026 if (!process_config_every_time (context, parser, TRUE, error))
1028 _DBUS_ASSERT_ERROR_IS_SET (error);
1031 if (!process_config_postinit (context, parser, error))
1033 _DBUS_ASSERT_ERROR_IS_SET (error);
1038 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
1041 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
1043 bus_config_parser_unref (parser);
1048 shutdown_server (BusContext *context,
1051 if (server == NULL ||
1052 !dbus_server_get_is_connected (server))
1055 if (!dbus_server_set_watch_functions (server,
1059 _dbus_assert_not_reached ("setting watch functions to NULL failed");
1061 if (!dbus_server_set_timeout_functions (server,
1065 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
1067 dbus_server_disconnect (server);
1071 bus_context_shutdown (BusContext *context)
1075 link = _dbus_list_get_first_link (&context->servers);
1076 while (link != NULL)
1078 shutdown_server (context, link->data);
1080 link = _dbus_list_get_next_link (&context->servers, link);
1085 bus_context_ref (BusContext *context)
1087 _dbus_assert (context->refcount > 0);
1088 context->refcount += 1;
1094 bus_context_unref (BusContext *context)
1096 _dbus_assert (context->refcount > 0);
1097 context->refcount -= 1;
1099 if (context->refcount == 0)
1103 _dbus_verbose ("Finalizing bus context %p\n", context);
1105 bus_context_shutdown (context);
1107 if (context->connections)
1109 bus_connections_unref (context->connections);
1110 context->connections = NULL;
1113 if (context->registry)
1115 bus_registry_unref (context->registry);
1116 context->registry = NULL;
1119 if (context->activation)
1121 bus_activation_unref (context->activation);
1122 context->activation = NULL;
1125 link = _dbus_list_get_first_link (&context->servers);
1126 while (link != NULL)
1128 dbus_server_unref (link->data);
1130 link = _dbus_list_get_next_link (&context->servers, link);
1132 _dbus_list_clear (&context->servers);
1134 if (context->policy)
1136 bus_policy_unref (context->policy);
1137 context->policy = NULL;
1142 _dbus_loop_unref (context->loop);
1143 context->loop = NULL;
1146 if (context->matchmaker)
1148 bus_matchmaker_unref (context->matchmaker);
1149 context->matchmaker = NULL;
1152 dbus_free (context->config_file);
1153 dbus_free (context->log_prefix);
1154 dbus_free (context->type);
1155 dbus_free (context->address);
1156 dbus_free (context->user);
1157 dbus_free (context->servicehelper);
1159 if (context->pidfile)
1162 _dbus_string_init_const (&u, context->pidfile);
1164 /* Deliberately ignore errors here, since there's not much
1165 * we can do about it, and we're exiting anyways.
1167 _dbus_delete_file (&u, NULL);
1169 dbus_free (context->pidfile);
1172 if (context->initial_fd_limit)
1173 _dbus_rlimit_free (context->initial_fd_limit);
1175 dbus_free (context);
1177 dbus_server_free_data_slot (&server_data_slot);
1181 /* type may be NULL */
1183 bus_context_get_type (BusContext *context)
1185 return context->type;
1189 bus_context_get_address (BusContext *context)
1191 return context->address;
1195 bus_context_get_servicehelper (BusContext *context)
1197 return context->servicehelper;
1201 bus_context_get_systemd_activation (BusContext *context)
1203 return context->systemd_activation;
1207 bus_context_get_registry (BusContext *context)
1209 return context->registry;
1213 bus_context_get_connections (BusContext *context)
1215 return context->connections;
1219 bus_context_get_activation (BusContext *context)
1221 return context->activation;
1225 bus_context_get_matchmaker (BusContext *context)
1227 return context->matchmaker;
1231 bus_context_get_loop (BusContext *context)
1233 return context->loop;
1237 bus_context_allow_unix_user (BusContext *context,
1240 return bus_policy_allow_unix_user (context->policy,
1244 /* For now this is never actually called because the default
1245 * DBusConnection behavior of 'same user that owns the bus can connect'
1246 * is all it would do.
1249 bus_context_allow_windows_user (BusContext *context,
1250 const char *windows_sid)
1252 return bus_policy_allow_windows_user (context->policy,
1257 bus_context_get_policy (BusContext *context)
1259 return context->policy;
1263 bus_context_create_client_policy (BusContext *context,
1264 DBusConnection *connection,
1267 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1268 return bus_policy_create_client_policy (context->policy, connection,
1273 bus_context_get_activation_timeout (BusContext *context)
1276 return context->limits.activation_timeout;
1280 bus_context_get_auth_timeout (BusContext *context)
1282 return context->limits.auth_timeout;
1286 bus_context_get_pending_fd_timeout (BusContext *context)
1288 return context->limits.pending_fd_timeout;
1292 bus_context_get_max_completed_connections (BusContext *context)
1294 return context->limits.max_completed_connections;
1298 bus_context_get_max_incomplete_connections (BusContext *context)
1300 return context->limits.max_incomplete_connections;
1304 bus_context_get_max_connections_per_user (BusContext *context)
1306 return context->limits.max_connections_per_user;
1310 bus_context_get_max_pending_activations (BusContext *context)
1312 return context->limits.max_pending_activations;
1316 bus_context_get_max_services_per_connection (BusContext *context)
1318 return context->limits.max_services_per_connection;
1322 bus_context_get_max_match_rules_per_connection (BusContext *context)
1324 return context->limits.max_match_rules_per_connection;
1328 bus_context_get_max_replies_per_connection (BusContext *context)
1330 return context->limits.max_replies_per_connection;
1334 bus_context_get_reply_timeout (BusContext *context)
1336 return context->limits.reply_timeout;
1340 bus_context_get_initial_fd_limit (BusContext *context)
1342 return context->initial_fd_limit;
1346 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1350 if (!context->syslog)
1352 /* we're not syslogging; just output to stderr */
1353 va_start (args, msg);
1354 vfprintf (stderr, msg, args);
1355 fprintf (stderr, "\n");
1358 if (severity == DBUS_SYSTEM_LOG_FATAL)
1364 va_start (args, msg);
1366 if (context->log_prefix)
1368 DBusString full_msg;
1370 if (!_dbus_string_init (&full_msg))
1372 if (!_dbus_string_append (&full_msg, context->log_prefix))
1374 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1377 _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1379 _dbus_string_free (&full_msg);
1382 _dbus_system_logv (severity, msg, args);
1388 static inline const char *
1389 nonnull (const char *maybe_null,
1390 const char *if_null)
1392 return (maybe_null ? maybe_null : if_null);
1396 bus_context_log_literal (BusContext *context,
1397 DBusSystemLogSeverity severity,
1400 if (!context->syslog)
1402 fputs (msg, stderr);
1403 fputc ('\n', stderr);
1405 if (severity == DBUS_SYSTEM_LOG_FATAL)
1410 _dbus_system_log (severity, "%s%s", nonnull (context->log_prefix, ""),
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 proposed_recipient or sender definitely means the bus driver.
1512 * NULL for addressed_recipient may mean the bus driver, or may mean
1513 * no destination was specified in the message (e.g. a signal).
1516 bus_context_check_security_policy (BusContext *context,
1517 BusTransaction *transaction,
1518 DBusConnection *sender,
1519 DBusConnection *addressed_recipient,
1520 DBusConnection *proposed_recipient,
1521 DBusMessage *message,
1524 const char *src, *dest;
1525 BusClientPolicy *sender_policy;
1526 BusClientPolicy *recipient_policy;
1527 dbus_int32_t toggles;
1530 dbus_bool_t requested_reply;
1532 type = dbus_message_get_type (message);
1533 src = dbus_message_get_sender (message);
1534 dest = dbus_message_get_destination (message);
1536 /* dispatch.c was supposed to ensure these invariants */
1537 _dbus_assert (dest != NULL ||
1538 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1539 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1540 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1541 addressed_recipient != NULL ||
1542 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1546 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1547 case DBUS_MESSAGE_TYPE_SIGNAL:
1548 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1549 case DBUS_MESSAGE_TYPE_ERROR:
1553 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1556 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1557 "Message bus will not accept messages of unknown type\n");
1562 requested_reply = FALSE;
1566 if (bus_connection_is_active (sender))
1568 sender_policy = bus_connection_get_policy (sender);
1569 _dbus_assert (sender_policy != NULL);
1571 /* Fill in requested_reply variable with TRUE if this is a
1572 * reply and the reply was pending.
1574 if (dbus_message_get_reply_serial (message) != 0)
1576 if (proposed_recipient != NULL /* not to the bus driver */ &&
1577 addressed_recipient == proposed_recipient /* not eavesdropping */)
1581 dbus_error_init (&error2);
1582 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1584 sender, addressed_recipient, message,
1586 if (dbus_error_is_set (&error2))
1588 dbus_move_error (&error2, error);
1596 sender_policy = NULL;
1599 /* First verify the SELinux access controls. If allowed then
1600 * go on with the standard checks.
1602 if (!bus_selinux_allows_send (sender, proposed_recipient,
1603 dbus_message_type_to_string (dbus_message_get_type (message)),
1604 dbus_message_get_interface (message),
1605 dbus_message_get_member (message),
1606 dbus_message_get_error_name (message),
1607 dest ? dest : DBUS_SERVICE_DBUS, error))
1609 if (error != NULL && !dbus_error_is_set (error))
1611 /* don't syslog this, just set the error: avc_has_perm should
1612 * have already written to either the audit log or syslog */
1613 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1614 "An SELinux policy prevents this sender from sending this "
1615 "message to this recipient",
1616 0, message, sender, proposed_recipient, FALSE, FALSE, error);
1617 _dbus_verbose ("SELinux security check denying send to service\n");
1623 /* next verify AppArmor access controls. If allowed then
1624 * go on with the standard checks.
1626 if (!bus_apparmor_allows_send (sender, proposed_recipient,
1628 bus_context_get_type (context),
1629 dbus_message_get_type (message),
1630 dbus_message_get_path (message),
1631 dbus_message_get_interface (message),
1632 dbus_message_get_member (message),
1633 dbus_message_get_error_name (message),
1634 dest ? dest : DBUS_SERVICE_DBUS,
1635 src ? src : DBUS_SERVICE_DBUS,
1639 if (!bus_connection_is_active (sender))
1641 /* Policy for inactive connections is that they can only send
1642 * the hello message to the bus driver
1644 if (proposed_recipient == NULL &&
1645 dbus_message_is_method_call (message,
1646 DBUS_INTERFACE_DBUS,
1649 _dbus_verbose ("security check allowing %s message\n",
1655 _dbus_verbose ("security check disallowing non-%s message\n",
1658 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1659 "Client tried to send a message other than %s without being registered",
1668 sender_policy = NULL;
1670 /* If the sender is the bus driver, we assume any reply was a
1671 * requested reply as bus driver won't send bogus ones
1673 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1674 dbus_message_get_reply_serial (message) != 0)
1675 requested_reply = TRUE;
1678 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1679 (sender == NULL && sender_policy == NULL));
1681 if (proposed_recipient != NULL)
1683 /* only the bus driver can send to an inactive recipient (as it
1684 * owns no services, so other apps can't address it). Inactive
1685 * recipients can receive any message.
1687 if (bus_connection_is_active (proposed_recipient))
1689 recipient_policy = bus_connection_get_policy (proposed_recipient);
1690 _dbus_assert (recipient_policy != NULL);
1692 else if (sender == NULL)
1694 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1695 recipient_policy = NULL;
1699 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1700 recipient_policy = NULL;
1704 recipient_policy = NULL;
1706 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1707 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1708 (proposed_recipient == NULL && recipient_policy == NULL));
1711 if (sender_policy &&
1712 !bus_client_policy_check_can_send (sender_policy,
1716 message, &toggles, &log))
1718 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1719 "Rejected send message", toggles,
1720 message, sender, proposed_recipient, requested_reply,
1721 (addressed_recipient == proposed_recipient), error);
1722 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1728 /* We want to drop this message, and are only not doing so for backwards
1730 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1731 "Would reject message", toggles,
1732 message, sender, proposed_recipient, requested_reply,
1736 if (recipient_policy &&
1737 !bus_client_policy_check_can_receive (recipient_policy,
1741 addressed_recipient, proposed_recipient,
1744 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1745 "Rejected receive message", toggles,
1746 message, sender, proposed_recipient, requested_reply,
1747 (addressed_recipient == proposed_recipient), error);
1748 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1752 /* See if limits on size have been exceeded */
1753 if (proposed_recipient &&
1754 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1755 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1757 complain_about_message (context, DBUS_ERROR_LIMITS_EXCEEDED,
1758 "Rejected: destination has a full message queue",
1759 0, message, sender, proposed_recipient, requested_reply, TRUE,
1761 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1765 /* Record that we will allow a reply here in the future (don't
1766 * bother if the recipient is the bus or this is an eavesdropping
1767 * connection). Only the addressed recipient may reply.
1769 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1771 addressed_recipient &&
1772 addressed_recipient == proposed_recipient && /* not eavesdropping */
1773 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1775 sender, addressed_recipient,
1778 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1782 _dbus_verbose ("security policy allowing message\n");
1787 bus_context_check_all_watches (BusContext *context)
1790 dbus_bool_t enabled = TRUE;
1792 if (bus_connections_get_n_incomplete (context->connections) >=
1793 bus_context_get_max_incomplete_connections (context))
1798 if (context->watches_enabled == enabled)
1801 context->watches_enabled = enabled;
1803 for (link = _dbus_list_get_first_link (&context->servers);
1805 link = _dbus_list_get_next_link (&context->servers, link))
1807 /* A BusContext might contains several DBusServer (if there are
1808 * several <listen> configuration items) and a DBusServer might
1809 * contain several DBusWatch in its DBusWatchList (if getaddrinfo
1810 * returns several addresses on a dual IPv4-IPv6 stack or if
1811 * systemd passes several fds).
1812 * We want to enable/disable them all.
1814 DBusServer *server = link->data;
1815 _dbus_server_toggle_all_watches (server, enabled);