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>
60 BusConnections *connections;
61 BusActivation *activation;
62 BusRegistry *registry;
64 BusMatchmaker *matchmaker;
66 unsigned int fork : 1;
67 unsigned int syslog : 1;
68 unsigned int keep_umask : 1;
69 unsigned int allow_anonymous : 1;
70 unsigned int systemd_activation : 1;
73 static dbus_int32_t server_data_slot = -1;
80 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
83 server_get_context (DBusServer *server)
88 if (!dbus_server_allocate_data_slot (&server_data_slot))
91 bd = BUS_SERVER_DATA (server);
94 dbus_server_free_data_slot (&server_data_slot);
98 context = bd->context;
100 dbus_server_free_data_slot (&server_data_slot);
106 add_server_watch (DBusWatch *watch,
109 DBusServer *server = data;
112 context = server_get_context (server);
114 return _dbus_loop_add_watch (context->loop, watch);
118 remove_server_watch (DBusWatch *watch,
121 DBusServer *server = data;
124 context = server_get_context (server);
126 _dbus_loop_remove_watch (context->loop, watch);
130 toggle_server_watch (DBusWatch *watch,
133 DBusServer *server = data;
136 context = server_get_context (server);
138 _dbus_loop_toggle_watch (context->loop, watch);
142 add_server_timeout (DBusTimeout *timeout,
145 DBusServer *server = data;
148 context = server_get_context (server);
150 return _dbus_loop_add_timeout (context->loop, timeout);
154 remove_server_timeout (DBusTimeout *timeout,
157 DBusServer *server = data;
160 context = server_get_context (server);
162 _dbus_loop_remove_timeout (context->loop, timeout);
166 new_connection_callback (DBusServer *server,
167 DBusConnection *new_connection,
170 BusContext *context = data;
172 if (!bus_connections_setup_connection (context->connections, new_connection))
174 _dbus_verbose ("No memory to setup new connection\n");
176 /* if we don't do this, it will get unref'd without
177 * being disconnected... kind of strange really
178 * that we have to do this, people won't get it right
181 dbus_connection_close (new_connection);
184 dbus_connection_set_max_received_size (new_connection,
185 context->limits.max_incoming_bytes);
187 dbus_connection_set_max_message_size (new_connection,
188 context->limits.max_message_size);
190 dbus_connection_set_max_received_unix_fds (new_connection,
191 context->limits.max_incoming_unix_fds);
193 dbus_connection_set_max_message_unix_fds (new_connection,
194 context->limits.max_message_unix_fds);
196 dbus_connection_set_allow_anonymous (new_connection,
197 context->allow_anonymous);
199 /* on OOM, we won't have ref'd the connection so it will die. */
203 free_server_data (void *data)
205 BusServerData *bd = data;
211 setup_server (BusContext *context,
213 char **auth_mechanisms,
218 bd = dbus_new0 (BusServerData, 1);
219 if (bd == NULL || !dbus_server_set_data (server,
221 bd, free_server_data))
228 bd->context = context;
230 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
236 dbus_server_set_new_connection_function (server,
237 new_connection_callback,
240 if (!dbus_server_set_watch_functions (server,
251 if (!dbus_server_set_timeout_functions (server,
253 remove_server_timeout,
264 /* This code only gets executed the first time the
265 * config files are parsed. It is not executed
266 * when config files are reloaded.
269 process_config_first_time_only (BusContext *context,
270 BusConfigParser *parser,
271 const DBusString *address,
272 dbus_bool_t systemd_activation,
273 dbus_bool_t write_pidfile,
276 DBusString log_prefix;
278 DBusList **addresses;
279 const char *user, *pidfile;
280 char **auth_mechanisms;
281 DBusList **auth_mechanisms_list;
285 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
288 auth_mechanisms = NULL;
291 _dbus_init_system_log ();
293 context->systemd_activation = systemd_activation;
295 /* Check for an existing pid file. Of course this is a race;
296 * we'd have to use fcntl() locks on the pid file to
297 * avoid that. But we want to check for the pid file
298 * before overwriting any existing sockets, etc.
302 pidfile = bus_config_parser_get_pidfile (parser);
309 _dbus_string_init_const (&u, pidfile);
311 if (_dbus_stat (&u, &stbuf, NULL))
317 _dbus_string_init (&p);
318 _dbus_file_get_contents(&p, &u, NULL);
319 _dbus_string_parse_int(&p, 0, &pid, NULL);
320 _dbus_string_free(&p);
322 if ((kill((int)pid, 0))) {
323 dbus_set_error(NULL, DBUS_ERROR_FILE_EXISTS,
324 "pid %ld not running, removing stale pid file\n",
326 _dbus_delete_file(&u, NULL);
329 dbus_set_error (error, DBUS_ERROR_FAILED,
330 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
339 /* keep around the pid filename so we can delete it later */
340 context->pidfile = _dbus_strdup (pidfile);
342 /* note that type may be NULL */
343 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
344 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
347 user = bus_config_parser_get_user (parser);
350 context->user = _dbus_strdup (user);
351 if (context->user == NULL)
355 /* Set up the prefix for syslog messages */
356 if (!_dbus_string_init (&log_prefix))
358 if (context->type && !strcmp (context->type, "system"))
360 if (!_dbus_string_append (&log_prefix, "[system] "))
363 else if (context->type && !strcmp (context->type, "session"))
365 DBusCredentials *credentials;
367 credentials = _dbus_credentials_new_from_current_process ();
370 if (!_dbus_string_append (&log_prefix, "[session "))
372 _dbus_credentials_unref (credentials);
375 if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
377 _dbus_credentials_unref (credentials);
380 if (!_dbus_string_append (&log_prefix, "] "))
382 _dbus_credentials_unref (credentials);
385 _dbus_credentials_unref (credentials);
387 if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
389 _dbus_string_free (&log_prefix);
391 /* Build an array of auth mechanisms */
393 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
394 len = _dbus_list_get_length (auth_mechanisms_list);
400 auth_mechanisms = dbus_new0 (char*, len + 1);
401 if (auth_mechanisms == NULL)
405 link = _dbus_list_get_first_link (auth_mechanisms_list);
408 auth_mechanisms[i] = _dbus_strdup (link->data);
409 if (auth_mechanisms[i] == NULL)
411 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
417 auth_mechanisms = NULL;
420 /* Listen on our addresses */
426 server = dbus_server_listen (_dbus_string_get_const_data(address), error);
429 _DBUS_ASSERT_ERROR_IS_SET (error);
432 else if (!setup_server (context, server, auth_mechanisms, error))
434 _DBUS_ASSERT_ERROR_IS_SET (error);
438 if (!_dbus_list_append (&context->servers, server))
443 addresses = bus_config_parser_get_addresses (parser);
445 link = _dbus_list_get_first_link (addresses);
450 server = dbus_server_listen (link->data, error);
453 _DBUS_ASSERT_ERROR_IS_SET (error);
456 else if (!setup_server (context, server, auth_mechanisms, error))
458 _DBUS_ASSERT_ERROR_IS_SET (error);
462 if (!_dbus_list_append (&context->servers, server))
465 link = _dbus_list_get_next_link (addresses, link);
469 context->fork = bus_config_parser_get_fork (parser);
470 context->syslog = bus_config_parser_get_syslog (parser);
471 context->keep_umask = bus_config_parser_get_keep_umask (parser);
472 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
474 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
478 dbus_free_string_array (auth_mechanisms);
483 dbus_free_string_array (auth_mechanisms);
487 /* This code gets executed every time the config files
488 * are parsed: both during BusContext construction
489 * and on reloads. This function is slightly screwy
490 * since it can do a "half reload" in out-of-memory
491 * situations. Realistically, unlikely to ever matter.
494 process_config_every_time (BusContext *context,
495 BusConfigParser *parser,
496 dbus_bool_t is_reload,
499 DBusString full_address;
503 const char *servicehelper;
508 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
513 if (!_dbus_string_init (&full_address))
519 /* get our limits and timeout lengths */
520 bus_config_parser_get_limits (parser, &context->limits);
523 bus_policy_unref (context->policy);
524 context->policy = bus_config_parser_steal_policy (parser);
525 _dbus_assert (context->policy != NULL);
527 /* We have to build the address backward, so that
528 * <listen> later in the config file have priority
530 link = _dbus_list_get_last_link (&context->servers);
533 addr = dbus_server_get_address (link->data);
540 if (_dbus_string_get_length (&full_address) > 0)
542 if (!_dbus_string_append (&full_address, ";"))
549 if (!_dbus_string_append (&full_address, addr))
558 link = _dbus_list_get_prev_link (&context->servers, link);
562 dbus_free (context->address);
564 if (!_dbus_string_copy_data (&full_address, &context->address))
570 /* get the service directories */
571 dirs = bus_config_parser_get_service_dirs (parser);
573 /* and the service helper */
574 servicehelper = bus_config_parser_get_servicehelper (parser);
576 s = _dbus_strdup(servicehelper);
577 if (s == NULL && servicehelper != NULL)
584 dbus_free(context->servicehelper);
585 context->servicehelper = s;
588 /* Create activation subsystem */
589 if (context->activation)
591 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
596 context->activation = bus_activation_new (context, &full_address, dirs, error);
599 if (context->activation == NULL)
601 _DBUS_ASSERT_ERROR_IS_SET (error);
605 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
609 _dbus_string_free (&full_address);
618 list_concat_new (DBusList **a,
626 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
628 if (!_dbus_list_append (result, link->data))
631 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
633 if (!_dbus_list_append (result, link->data))
639 _dbus_list_clear (result);
644 raise_file_descriptor_limit (BusContext *context)
647 /* I just picked this out of thin air; we need some extra
648 * descriptors for things like any internal pipes we create,
649 * inotify, connections to SELinux, etc.
651 unsigned int arbitrary_extra_fds = 32;
654 limit = context->limits.max_completed_connections +
655 context->limits.max_incomplete_connections
656 + arbitrary_extra_fds;
658 _dbus_request_file_descriptor_limit (limit);
662 process_config_postinit (BusContext *context,
663 BusConfigParser *parser,
666 DBusHashTable *service_context_table;
667 DBusList *watched_dirs = NULL;
669 raise_file_descriptor_limit (context);
671 service_context_table = bus_config_parser_steal_service_context_table (parser);
672 if (!bus_registry_set_service_context_table (context->registry,
673 service_context_table))
679 _dbus_hash_table_unref (service_context_table);
681 /* We need to monitor both the configuration directories and directories
682 * containing .service files.
684 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
685 bus_config_parser_get_service_dirs (parser),
692 bus_set_watched_dirs (context, &watched_dirs);
694 _dbus_list_clear (&watched_dirs);
700 bus_context_new (const DBusString *config_file,
701 ForceForkSetting force_fork,
702 DBusPipe *print_addr_pipe,
703 DBusPipe *print_pid_pipe,
704 const DBusString *address,
705 dbus_bool_t systemd_activation,
706 dbus_bool_t write_pidfile,
710 BusConfigParser *parser;
712 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
717 if (!dbus_server_allocate_data_slot (&server_data_slot))
723 context = dbus_new0 (BusContext, 1);
729 context->refcount = 1;
731 _dbus_generate_uuid (&context->uuid);
733 if (!_dbus_string_copy_data (config_file, &context->config_file))
739 context->loop = _dbus_loop_new ();
740 if (context->loop == NULL)
746 context->registry = bus_registry_new (context);
747 if (context->registry == NULL)
753 parser = bus_config_load (config_file, TRUE, NULL, error);
756 _DBUS_ASSERT_ERROR_IS_SET (error);
760 if (!process_config_first_time_only (context, parser, address, systemd_activation, write_pidfile, error))
762 _DBUS_ASSERT_ERROR_IS_SET (error);
765 if (!process_config_every_time (context, parser, FALSE, error))
767 _DBUS_ASSERT_ERROR_IS_SET (error);
771 /* we need another ref of the server data slot for the context
774 if (!dbus_server_allocate_data_slot (&server_data_slot))
775 _dbus_assert_not_reached ("second ref of server data slot failed");
777 /* Note that we don't know whether the print_addr_pipe is
778 * one of the sockets we're using to listen on, or some
779 * other random thing. But I think the answer is "don't do
782 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
785 const char *a = bus_context_get_address (context);
788 _dbus_assert (a != NULL);
789 if (!_dbus_string_init (&addr))
795 if (!_dbus_string_append (&addr, a) ||
796 !_dbus_string_append (&addr, "\n"))
798 _dbus_string_free (&addr);
803 bytes = _dbus_string_get_length (&addr);
804 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
806 /* pipe write returns an error on failure but not short write */
807 if (error != NULL && !dbus_error_is_set (error))
809 dbus_set_error (error, DBUS_ERROR_FAILED,
810 "Printing message bus address: did not write all bytes\n");
812 _dbus_string_free (&addr);
816 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
817 _dbus_pipe_close (print_addr_pipe, NULL);
819 _dbus_string_free (&addr);
822 context->connections = bus_connections_new (context);
823 if (context->connections == NULL)
829 context->matchmaker = bus_matchmaker_new ();
830 if (context->matchmaker == NULL)
836 /* check user before we fork */
837 if (context->user != NULL)
839 if (!_dbus_verify_daemon_user (context->user))
841 dbus_set_error (error, DBUS_ERROR_FAILED,
842 "Could not get UID and GID for username \"%s\"",
848 /* Now become a daemon if appropriate and write out pid file in any case */
852 if (context->pidfile)
853 _dbus_string_init_const (&u, context->pidfile);
855 if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
857 _dbus_verbose ("Forking and becoming daemon\n");
859 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
862 context->keep_umask))
864 _DBUS_ASSERT_ERROR_IS_SET (error);
870 _dbus_verbose ("Fork not requested\n");
872 /* Need to write PID file and to PID pipe for ourselves,
873 * not for the child process. This is a no-op if the pidfile
874 * is NULL and print_pid_pipe is NULL.
876 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
881 _DBUS_ASSERT_ERROR_IS_SET (error);
887 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
888 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
889 _dbus_pipe_close (print_pid_pipe, NULL);
891 if (!bus_selinux_full_init ())
893 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but AVC initialization failed; check system log\n");
896 if (!process_config_postinit (context, parser, error))
898 _DBUS_ASSERT_ERROR_IS_SET (error);
904 bus_config_parser_unref (parser);
908 /* Here we change our credentials if required,
909 * as soon as we've set up our sockets and pidfile
911 if (context->user != NULL)
913 if (!_dbus_change_to_daemon_user (context->user, error))
915 _DBUS_ASSERT_ERROR_IS_SET (error);
920 /* FIXME - why not just put this in full_init() below? */
921 bus_selinux_audit_init ();
925 dbus_server_free_data_slot (&server_data_slot);
931 bus_config_parser_unref (parser);
933 bus_context_unref (context);
935 if (server_data_slot >= 0)
936 dbus_server_free_data_slot (&server_data_slot);
942 bus_context_get_id (BusContext *context,
945 return _dbus_uuid_encode (&context->uuid, uuid);
949 bus_context_reload_config (BusContext *context,
952 BusConfigParser *parser;
953 DBusString config_file;
956 /* Flush the user database cache */
957 _dbus_flush_caches ();
960 _dbus_string_init_const (&config_file, context->config_file);
961 parser = bus_config_load (&config_file, TRUE, NULL, error);
964 _DBUS_ASSERT_ERROR_IS_SET (error);
968 if (!process_config_every_time (context, parser, TRUE, error))
970 _DBUS_ASSERT_ERROR_IS_SET (error);
973 if (!process_config_postinit (context, parser, error))
975 _DBUS_ASSERT_ERROR_IS_SET (error);
980 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
983 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
985 bus_config_parser_unref (parser);
990 shutdown_server (BusContext *context,
993 if (server == NULL ||
994 !dbus_server_get_is_connected (server))
997 if (!dbus_server_set_watch_functions (server,
1001 _dbus_assert_not_reached ("setting watch functions to NULL failed");
1003 if (!dbus_server_set_timeout_functions (server,
1007 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
1009 dbus_server_disconnect (server);
1013 bus_context_shutdown (BusContext *context)
1017 link = _dbus_list_get_first_link (&context->servers);
1018 while (link != NULL)
1020 shutdown_server (context, link->data);
1022 link = _dbus_list_get_next_link (&context->servers, link);
1027 bus_context_ref (BusContext *context)
1029 _dbus_assert (context->refcount > 0);
1030 context->refcount += 1;
1036 bus_context_unref (BusContext *context)
1038 _dbus_assert (context->refcount > 0);
1039 context->refcount -= 1;
1041 if (context->refcount == 0)
1045 _dbus_verbose ("Finalizing bus context %p\n", context);
1047 bus_context_shutdown (context);
1049 if (context->connections)
1051 bus_connections_unref (context->connections);
1052 context->connections = NULL;
1055 if (context->registry)
1057 bus_registry_unref (context->registry);
1058 context->registry = NULL;
1061 if (context->activation)
1063 bus_activation_unref (context->activation);
1064 context->activation = NULL;
1067 link = _dbus_list_get_first_link (&context->servers);
1068 while (link != NULL)
1070 dbus_server_unref (link->data);
1072 link = _dbus_list_get_next_link (&context->servers, link);
1074 _dbus_list_clear (&context->servers);
1076 if (context->policy)
1078 bus_policy_unref (context->policy);
1079 context->policy = NULL;
1084 _dbus_loop_unref (context->loop);
1085 context->loop = NULL;
1088 if (context->matchmaker)
1090 bus_matchmaker_unref (context->matchmaker);
1091 context->matchmaker = NULL;
1094 dbus_free (context->config_file);
1095 dbus_free (context->log_prefix);
1096 dbus_free (context->type);
1097 dbus_free (context->address);
1098 dbus_free (context->user);
1099 dbus_free (context->servicehelper);
1101 if (context->pidfile)
1104 _dbus_string_init_const (&u, context->pidfile);
1106 /* Deliberately ignore errors here, since there's not much
1107 * we can do about it, and we're exiting anyways.
1109 _dbus_delete_file (&u, NULL);
1111 dbus_free (context->pidfile);
1113 dbus_free (context);
1115 dbus_server_free_data_slot (&server_data_slot);
1119 /* type may be NULL */
1121 bus_context_get_type (BusContext *context)
1123 return context->type;
1127 bus_context_get_address (BusContext *context)
1129 return context->address;
1133 bus_context_get_servicehelper (BusContext *context)
1135 return context->servicehelper;
1139 bus_context_get_systemd_activation (BusContext *context)
1141 return context->systemd_activation;
1145 bus_context_get_registry (BusContext *context)
1147 return context->registry;
1151 bus_context_get_connections (BusContext *context)
1153 return context->connections;
1157 bus_context_get_activation (BusContext *context)
1159 return context->activation;
1163 bus_context_get_matchmaker (BusContext *context)
1165 return context->matchmaker;
1169 bus_context_get_loop (BusContext *context)
1171 return context->loop;
1175 bus_context_allow_unix_user (BusContext *context,
1178 return bus_policy_allow_unix_user (context->policy,
1182 /* For now this is never actually called because the default
1183 * DBusConnection behavior of 'same user that owns the bus can connect'
1184 * is all it would do.
1187 bus_context_allow_windows_user (BusContext *context,
1188 const char *windows_sid)
1190 return bus_policy_allow_windows_user (context->policy,
1195 bus_context_get_policy (BusContext *context)
1197 return context->policy;
1201 bus_context_create_client_policy (BusContext *context,
1202 DBusConnection *connection,
1205 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1206 return bus_policy_create_client_policy (context->policy, connection,
1211 bus_context_get_activation_timeout (BusContext *context)
1214 return context->limits.activation_timeout;
1218 bus_context_get_auth_timeout (BusContext *context)
1220 return context->limits.auth_timeout;
1224 bus_context_get_max_completed_connections (BusContext *context)
1226 return context->limits.max_completed_connections;
1230 bus_context_get_max_incomplete_connections (BusContext *context)
1232 return context->limits.max_incomplete_connections;
1236 bus_context_get_max_connections_per_user (BusContext *context)
1238 return context->limits.max_connections_per_user;
1242 bus_context_get_max_pending_activations (BusContext *context)
1244 return context->limits.max_pending_activations;
1248 bus_context_get_max_services_per_connection (BusContext *context)
1250 return context->limits.max_services_per_connection;
1254 bus_context_get_max_match_rules_per_connection (BusContext *context)
1256 return context->limits.max_match_rules_per_connection;
1260 bus_context_get_max_replies_per_connection (BusContext *context)
1262 return context->limits.max_replies_per_connection;
1266 bus_context_get_reply_timeout (BusContext *context)
1268 return context->limits.reply_timeout;
1272 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
1275 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1279 if (!context->syslog)
1281 /* we're not syslogging; just output to stderr */
1282 va_start (args, msg);
1283 vfprintf (stderr, msg, args);
1284 fprintf (stderr, "\n");
1289 va_start (args, msg);
1291 if (context->log_prefix)
1293 DBusString full_msg;
1295 if (!_dbus_string_init (&full_msg))
1297 if (!_dbus_string_append (&full_msg, context->log_prefix))
1299 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1302 _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1304 _dbus_string_free (&full_msg);
1307 _dbus_system_logv (severity, msg, args);
1313 static inline const char *
1314 nonnull (const char *maybe_null,
1315 const char *if_null)
1317 return (maybe_null ? maybe_null : if_null);
1321 * Log something about a message, usually that it was rejected.
1324 complain_about_message (BusContext *context,
1325 const char *error_name,
1326 const char *complaint,
1328 DBusMessage *message,
1329 DBusConnection *sender,
1330 DBusConnection *proposed_recipient,
1331 dbus_bool_t requested_reply,
1335 DBusError stack_error = DBUS_ERROR_INIT;
1336 const char *sender_name;
1337 const char *sender_loginfo;
1338 const char *proposed_recipient_loginfo;
1340 if (error == NULL && !log)
1345 sender_name = bus_connection_get_name (sender);
1346 sender_loginfo = bus_connection_get_loginfo (sender);
1350 sender_name = "(unset)";
1351 sender_loginfo = "(bus)";
1354 if (proposed_recipient != NULL)
1355 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1357 proposed_recipient_loginfo = "bus";
1359 dbus_set_error (&stack_error, error_name,
1360 "%s, %d matched rules; type=\"%s\", sender=\"%s\" (%s) "
1361 "interface=\"%s\" member=\"%s\" error name=\"%s\" "
1362 "requested_reply=\"%d\" destination=\"%s\" (%s)",
1365 dbus_message_type_to_string (dbus_message_get_type (message)),
1368 nonnull (dbus_message_get_interface (message), "(unset)"),
1369 nonnull (dbus_message_get_member (message), "(unset)"),
1370 nonnull (dbus_message_get_error_name (message), "(unset)"),
1372 nonnull (dbus_message_get_destination (message), DBUS_SERVICE_DBUS),
1373 proposed_recipient_loginfo);
1375 /* If we hit OOM while setting the error, this will syslog "out of memory"
1376 * which is itself an indication that something is seriously wrong */
1378 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, "%s",
1379 stack_error.message);
1381 dbus_move_error (&stack_error, error);
1385 * addressed_recipient is the recipient specified in the message.
1387 * proposed_recipient is the recipient we're considering sending
1388 * to right this second, and may be an eavesdropper.
1390 * sender is the sender of the message.
1392 * NULL for proposed_recipient or sender definitely means the bus driver.
1394 * NULL for addressed_recipient may mean the bus driver, or may mean
1395 * no destination was specified in the message (e.g. a signal).
1398 bus_context_check_security_policy (BusContext *context,
1399 BusTransaction *transaction,
1400 DBusConnection *sender,
1401 DBusConnection *addressed_recipient,
1402 DBusConnection *proposed_recipient,
1403 DBusMessage *message,
1407 BusClientPolicy *sender_policy;
1408 BusClientPolicy *recipient_policy;
1409 dbus_int32_t toggles;
1412 dbus_bool_t requested_reply;
1414 type = dbus_message_get_type (message);
1415 dest = dbus_message_get_destination (message);
1417 /* dispatch.c was supposed to ensure these invariants */
1418 _dbus_assert (dest != NULL ||
1419 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1420 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1421 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1422 addressed_recipient != NULL ||
1423 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1427 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1428 case DBUS_MESSAGE_TYPE_SIGNAL:
1429 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1430 case DBUS_MESSAGE_TYPE_ERROR:
1434 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1437 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1438 "Message bus will not accept messages of unknown type\n");
1443 requested_reply = FALSE;
1447 /* First verify the SELinux access controls. If allowed then
1448 * go on with the standard checks.
1450 if (!bus_selinux_allows_send (sender, proposed_recipient,
1451 dbus_message_type_to_string (dbus_message_get_type (message)),
1452 dbus_message_get_interface (message),
1453 dbus_message_get_member (message),
1454 dbus_message_get_error_name (message),
1455 dest ? dest : DBUS_SERVICE_DBUS, error))
1457 if (error != NULL && !dbus_error_is_set (error))
1459 /* don't syslog this, just set the error: avc_has_perm should
1460 * have already written to either the audit log or syslog */
1461 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1462 "An SELinux policy prevents this sender from sending this "
1463 "message to this recipient",
1464 0, message, sender, proposed_recipient, FALSE, FALSE, error);
1465 _dbus_verbose ("SELinux security check denying send to service\n");
1471 if (bus_connection_is_active (sender))
1473 sender_policy = bus_connection_get_policy (sender);
1474 _dbus_assert (sender_policy != NULL);
1476 /* Fill in requested_reply variable with TRUE if this is a
1477 * reply and the reply was pending.
1479 if (dbus_message_get_reply_serial (message) != 0)
1481 if (proposed_recipient != NULL /* not to the bus driver */ &&
1482 addressed_recipient == proposed_recipient /* not eavesdropping */)
1486 dbus_error_init (&error2);
1487 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1489 sender, addressed_recipient, message,
1491 if (dbus_error_is_set (&error2))
1493 dbus_move_error (&error2, error);
1501 /* Policy for inactive connections is that they can only send
1502 * the hello message to the bus driver
1504 if (proposed_recipient == NULL &&
1505 dbus_message_is_method_call (message,
1506 DBUS_INTERFACE_DBUS,
1509 _dbus_verbose ("security check allowing %s message\n",
1515 _dbus_verbose ("security check disallowing non-%s message\n",
1518 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1519 "Client tried to send a message other than %s without being registered",
1528 sender_policy = NULL;
1530 /* If the sender is the bus driver, we assume any reply was a
1531 * requested reply as bus driver won't send bogus ones
1533 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1534 dbus_message_get_reply_serial (message) != 0)
1535 requested_reply = TRUE;
1538 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1539 (sender == NULL && sender_policy == NULL));
1541 if (proposed_recipient != NULL)
1543 /* only the bus driver can send to an inactive recipient (as it
1544 * owns no services, so other apps can't address it). Inactive
1545 * recipients can receive any message.
1547 if (bus_connection_is_active (proposed_recipient))
1549 recipient_policy = bus_connection_get_policy (proposed_recipient);
1550 _dbus_assert (recipient_policy != NULL);
1552 else if (sender == NULL)
1554 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1555 recipient_policy = NULL;
1559 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1560 recipient_policy = NULL;
1564 recipient_policy = NULL;
1566 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1567 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1568 (proposed_recipient == NULL && recipient_policy == NULL));
1571 if (sender_policy &&
1572 !bus_client_policy_check_can_send (sender_policy,
1576 message, &toggles, &log))
1578 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1579 "Rejected send message", toggles,
1580 message, sender, proposed_recipient, requested_reply,
1581 (addressed_recipient == proposed_recipient), error);
1582 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1588 /* We want to drop this message, and are only not doing so for backwards
1590 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1591 "Would reject message", toggles,
1592 message, sender, proposed_recipient, requested_reply,
1596 if (recipient_policy &&
1597 !bus_client_policy_check_can_receive (recipient_policy,
1601 addressed_recipient, proposed_recipient,
1604 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1605 "Rejected receive message", toggles,
1606 message, sender, proposed_recipient, requested_reply,
1607 (addressed_recipient == proposed_recipient), NULL);
1608 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1612 /* See if limits on size have been exceeded */
1613 if (proposed_recipient &&
1614 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1615 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1617 complain_about_message (context, DBUS_ERROR_LIMITS_EXCEEDED,
1618 "Rejected: destination has a full message queue",
1619 0, message, sender, proposed_recipient, requested_reply, TRUE,
1621 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1625 /* Record that we will allow a reply here in the future (don't
1626 * bother if the recipient is the bus or this is an eavesdropping
1627 * connection). Only the addressed recipient may reply.
1629 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1631 addressed_recipient &&
1632 addressed_recipient == proposed_recipient && /* not eavesdropping */
1633 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1635 sender, addressed_recipient,
1638 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1642 _dbus_verbose ("security policy allowing message\n");