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 server_watch_callback (DBusWatch *watch,
107 unsigned int condition,
110 /* FIXME this can be done in dbus-mainloop.c
111 * if the code in activation.c for the babysitter
112 * watch handler is fixed.
115 return dbus_watch_handle (watch, condition);
119 add_server_watch (DBusWatch *watch,
122 DBusServer *server = data;
125 context = server_get_context (server);
127 return _dbus_loop_add_watch (context->loop,
128 watch, server_watch_callback, server,
133 remove_server_watch (DBusWatch *watch,
136 DBusServer *server = data;
139 context = server_get_context (server);
141 _dbus_loop_remove_watch (context->loop,
142 watch, server_watch_callback, server);
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 dbus_bool_t systemd_activation,
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;
293 _dbus_init_system_log ();
295 context->systemd_activation = systemd_activation;
297 /* Check for an existing pid file. Of course this is a race;
298 * we'd have to use fcntl() locks on the pid file to
299 * avoid that. But we want to check for the pid file
300 * before overwriting any existing sockets, etc.
302 pidfile = bus_config_parser_get_pidfile (parser);
308 _dbus_string_init_const (&u, pidfile);
310 if (_dbus_stat (&u, &stbuf, NULL))
316 _dbus_string_init (&p);
317 _dbus_file_get_contents(&p, &u, NULL);
318 _dbus_string_parse_int(&p, 0, &pid, NULL);
319 _dbus_string_free(&p);
321 if ((kill((int)pid, 0))) {
322 dbus_set_error(NULL, DBUS_ERROR_FILE_EXISTS,
323 "pid %ld not running, removing stale pid file\n",
325 _dbus_delete_file(&u, NULL);
328 dbus_set_error (error, DBUS_ERROR_FAILED,
329 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
338 /* keep around the pid filename so we can delete it later */
339 context->pidfile = _dbus_strdup (pidfile);
341 /* note that type may be NULL */
342 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
343 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
346 user = bus_config_parser_get_user (parser);
349 context->user = _dbus_strdup (user);
350 if (context->user == NULL)
354 /* Set up the prefix for syslog messages */
355 if (!_dbus_string_init (&log_prefix))
357 if (context->type && !strcmp (context->type, "system"))
359 if (!_dbus_string_append (&log_prefix, "[system] "))
362 else if (context->type && !strcmp (context->type, "session"))
364 DBusCredentials *credentials;
366 credentials = _dbus_credentials_new_from_current_process ();
369 if (!_dbus_string_append (&log_prefix, "[session "))
371 _dbus_credentials_unref (credentials);
374 if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
376 _dbus_credentials_unref (credentials);
379 if (!_dbus_string_append (&log_prefix, "] "))
381 _dbus_credentials_unref (credentials);
384 _dbus_credentials_unref (credentials);
386 if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
388 _dbus_string_free (&log_prefix);
390 /* Build an array of auth mechanisms */
392 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
393 len = _dbus_list_get_length (auth_mechanisms_list);
399 auth_mechanisms = dbus_new0 (char*, len + 1);
400 if (auth_mechanisms == NULL)
404 link = _dbus_list_get_first_link (auth_mechanisms_list);
407 auth_mechanisms[i] = _dbus_strdup (link->data);
408 if (auth_mechanisms[i] == NULL)
410 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
415 auth_mechanisms = NULL;
418 /* Listen on our addresses */
424 server = dbus_server_listen (_dbus_string_get_const_data(address), error);
427 _DBUS_ASSERT_ERROR_IS_SET (error);
430 else if (!setup_server (context, server, auth_mechanisms, error))
432 _DBUS_ASSERT_ERROR_IS_SET (error);
436 if (!_dbus_list_append (&context->servers, server))
441 addresses = bus_config_parser_get_addresses (parser);
443 link = _dbus_list_get_first_link (addresses);
448 server = dbus_server_listen (link->data, error);
451 _DBUS_ASSERT_ERROR_IS_SET (error);
454 else if (!setup_server (context, server, auth_mechanisms, error))
456 _DBUS_ASSERT_ERROR_IS_SET (error);
460 if (!_dbus_list_append (&context->servers, server))
463 link = _dbus_list_get_next_link (addresses, link);
467 context->fork = bus_config_parser_get_fork (parser);
468 context->syslog = bus_config_parser_get_syslog (parser);
469 context->keep_umask = bus_config_parser_get_keep_umask (parser);
470 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
472 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
476 dbus_free_string_array (auth_mechanisms);
481 dbus_free_string_array (auth_mechanisms);
485 /* This code gets executed every time the config files
486 * are parsed: both during BusContext construction
487 * and on reloads. This function is slightly screwy
488 * since it can do a "half reload" in out-of-memory
489 * situations. Realistically, unlikely to ever matter.
492 process_config_every_time (BusContext *context,
493 BusConfigParser *parser,
494 dbus_bool_t is_reload,
497 DBusString full_address;
500 BusActivation *new_activation;
502 const char *servicehelper;
507 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
512 if (!_dbus_string_init (&full_address))
518 /* get our limits and timeout lengths */
519 bus_config_parser_get_limits (parser, &context->limits);
522 bus_policy_unref (context->policy);
523 context->policy = bus_config_parser_steal_policy (parser);
524 _dbus_assert (context->policy != NULL);
526 /* We have to build the address backward, so that
527 * <listen> later in the config file have priority
529 link = _dbus_list_get_last_link (&context->servers);
532 addr = dbus_server_get_address (link->data);
539 if (_dbus_string_get_length (&full_address) > 0)
541 if (!_dbus_string_append (&full_address, ";"))
548 if (!_dbus_string_append (&full_address, addr))
557 link = _dbus_list_get_prev_link (&context->servers, link);
561 dbus_free (context->address);
563 if (!_dbus_string_copy_data (&full_address, &context->address))
569 /* get the service directories */
570 dirs = bus_config_parser_get_service_dirs (parser);
572 /* and the service helper */
573 servicehelper = bus_config_parser_get_servicehelper (parser);
575 s = _dbus_strdup(servicehelper);
576 if (s == NULL && servicehelper != NULL)
583 dbus_free(context->servicehelper);
584 context->servicehelper = s;
587 /* Create activation subsystem */
588 if (context->activation)
590 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
595 context->activation = bus_activation_new (context, &full_address, dirs, error);
598 if (context->activation == NULL)
600 _DBUS_ASSERT_ERROR_IS_SET (error);
604 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
608 _dbus_string_free (&full_address);
617 list_concat_new (DBusList **a,
625 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
627 if (!_dbus_list_append (result, link->data))
630 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
632 if (!_dbus_list_append (result, link->data))
638 _dbus_list_clear (result);
643 raise_file_descriptor_limit (BusContext *context)
646 /* I just picked this out of thin air; we need some extra
647 * descriptors for things like any internal pipes we create,
648 * inotify, connections to SELinux, etc.
650 unsigned int arbitrary_extra_fds = 32;
653 limit = context->limits.max_completed_connections +
654 context->limits.max_incomplete_connections
655 + arbitrary_extra_fds;
657 _dbus_request_file_descriptor_limit (limit);
661 process_config_postinit (BusContext *context,
662 BusConfigParser *parser,
665 DBusHashTable *service_context_table;
666 DBusList *watched_dirs = NULL;
668 raise_file_descriptor_limit (context);
670 service_context_table = bus_config_parser_steal_service_context_table (parser);
671 if (!bus_registry_set_service_context_table (context->registry,
672 service_context_table))
678 _dbus_hash_table_unref (service_context_table);
680 /* We need to monitor both the configuration directories and directories
681 * containing .service files.
683 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
684 bus_config_parser_get_service_dirs (parser),
691 bus_set_watched_dirs (context, &watched_dirs);
693 _dbus_list_clear (&watched_dirs);
699 bus_context_new (const DBusString *config_file,
700 ForceForkSetting force_fork,
701 DBusPipe *print_addr_pipe,
702 DBusPipe *print_pid_pipe,
703 const DBusString *address,
704 dbus_bool_t systemd_activation,
707 DBusString log_prefix;
709 BusConfigParser *parser;
711 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
716 if (!dbus_server_allocate_data_slot (&server_data_slot))
722 context = dbus_new0 (BusContext, 1);
728 context->refcount = 1;
730 _dbus_generate_uuid (&context->uuid);
732 if (!_dbus_string_copy_data (config_file, &context->config_file))
738 context->loop = _dbus_loop_new ();
739 if (context->loop == NULL)
745 context->registry = bus_registry_new (context);
746 if (context->registry == NULL)
752 parser = bus_config_load (config_file, TRUE, NULL, error);
755 _DBUS_ASSERT_ERROR_IS_SET (error);
759 if (!process_config_first_time_only (context, parser, address, systemd_activation, error))
761 _DBUS_ASSERT_ERROR_IS_SET (error);
764 if (!process_config_every_time (context, parser, FALSE, error))
766 _DBUS_ASSERT_ERROR_IS_SET (error);
770 /* we need another ref of the server data slot for the context
773 if (!dbus_server_allocate_data_slot (&server_data_slot))
774 _dbus_assert_not_reached ("second ref of server data slot failed");
776 /* Note that we don't know whether the print_addr_pipe is
777 * one of the sockets we're using to listen on, or some
778 * other random thing. But I think the answer is "don't do
781 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
784 const char *a = bus_context_get_address (context);
787 _dbus_assert (a != NULL);
788 if (!_dbus_string_init (&addr))
794 if (!_dbus_string_append (&addr, a) ||
795 !_dbus_string_append (&addr, "\n"))
797 _dbus_string_free (&addr);
802 bytes = _dbus_string_get_length (&addr);
803 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
805 /* pipe write returns an error on failure but not short write */
806 if (error != NULL && !dbus_error_is_set (error))
808 dbus_set_error (error, DBUS_ERROR_FAILED,
809 "Printing message bus address: did not write all bytes\n");
811 _dbus_string_free (&addr);
815 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
816 _dbus_pipe_close (print_addr_pipe, NULL);
818 _dbus_string_free (&addr);
821 context->connections = bus_connections_new (context);
822 if (context->connections == NULL)
828 context->matchmaker = bus_matchmaker_new ();
829 if (context->matchmaker == NULL)
835 /* check user before we fork */
836 if (context->user != NULL)
838 if (!_dbus_verify_daemon_user (context->user))
840 dbus_set_error (error, DBUS_ERROR_FAILED,
841 "Could not get UID and GID for username \"%s\"",
847 /* Now become a daemon if appropriate and write out pid file in any case */
851 if (context->pidfile)
852 _dbus_string_init_const (&u, context->pidfile);
854 if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
856 _dbus_verbose ("Forking and becoming daemon\n");
858 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
861 context->keep_umask))
863 _DBUS_ASSERT_ERROR_IS_SET (error);
869 _dbus_verbose ("Fork not requested\n");
871 /* Need to write PID file and to PID pipe for ourselves,
872 * not for the child process. This is a no-op if the pidfile
873 * is NULL and print_pid_pipe is NULL.
875 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
880 _DBUS_ASSERT_ERROR_IS_SET (error);
886 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
887 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
888 _dbus_pipe_close (print_pid_pipe, NULL);
890 if (!bus_selinux_full_init ())
892 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but AVC initialization failed; check system log\n");
895 if (!process_config_postinit (context, parser, error))
897 _DBUS_ASSERT_ERROR_IS_SET (error);
903 bus_config_parser_unref (parser);
907 /* Here we change our credentials if required,
908 * as soon as we've set up our sockets and pidfile
910 if (context->user != NULL)
912 if (!_dbus_change_to_daemon_user (context->user, error))
914 _DBUS_ASSERT_ERROR_IS_SET (error);
919 /* FIXME - why not just put this in full_init() below? */
920 bus_selinux_audit_init ();
924 dbus_server_free_data_slot (&server_data_slot);
930 bus_config_parser_unref (parser);
932 bus_context_unref (context);
934 if (server_data_slot >= 0)
935 dbus_server_free_data_slot (&server_data_slot);
941 bus_context_get_id (BusContext *context,
944 return _dbus_uuid_encode (&context->uuid, uuid);
948 bus_context_reload_config (BusContext *context,
951 BusConfigParser *parser;
952 DBusString config_file;
955 /* Flush the user database cache */
956 _dbus_flush_caches ();
959 _dbus_string_init_const (&config_file, context->config_file);
960 parser = bus_config_load (&config_file, TRUE, NULL, error);
963 _DBUS_ASSERT_ERROR_IS_SET (error);
967 if (!process_config_every_time (context, parser, TRUE, error))
969 _DBUS_ASSERT_ERROR_IS_SET (error);
972 if (!process_config_postinit (context, parser, error))
974 _DBUS_ASSERT_ERROR_IS_SET (error);
979 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
982 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
984 bus_config_parser_unref (parser);
989 shutdown_server (BusContext *context,
992 if (server == NULL ||
993 !dbus_server_get_is_connected (server))
996 if (!dbus_server_set_watch_functions (server,
1000 _dbus_assert_not_reached ("setting watch functions to NULL failed");
1002 if (!dbus_server_set_timeout_functions (server,
1006 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
1008 dbus_server_disconnect (server);
1012 bus_context_shutdown (BusContext *context)
1016 link = _dbus_list_get_first_link (&context->servers);
1017 while (link != NULL)
1019 shutdown_server (context, link->data);
1021 link = _dbus_list_get_next_link (&context->servers, link);
1026 bus_context_ref (BusContext *context)
1028 _dbus_assert (context->refcount > 0);
1029 context->refcount += 1;
1035 bus_context_unref (BusContext *context)
1037 _dbus_assert (context->refcount > 0);
1038 context->refcount -= 1;
1040 if (context->refcount == 0)
1044 _dbus_verbose ("Finalizing bus context %p\n", context);
1046 bus_context_shutdown (context);
1048 if (context->connections)
1050 bus_connections_unref (context->connections);
1051 context->connections = NULL;
1054 if (context->registry)
1056 bus_registry_unref (context->registry);
1057 context->registry = NULL;
1060 if (context->activation)
1062 bus_activation_unref (context->activation);
1063 context->activation = NULL;
1066 link = _dbus_list_get_first_link (&context->servers);
1067 while (link != NULL)
1069 dbus_server_unref (link->data);
1071 link = _dbus_list_get_next_link (&context->servers, link);
1073 _dbus_list_clear (&context->servers);
1075 if (context->policy)
1077 bus_policy_unref (context->policy);
1078 context->policy = NULL;
1083 _dbus_loop_unref (context->loop);
1084 context->loop = NULL;
1087 if (context->matchmaker)
1089 bus_matchmaker_unref (context->matchmaker);
1090 context->matchmaker = NULL;
1093 dbus_free (context->config_file);
1094 dbus_free (context->log_prefix);
1095 dbus_free (context->type);
1096 dbus_free (context->address);
1097 dbus_free (context->user);
1098 dbus_free (context->servicehelper);
1100 if (context->pidfile)
1103 _dbus_string_init_const (&u, context->pidfile);
1105 /* Deliberately ignore errors here, since there's not much
1106 * we can do about it, and we're exiting anyways.
1108 _dbus_delete_file (&u, NULL);
1110 dbus_free (context->pidfile);
1112 dbus_free (context);
1114 dbus_server_free_data_slot (&server_data_slot);
1118 /* type may be NULL */
1120 bus_context_get_type (BusContext *context)
1122 return context->type;
1126 bus_context_get_address (BusContext *context)
1128 return context->address;
1132 bus_context_get_servicehelper (BusContext *context)
1134 return context->servicehelper;
1138 bus_context_get_systemd_activation (BusContext *context)
1140 return context->systemd_activation;
1144 bus_context_get_registry (BusContext *context)
1146 return context->registry;
1150 bus_context_get_connections (BusContext *context)
1152 return context->connections;
1156 bus_context_get_activation (BusContext *context)
1158 return context->activation;
1162 bus_context_get_matchmaker (BusContext *context)
1164 return context->matchmaker;
1168 bus_context_get_loop (BusContext *context)
1170 return context->loop;
1174 bus_context_allow_unix_user (BusContext *context,
1177 return bus_policy_allow_unix_user (context->policy,
1181 /* For now this is never actually called because the default
1182 * DBusConnection behavior of 'same user that owns the bus can connect'
1183 * is all it would do.
1186 bus_context_allow_windows_user (BusContext *context,
1187 const char *windows_sid)
1189 return bus_policy_allow_windows_user (context->policy,
1194 bus_context_get_policy (BusContext *context)
1196 return context->policy;
1200 bus_context_create_client_policy (BusContext *context,
1201 DBusConnection *connection,
1204 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1205 return bus_policy_create_client_policy (context->policy, connection,
1210 bus_context_get_activation_timeout (BusContext *context)
1213 return context->limits.activation_timeout;
1217 bus_context_get_auth_timeout (BusContext *context)
1219 return context->limits.auth_timeout;
1223 bus_context_get_max_completed_connections (BusContext *context)
1225 return context->limits.max_completed_connections;
1229 bus_context_get_max_incomplete_connections (BusContext *context)
1231 return context->limits.max_incomplete_connections;
1235 bus_context_get_max_connections_per_user (BusContext *context)
1237 return context->limits.max_connections_per_user;
1241 bus_context_get_max_pending_activations (BusContext *context)
1243 return context->limits.max_pending_activations;
1247 bus_context_get_max_services_per_connection (BusContext *context)
1249 return context->limits.max_services_per_connection;
1253 bus_context_get_max_match_rules_per_connection (BusContext *context)
1255 return context->limits.max_match_rules_per_connection;
1259 bus_context_get_max_replies_per_connection (BusContext *context)
1261 return context->limits.max_replies_per_connection;
1265 bus_context_get_reply_timeout (BusContext *context)
1267 return context->limits.reply_timeout;
1271 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
1274 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1278 if (!context->syslog)
1280 /* we're not syslogging; just output to stderr */
1281 va_start (args, msg);
1282 vfprintf (stderr, msg, args);
1283 fprintf (stderr, "\n");
1288 va_start (args, msg);
1290 if (context->log_prefix)
1292 DBusString full_msg;
1294 if (!_dbus_string_init (&full_msg))
1296 if (!_dbus_string_append (&full_msg, context->log_prefix))
1298 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1301 _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1303 _dbus_string_free (&full_msg);
1306 _dbus_system_logv (severity, msg, args);
1312 static inline const char *
1313 nonnull (const char *maybe_null,
1314 const char *if_null)
1316 return (maybe_null ? maybe_null : if_null);
1320 * Log something about a message, usually that it was rejected.
1323 complain_about_message (BusContext *context,
1324 const char *error_name,
1325 const char *complaint,
1327 DBusMessage *message,
1328 DBusConnection *sender,
1329 DBusConnection *proposed_recipient,
1330 dbus_bool_t requested_reply,
1334 DBusError stack_error = DBUS_ERROR_INIT;
1335 const char *sender_name;
1336 const char *sender_loginfo;
1337 const char *proposed_recipient_loginfo;
1339 if (error == NULL && !log)
1344 sender_name = bus_connection_get_name (sender);
1345 sender_loginfo = bus_connection_get_loginfo (sender);
1349 sender_name = "(unset)";
1350 sender_loginfo = "(bus)";
1353 if (proposed_recipient != NULL)
1354 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1356 proposed_recipient_loginfo = "bus";
1358 dbus_set_error (&stack_error, error_name,
1359 "%s, %d matched rules; type=\"%s\", sender=\"%s\" (%s) "
1360 "interface=\"%s\" member=\"%s\" error name=\"%s\" "
1361 "requested_reply=\"%d\" destination=\"%s\" (%s)",
1364 dbus_message_type_to_string (dbus_message_get_type (message)),
1367 nonnull (dbus_message_get_interface (message), "(unset)"),
1368 nonnull (dbus_message_get_member (message), "(unset)"),
1369 nonnull (dbus_message_get_error_name (message), "(unset)"),
1371 nonnull (dbus_message_get_destination (message), DBUS_SERVICE_DBUS),
1372 proposed_recipient_loginfo);
1374 /* If we hit OOM while setting the error, this will syslog "out of memory"
1375 * which is itself an indication that something is seriously wrong */
1377 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, "%s",
1378 stack_error.message);
1380 dbus_move_error (&stack_error, error);
1384 * addressed_recipient is the recipient specified in the message.
1386 * proposed_recipient is the recipient we're considering sending
1387 * to right this second, and may be an eavesdropper.
1389 * sender is the sender of the message.
1391 * NULL for proposed_recipient or sender definitely means the bus driver.
1393 * NULL for addressed_recipient may mean the bus driver, or may mean
1394 * no destination was specified in the message (e.g. a signal).
1397 bus_context_check_security_policy (BusContext *context,
1398 BusTransaction *transaction,
1399 DBusConnection *sender,
1400 DBusConnection *addressed_recipient,
1401 DBusConnection *proposed_recipient,
1402 DBusMessage *message,
1406 BusClientPolicy *sender_policy;
1407 BusClientPolicy *recipient_policy;
1408 dbus_int32_t toggles;
1411 dbus_bool_t requested_reply;
1412 const char *sender_name;
1413 const char *sender_loginfo;
1414 const char *proposed_recipient_loginfo;
1416 type = dbus_message_get_type (message);
1417 dest = dbus_message_get_destination (message);
1419 /* dispatch.c was supposed to ensure these invariants */
1420 _dbus_assert (dest != NULL ||
1421 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1422 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1423 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1424 addressed_recipient != NULL ||
1425 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1429 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1430 case DBUS_MESSAGE_TYPE_SIGNAL:
1431 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1432 case DBUS_MESSAGE_TYPE_ERROR:
1436 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1439 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1440 "Message bus will not accept messages of unknown type\n");
1445 requested_reply = FALSE;
1449 /* First verify the SELinux access controls. If allowed then
1450 * go on with the standard checks.
1452 if (!bus_selinux_allows_send (sender, proposed_recipient,
1453 dbus_message_type_to_string (dbus_message_get_type (message)),
1454 dbus_message_get_interface (message),
1455 dbus_message_get_member (message),
1456 dbus_message_get_error_name (message),
1457 dest ? dest : DBUS_SERVICE_DBUS, error))
1459 if (error != NULL && !dbus_error_is_set (error))
1461 /* don't syslog this, just set the error: avc_has_perm should
1462 * have already written to either the audit log or syslog */
1463 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1464 "An SELinux policy prevents this sender from sending this "
1465 "message to this recipient",
1466 0, message, sender, proposed_recipient, FALSE, FALSE, error);
1467 _dbus_verbose ("SELinux security check denying send to service\n");
1473 if (bus_connection_is_active (sender))
1475 sender_policy = bus_connection_get_policy (sender);
1476 _dbus_assert (sender_policy != NULL);
1478 /* Fill in requested_reply variable with TRUE if this is a
1479 * reply and the reply was pending.
1481 if (dbus_message_get_reply_serial (message) != 0)
1483 if (proposed_recipient != NULL /* not to the bus driver */ &&
1484 addressed_recipient == proposed_recipient /* not eavesdropping */)
1488 dbus_error_init (&error2);
1489 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1491 sender, addressed_recipient, message,
1493 if (dbus_error_is_set (&error2))
1495 dbus_move_error (&error2, error);
1503 /* Policy for inactive connections is that they can only send
1504 * the hello message to the bus driver
1506 if (proposed_recipient == NULL &&
1507 dbus_message_is_method_call (message,
1508 DBUS_INTERFACE_DBUS,
1511 _dbus_verbose ("security check allowing %s message\n",
1517 _dbus_verbose ("security check disallowing non-%s message\n",
1520 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1521 "Client tried to send a message other than %s without being registered",
1530 sender_policy = NULL;
1532 /* If the sender is the bus driver, we assume any reply was a
1533 * requested reply as bus driver won't send bogus ones
1535 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1536 dbus_message_get_reply_serial (message) != 0)
1537 requested_reply = TRUE;
1540 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1541 (sender == NULL && sender_policy == NULL));
1543 if (proposed_recipient != NULL)
1545 /* only the bus driver can send to an inactive recipient (as it
1546 * owns no services, so other apps can't address it). Inactive
1547 * recipients can receive any message.
1549 if (bus_connection_is_active (proposed_recipient))
1551 recipient_policy = bus_connection_get_policy (proposed_recipient);
1552 _dbus_assert (recipient_policy != NULL);
1554 else if (sender == NULL)
1556 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1557 recipient_policy = NULL;
1561 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1562 recipient_policy = NULL;
1566 recipient_policy = NULL;
1568 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1569 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1570 (proposed_recipient == NULL && recipient_policy == NULL));
1573 if (sender_policy &&
1574 !bus_client_policy_check_can_send (sender_policy,
1578 message, &toggles, &log))
1580 const char *msg = "Rejected send message, %d matched rules; "
1581 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))";
1583 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1584 "Rejected send message", toggles,
1585 message, sender, proposed_recipient, requested_reply,
1586 (addressed_recipient == proposed_recipient), error);
1587 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1593 /* We want to drop this message, and are only not doing so for backwards
1595 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1596 "Would reject message", toggles,
1597 message, sender, proposed_recipient, requested_reply,
1601 if (recipient_policy &&
1602 !bus_client_policy_check_can_receive (recipient_policy,
1606 addressed_recipient, proposed_recipient,
1609 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1610 "Rejected receive message", toggles,
1611 message, sender, proposed_recipient, requested_reply,
1612 (addressed_recipient == proposed_recipient), NULL);
1613 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1617 /* See if limits on size have been exceeded */
1618 if (proposed_recipient &&
1619 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1620 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1622 complain_about_message (context, DBUS_ERROR_LIMITS_EXCEEDED,
1623 "Rejected: destination has a full message queue",
1624 0, message, sender, proposed_recipient, requested_reply, TRUE,
1626 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1630 /* Record that we will allow a reply here in the future (don't
1631 * bother if the recipient is the bus or this is an eavesdropping
1632 * connection). Only the addressed recipient may reply.
1634 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1636 addressed_recipient &&
1637 addressed_recipient == proposed_recipient && /* not eavesdropping */
1638 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1640 sender, addressed_recipient,
1643 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1647 _dbus_verbose ("security policy allowing message\n");