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);
147 server_timeout_callback (DBusTimeout *timeout,
150 /* can return FALSE on OOM but we just let it fire again later */
151 dbus_timeout_handle (timeout);
155 add_server_timeout (DBusTimeout *timeout,
158 DBusServer *server = data;
161 context = server_get_context (server);
163 return _dbus_loop_add_timeout (context->loop,
164 timeout, server_timeout_callback, server, NULL);
168 remove_server_timeout (DBusTimeout *timeout,
171 DBusServer *server = data;
174 context = server_get_context (server);
176 _dbus_loop_remove_timeout (context->loop,
177 timeout, server_timeout_callback, server);
181 new_connection_callback (DBusServer *server,
182 DBusConnection *new_connection,
185 BusContext *context = data;
187 if (!bus_connections_setup_connection (context->connections, new_connection))
189 _dbus_verbose ("No memory to setup new connection\n");
191 /* if we don't do this, it will get unref'd without
192 * being disconnected... kind of strange really
193 * that we have to do this, people won't get it right
196 dbus_connection_close (new_connection);
199 dbus_connection_set_max_received_size (new_connection,
200 context->limits.max_incoming_bytes);
202 dbus_connection_set_max_message_size (new_connection,
203 context->limits.max_message_size);
205 dbus_connection_set_max_received_unix_fds (new_connection,
206 context->limits.max_incoming_unix_fds);
208 dbus_connection_set_max_message_unix_fds (new_connection,
209 context->limits.max_message_unix_fds);
211 dbus_connection_set_allow_anonymous (new_connection,
212 context->allow_anonymous);
214 /* on OOM, we won't have ref'd the connection so it will die. */
218 free_server_data (void *data)
220 BusServerData *bd = data;
226 setup_server (BusContext *context,
228 char **auth_mechanisms,
233 bd = dbus_new0 (BusServerData, 1);
234 if (bd == NULL || !dbus_server_set_data (server,
236 bd, free_server_data))
243 bd->context = context;
245 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
251 dbus_server_set_new_connection_function (server,
252 new_connection_callback,
255 if (!dbus_server_set_watch_functions (server,
266 if (!dbus_server_set_timeout_functions (server,
268 remove_server_timeout,
279 /* This code only gets executed the first time the
280 * config files are parsed. It is not executed
281 * when config files are reloaded.
284 process_config_first_time_only (BusContext *context,
285 BusConfigParser *parser,
286 const DBusString *address,
287 dbus_bool_t systemd_activation,
290 DBusString log_prefix;
292 DBusList **addresses;
293 const char *user, *pidfile;
294 char **auth_mechanisms;
295 DBusList **auth_mechanisms_list;
299 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
302 auth_mechanisms = NULL;
304 _dbus_init_system_log ();
306 context->systemd_activation = systemd_activation;
308 /* Check for an existing pid file. Of course this is a race;
309 * we'd have to use fcntl() locks on the pid file to
310 * avoid that. But we want to check for the pid file
311 * before overwriting any existing sockets, etc.
313 pidfile = bus_config_parser_get_pidfile (parser);
319 _dbus_string_init_const (&u, pidfile);
321 if (_dbus_stat (&u, &stbuf, NULL))
327 _dbus_string_init (&p);
328 _dbus_file_get_contents(&p, &u, NULL);
329 _dbus_string_parse_int(&p, 0, &pid, NULL);
330 _dbus_string_free(&p);
332 if ((kill((int)pid, 0))) {
333 dbus_set_error(NULL, DBUS_ERROR_FILE_EXISTS,
334 "pid %ld not running, removing stale pid file\n",
336 _dbus_delete_file(&u, NULL);
339 dbus_set_error (error, DBUS_ERROR_FAILED,
340 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
349 /* keep around the pid filename so we can delete it later */
350 context->pidfile = _dbus_strdup (pidfile);
352 /* note that type may be NULL */
353 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
354 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
357 user = bus_config_parser_get_user (parser);
360 context->user = _dbus_strdup (user);
361 if (context->user == NULL)
365 /* Set up the prefix for syslog messages */
366 if (!_dbus_string_init (&log_prefix))
368 if (context->type && !strcmp (context->type, "system"))
370 if (!_dbus_string_append (&log_prefix, "[system] "))
373 else if (context->type && !strcmp (context->type, "session"))
375 DBusCredentials *credentials;
377 credentials = _dbus_credentials_new_from_current_process ();
380 if (!_dbus_string_append (&log_prefix, "[session "))
382 _dbus_credentials_unref (credentials);
385 if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
387 _dbus_credentials_unref (credentials);
390 if (!_dbus_string_append (&log_prefix, "] "))
392 _dbus_credentials_unref (credentials);
395 _dbus_credentials_unref (credentials);
397 if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
399 _dbus_string_free (&log_prefix);
401 /* Build an array of auth mechanisms */
403 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
404 len = _dbus_list_get_length (auth_mechanisms_list);
410 auth_mechanisms = dbus_new0 (char*, len + 1);
411 if (auth_mechanisms == NULL)
415 link = _dbus_list_get_first_link (auth_mechanisms_list);
418 auth_mechanisms[i] = _dbus_strdup (link->data);
419 if (auth_mechanisms[i] == NULL)
421 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
426 auth_mechanisms = NULL;
429 /* Listen on our addresses */
435 server = dbus_server_listen (_dbus_string_get_const_data(address), error);
438 _DBUS_ASSERT_ERROR_IS_SET (error);
441 else if (!setup_server (context, server, auth_mechanisms, error))
443 _DBUS_ASSERT_ERROR_IS_SET (error);
447 if (!_dbus_list_append (&context->servers, server))
452 addresses = bus_config_parser_get_addresses (parser);
454 link = _dbus_list_get_first_link (addresses);
459 server = dbus_server_listen (link->data, error);
462 _DBUS_ASSERT_ERROR_IS_SET (error);
465 else if (!setup_server (context, server, auth_mechanisms, error))
467 _DBUS_ASSERT_ERROR_IS_SET (error);
471 if (!_dbus_list_append (&context->servers, server))
474 link = _dbus_list_get_next_link (addresses, link);
478 context->fork = bus_config_parser_get_fork (parser);
479 context->syslog = bus_config_parser_get_syslog (parser);
480 context->keep_umask = bus_config_parser_get_keep_umask (parser);
481 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
483 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
487 dbus_free_string_array (auth_mechanisms);
492 dbus_free_string_array (auth_mechanisms);
496 /* This code gets executed every time the config files
497 * are parsed: both during BusContext construction
498 * and on reloads. This function is slightly screwy
499 * since it can do a "half reload" in out-of-memory
500 * situations. Realistically, unlikely to ever matter.
503 process_config_every_time (BusContext *context,
504 BusConfigParser *parser,
505 dbus_bool_t is_reload,
508 DBusString full_address;
511 BusActivation *new_activation;
513 const char *servicehelper;
518 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
523 if (!_dbus_string_init (&full_address))
529 /* get our limits and timeout lengths */
530 bus_config_parser_get_limits (parser, &context->limits);
533 bus_policy_unref (context->policy);
534 context->policy = bus_config_parser_steal_policy (parser);
535 _dbus_assert (context->policy != NULL);
537 /* We have to build the address backward, so that
538 * <listen> later in the config file have priority
540 link = _dbus_list_get_last_link (&context->servers);
543 addr = dbus_server_get_address (link->data);
550 if (_dbus_string_get_length (&full_address) > 0)
552 if (!_dbus_string_append (&full_address, ";"))
559 if (!_dbus_string_append (&full_address, addr))
568 link = _dbus_list_get_prev_link (&context->servers, link);
572 dbus_free (context->address);
574 if (!_dbus_string_copy_data (&full_address, &context->address))
580 /* get the service directories */
581 dirs = bus_config_parser_get_service_dirs (parser);
583 /* and the service helper */
584 servicehelper = bus_config_parser_get_servicehelper (parser);
586 s = _dbus_strdup(servicehelper);
587 if (s == NULL && servicehelper != NULL)
594 dbus_free(context->servicehelper);
595 context->servicehelper = s;
598 /* Create activation subsystem */
599 if (context->activation)
601 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
606 context->activation = bus_activation_new (context, &full_address, dirs, error);
609 if (context->activation == NULL)
611 _DBUS_ASSERT_ERROR_IS_SET (error);
615 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
619 _dbus_string_free (&full_address);
628 list_concat_new (DBusList **a,
636 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
638 if (!_dbus_list_append (result, link->data))
641 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
643 if (!_dbus_list_append (result, link->data))
649 _dbus_list_clear (result);
654 raise_file_descriptor_limit (BusContext *context)
657 /* I just picked this out of thin air; we need some extra
658 * descriptors for things like any internal pipes we create,
659 * inotify, connections to SELinux, etc.
661 unsigned int arbitrary_extra_fds = 32;
664 limit = context->limits.max_completed_connections +
665 context->limits.max_incomplete_connections
666 + arbitrary_extra_fds;
668 _dbus_request_file_descriptor_limit (limit);
672 process_config_postinit (BusContext *context,
673 BusConfigParser *parser,
676 DBusHashTable *service_context_table;
677 DBusList *watched_dirs = NULL;
679 raise_file_descriptor_limit (context);
681 service_context_table = bus_config_parser_steal_service_context_table (parser);
682 if (!bus_registry_set_service_context_table (context->registry,
683 service_context_table))
689 _dbus_hash_table_unref (service_context_table);
691 /* We need to monitor both the configuration directories and directories
692 * containing .service files.
694 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
695 bus_config_parser_get_service_dirs (parser),
702 bus_set_watched_dirs (context, &watched_dirs);
704 _dbus_list_clear (&watched_dirs);
710 bus_context_new (const DBusString *config_file,
711 ForceForkSetting force_fork,
712 DBusPipe *print_addr_pipe,
713 DBusPipe *print_pid_pipe,
714 const DBusString *address,
715 dbus_bool_t systemd_activation,
718 DBusString log_prefix;
720 BusConfigParser *parser;
722 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
727 if (!dbus_server_allocate_data_slot (&server_data_slot))
733 context = dbus_new0 (BusContext, 1);
739 context->refcount = 1;
741 _dbus_generate_uuid (&context->uuid);
743 if (!_dbus_string_copy_data (config_file, &context->config_file))
749 context->loop = _dbus_loop_new ();
750 if (context->loop == NULL)
756 context->registry = bus_registry_new (context);
757 if (context->registry == NULL)
763 parser = bus_config_load (config_file, TRUE, NULL, error);
766 _DBUS_ASSERT_ERROR_IS_SET (error);
770 if (!process_config_first_time_only (context, parser, address, systemd_activation, error))
772 _DBUS_ASSERT_ERROR_IS_SET (error);
775 if (!process_config_every_time (context, parser, FALSE, error))
777 _DBUS_ASSERT_ERROR_IS_SET (error);
781 /* we need another ref of the server data slot for the context
784 if (!dbus_server_allocate_data_slot (&server_data_slot))
785 _dbus_assert_not_reached ("second ref of server data slot failed");
787 /* Note that we don't know whether the print_addr_pipe is
788 * one of the sockets we're using to listen on, or some
789 * other random thing. But I think the answer is "don't do
792 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
795 const char *a = bus_context_get_address (context);
798 _dbus_assert (a != NULL);
799 if (!_dbus_string_init (&addr))
805 if (!_dbus_string_append (&addr, a) ||
806 !_dbus_string_append (&addr, "\n"))
808 _dbus_string_free (&addr);
813 bytes = _dbus_string_get_length (&addr);
814 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
816 /* pipe write returns an error on failure but not short write */
817 if (error != NULL && !dbus_error_is_set (error))
819 dbus_set_error (error, DBUS_ERROR_FAILED,
820 "Printing message bus address: did not write all bytes\n");
822 _dbus_string_free (&addr);
826 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
827 _dbus_pipe_close (print_addr_pipe, NULL);
829 _dbus_string_free (&addr);
832 context->connections = bus_connections_new (context);
833 if (context->connections == NULL)
839 context->matchmaker = bus_matchmaker_new ();
840 if (context->matchmaker == NULL)
846 /* check user before we fork */
847 if (context->user != NULL)
849 if (!_dbus_verify_daemon_user (context->user))
851 dbus_set_error (error, DBUS_ERROR_FAILED,
852 "Could not get UID and GID for username \"%s\"",
858 /* Now become a daemon if appropriate and write out pid file in any case */
862 if (context->pidfile)
863 _dbus_string_init_const (&u, context->pidfile);
865 if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
867 _dbus_verbose ("Forking and becoming daemon\n");
869 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
872 context->keep_umask))
874 _DBUS_ASSERT_ERROR_IS_SET (error);
880 _dbus_verbose ("Fork not requested\n");
882 /* Need to write PID file and to PID pipe for ourselves,
883 * not for the child process. This is a no-op if the pidfile
884 * is NULL and print_pid_pipe is NULL.
886 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
891 _DBUS_ASSERT_ERROR_IS_SET (error);
897 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
898 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
899 _dbus_pipe_close (print_pid_pipe, NULL);
901 if (!bus_selinux_full_init ())
903 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but AVC initialization failed; check system log\n");
906 if (!process_config_postinit (context, parser, error))
908 _DBUS_ASSERT_ERROR_IS_SET (error);
914 bus_config_parser_unref (parser);
918 /* Here we change our credentials if required,
919 * as soon as we've set up our sockets and pidfile
921 if (context->user != NULL)
923 if (!_dbus_change_to_daemon_user (context->user, error))
925 _DBUS_ASSERT_ERROR_IS_SET (error);
930 /* FIXME - why not just put this in full_init() below? */
931 bus_selinux_audit_init ();
935 dbus_server_free_data_slot (&server_data_slot);
941 bus_config_parser_unref (parser);
943 bus_context_unref (context);
945 if (server_data_slot >= 0)
946 dbus_server_free_data_slot (&server_data_slot);
952 bus_context_get_id (BusContext *context,
955 return _dbus_uuid_encode (&context->uuid, uuid);
959 bus_context_reload_config (BusContext *context,
962 BusConfigParser *parser;
963 DBusString config_file;
966 /* Flush the user database cache */
967 _dbus_flush_caches ();
970 _dbus_string_init_const (&config_file, context->config_file);
971 parser = bus_config_load (&config_file, TRUE, NULL, error);
974 _DBUS_ASSERT_ERROR_IS_SET (error);
978 if (!process_config_every_time (context, parser, TRUE, error))
980 _DBUS_ASSERT_ERROR_IS_SET (error);
983 if (!process_config_postinit (context, parser, error))
985 _DBUS_ASSERT_ERROR_IS_SET (error);
990 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
993 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
995 bus_config_parser_unref (parser);
1000 shutdown_server (BusContext *context,
1003 if (server == NULL ||
1004 !dbus_server_get_is_connected (server))
1007 if (!dbus_server_set_watch_functions (server,
1011 _dbus_assert_not_reached ("setting watch functions to NULL failed");
1013 if (!dbus_server_set_timeout_functions (server,
1017 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
1019 dbus_server_disconnect (server);
1023 bus_context_shutdown (BusContext *context)
1027 link = _dbus_list_get_first_link (&context->servers);
1028 while (link != NULL)
1030 shutdown_server (context, link->data);
1032 link = _dbus_list_get_next_link (&context->servers, link);
1037 bus_context_ref (BusContext *context)
1039 _dbus_assert (context->refcount > 0);
1040 context->refcount += 1;
1046 bus_context_unref (BusContext *context)
1048 _dbus_assert (context->refcount > 0);
1049 context->refcount -= 1;
1051 if (context->refcount == 0)
1055 _dbus_verbose ("Finalizing bus context %p\n", context);
1057 bus_context_shutdown (context);
1059 if (context->connections)
1061 bus_connections_unref (context->connections);
1062 context->connections = NULL;
1065 if (context->registry)
1067 bus_registry_unref (context->registry);
1068 context->registry = NULL;
1071 if (context->activation)
1073 bus_activation_unref (context->activation);
1074 context->activation = NULL;
1077 link = _dbus_list_get_first_link (&context->servers);
1078 while (link != NULL)
1080 dbus_server_unref (link->data);
1082 link = _dbus_list_get_next_link (&context->servers, link);
1084 _dbus_list_clear (&context->servers);
1086 if (context->policy)
1088 bus_policy_unref (context->policy);
1089 context->policy = NULL;
1094 _dbus_loop_unref (context->loop);
1095 context->loop = NULL;
1098 if (context->matchmaker)
1100 bus_matchmaker_unref (context->matchmaker);
1101 context->matchmaker = NULL;
1104 dbus_free (context->config_file);
1105 dbus_free (context->log_prefix);
1106 dbus_free (context->type);
1107 dbus_free (context->address);
1108 dbus_free (context->user);
1109 dbus_free (context->servicehelper);
1111 if (context->pidfile)
1114 _dbus_string_init_const (&u, context->pidfile);
1116 /* Deliberately ignore errors here, since there's not much
1117 * we can do about it, and we're exiting anyways.
1119 _dbus_delete_file (&u, NULL);
1121 dbus_free (context->pidfile);
1123 dbus_free (context);
1125 dbus_server_free_data_slot (&server_data_slot);
1129 /* type may be NULL */
1131 bus_context_get_type (BusContext *context)
1133 return context->type;
1137 bus_context_get_address (BusContext *context)
1139 return context->address;
1143 bus_context_get_servicehelper (BusContext *context)
1145 return context->servicehelper;
1149 bus_context_get_systemd_activation (BusContext *context)
1151 return context->systemd_activation;
1155 bus_context_get_registry (BusContext *context)
1157 return context->registry;
1161 bus_context_get_connections (BusContext *context)
1163 return context->connections;
1167 bus_context_get_activation (BusContext *context)
1169 return context->activation;
1173 bus_context_get_matchmaker (BusContext *context)
1175 return context->matchmaker;
1179 bus_context_get_loop (BusContext *context)
1181 return context->loop;
1185 bus_context_allow_unix_user (BusContext *context,
1188 return bus_policy_allow_unix_user (context->policy,
1192 /* For now this is never actually called because the default
1193 * DBusConnection behavior of 'same user that owns the bus can connect'
1194 * is all it would do.
1197 bus_context_allow_windows_user (BusContext *context,
1198 const char *windows_sid)
1200 return bus_policy_allow_windows_user (context->policy,
1205 bus_context_get_policy (BusContext *context)
1207 return context->policy;
1211 bus_context_create_client_policy (BusContext *context,
1212 DBusConnection *connection,
1215 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1216 return bus_policy_create_client_policy (context->policy, connection,
1221 bus_context_get_activation_timeout (BusContext *context)
1224 return context->limits.activation_timeout;
1228 bus_context_get_auth_timeout (BusContext *context)
1230 return context->limits.auth_timeout;
1234 bus_context_get_max_completed_connections (BusContext *context)
1236 return context->limits.max_completed_connections;
1240 bus_context_get_max_incomplete_connections (BusContext *context)
1242 return context->limits.max_incomplete_connections;
1246 bus_context_get_max_connections_per_user (BusContext *context)
1248 return context->limits.max_connections_per_user;
1252 bus_context_get_max_pending_activations (BusContext *context)
1254 return context->limits.max_pending_activations;
1258 bus_context_get_max_services_per_connection (BusContext *context)
1260 return context->limits.max_services_per_connection;
1264 bus_context_get_max_match_rules_per_connection (BusContext *context)
1266 return context->limits.max_match_rules_per_connection;
1270 bus_context_get_max_replies_per_connection (BusContext *context)
1272 return context->limits.max_replies_per_connection;
1276 bus_context_get_reply_timeout (BusContext *context)
1278 return context->limits.reply_timeout;
1282 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
1285 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1289 if (!context->syslog)
1291 /* we're not syslogging; just output to stderr */
1292 va_start (args, msg);
1293 vfprintf (stderr, msg, args);
1294 fprintf (stderr, "\n");
1299 va_start (args, msg);
1301 if (context->log_prefix)
1303 DBusString full_msg;
1305 if (!_dbus_string_init (&full_msg))
1307 if (!_dbus_string_append (&full_msg, context->log_prefix))
1309 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1312 _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1314 _dbus_string_free (&full_msg);
1317 _dbus_system_logv (severity, msg, args);
1323 static inline const char *
1324 nonnull (const char *maybe_null,
1325 const char *if_null)
1327 return (maybe_null ? maybe_null : if_null);
1331 * Log something about a message, usually that it was rejected.
1334 complain_about_message (BusContext *context,
1335 const char *error_name,
1336 const char *complaint,
1338 DBusMessage *message,
1339 DBusConnection *sender,
1340 DBusConnection *proposed_recipient,
1341 dbus_bool_t requested_reply,
1345 DBusError stack_error = DBUS_ERROR_INIT;
1346 const char *sender_name;
1347 const char *sender_loginfo;
1348 const char *proposed_recipient_loginfo;
1350 if (error == NULL && !log)
1355 sender_name = bus_connection_get_name (sender);
1356 sender_loginfo = bus_connection_get_loginfo (sender);
1360 sender_name = "(unset)";
1361 sender_loginfo = "(bus)";
1364 if (proposed_recipient != NULL)
1365 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1367 proposed_recipient_loginfo = "bus";
1369 dbus_set_error (&stack_error, error_name,
1370 "%s, %d matched rules; type=\"%s\", sender=\"%s\" (%s) "
1371 "interface=\"%s\" member=\"%s\" error name=\"%s\" "
1372 "requested_reply=\"%d\" destination=\"%s\" (%s)",
1375 dbus_message_type_to_string (dbus_message_get_type (message)),
1378 nonnull (dbus_message_get_interface (message), "(unset)"),
1379 nonnull (dbus_message_get_member (message), "(unset)"),
1380 nonnull (dbus_message_get_error_name (message), "(unset)"),
1382 nonnull (dbus_message_get_destination (message), DBUS_SERVICE_DBUS),
1383 proposed_recipient_loginfo);
1385 /* If we hit OOM while setting the error, this will syslog "out of memory"
1386 * which is itself an indication that something is seriously wrong */
1388 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, "%s",
1389 stack_error.message);
1391 dbus_move_error (&stack_error, error);
1395 * addressed_recipient is the recipient specified in the message.
1397 * proposed_recipient is the recipient we're considering sending
1398 * to right this second, and may be an eavesdropper.
1400 * sender is the sender of the message.
1402 * NULL for proposed_recipient or sender definitely means the bus driver.
1404 * NULL for addressed_recipient may mean the bus driver, or may mean
1405 * no destination was specified in the message (e.g. a signal).
1408 bus_context_check_security_policy (BusContext *context,
1409 BusTransaction *transaction,
1410 DBusConnection *sender,
1411 DBusConnection *addressed_recipient,
1412 DBusConnection *proposed_recipient,
1413 DBusMessage *message,
1417 BusClientPolicy *sender_policy;
1418 BusClientPolicy *recipient_policy;
1419 dbus_int32_t toggles;
1422 dbus_bool_t requested_reply;
1423 const char *sender_name;
1424 const char *sender_loginfo;
1425 const char *proposed_recipient_loginfo;
1427 type = dbus_message_get_type (message);
1428 dest = dbus_message_get_destination (message);
1430 /* dispatch.c was supposed to ensure these invariants */
1431 _dbus_assert (dest != NULL ||
1432 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1433 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1434 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1435 addressed_recipient != NULL ||
1436 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1440 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1441 case DBUS_MESSAGE_TYPE_SIGNAL:
1442 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1443 case DBUS_MESSAGE_TYPE_ERROR:
1447 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1450 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1451 "Message bus will not accept messages of unknown type\n");
1456 requested_reply = FALSE;
1460 /* First verify the SELinux access controls. If allowed then
1461 * go on with the standard checks.
1463 if (!bus_selinux_allows_send (sender, proposed_recipient,
1464 dbus_message_type_to_string (dbus_message_get_type (message)),
1465 dbus_message_get_interface (message),
1466 dbus_message_get_member (message),
1467 dbus_message_get_error_name (message),
1468 dest ? dest : DBUS_SERVICE_DBUS, error))
1470 if (error != NULL && !dbus_error_is_set (error))
1472 /* don't syslog this, just set the error: avc_has_perm should
1473 * have already written to either the audit log or syslog */
1474 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1475 "An SELinux policy prevents this sender from sending this "
1476 "message to this recipient",
1477 0, message, sender, proposed_recipient, FALSE, FALSE, error);
1478 _dbus_verbose ("SELinux security check denying send to service\n");
1484 if (bus_connection_is_active (sender))
1486 sender_policy = bus_connection_get_policy (sender);
1487 _dbus_assert (sender_policy != NULL);
1489 /* Fill in requested_reply variable with TRUE if this is a
1490 * reply and the reply was pending.
1492 if (dbus_message_get_reply_serial (message) != 0)
1494 if (proposed_recipient != NULL /* not to the bus driver */ &&
1495 addressed_recipient == proposed_recipient /* not eavesdropping */)
1499 dbus_error_init (&error2);
1500 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1502 sender, addressed_recipient, message,
1504 if (dbus_error_is_set (&error2))
1506 dbus_move_error (&error2, error);
1514 /* Policy for inactive connections is that they can only send
1515 * the hello message to the bus driver
1517 if (proposed_recipient == NULL &&
1518 dbus_message_is_method_call (message,
1519 DBUS_INTERFACE_DBUS,
1522 _dbus_verbose ("security check allowing %s message\n",
1528 _dbus_verbose ("security check disallowing non-%s message\n",
1531 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1532 "Client tried to send a message other than %s without being registered",
1541 sender_policy = NULL;
1543 /* If the sender is the bus driver, we assume any reply was a
1544 * requested reply as bus driver won't send bogus ones
1546 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1547 dbus_message_get_reply_serial (message) != 0)
1548 requested_reply = TRUE;
1551 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1552 (sender == NULL && sender_policy == NULL));
1554 if (proposed_recipient != NULL)
1556 /* only the bus driver can send to an inactive recipient (as it
1557 * owns no services, so other apps can't address it). Inactive
1558 * recipients can receive any message.
1560 if (bus_connection_is_active (proposed_recipient))
1562 recipient_policy = bus_connection_get_policy (proposed_recipient);
1563 _dbus_assert (recipient_policy != NULL);
1565 else if (sender == NULL)
1567 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1568 recipient_policy = NULL;
1572 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1573 recipient_policy = NULL;
1577 recipient_policy = NULL;
1579 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1580 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1581 (proposed_recipient == NULL && recipient_policy == NULL));
1584 if (sender_policy &&
1585 !bus_client_policy_check_can_send (sender_policy,
1589 message, &toggles, &log))
1591 const char *msg = "Rejected send message, %d matched rules; "
1592 "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))";
1594 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1595 "Rejected send message", toggles,
1596 message, sender, proposed_recipient, requested_reply,
1597 (addressed_recipient == proposed_recipient), error);
1598 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1604 /* We want to drop this message, and are only not doing so for backwards
1606 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1607 "Would reject message", toggles,
1608 message, sender, proposed_recipient, requested_reply,
1612 if (recipient_policy &&
1613 !bus_client_policy_check_can_receive (recipient_policy,
1617 addressed_recipient, proposed_recipient,
1620 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1621 "Rejected receive message", toggles,
1622 message, sender, proposed_recipient, requested_reply,
1623 (addressed_recipient == proposed_recipient), NULL);
1624 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1628 /* See if limits on size have been exceeded */
1629 if (proposed_recipient &&
1630 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1631 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1633 complain_about_message (context, DBUS_ERROR_LIMITS_EXCEEDED,
1634 "Rejected: destination has a full message queue",
1635 0, message, sender, proposed_recipient, requested_reply, TRUE,
1637 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1641 /* Record that we will allow a reply here in the future (don't
1642 * bother if the recipient is the bus or this is an eavesdropping
1643 * connection). Only the addressed recipient may reply.
1645 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1647 addressed_recipient &&
1648 addressed_recipient == proposed_recipient && /* not eavesdropping */
1649 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1651 sender, addressed_recipient,
1654 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1658 _dbus_verbose ("security policy allowing message\n");