1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* bus.c message bus context object
4 * Copyright (C) 2003, 2004 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "activation.h"
30 #include "connection.h"
34 #include "config-parser.h"
37 #include "dir-watch.h"
38 #include <dbus/dbus-list.h>
39 #include <dbus/dbus-hash.h>
40 #include <dbus/dbus-credentials.h>
41 #include <dbus/dbus-internals.h>
42 #include <dbus/dbus-server-protected.h>
61 BusConnections *connections;
62 BusActivation *activation;
63 BusRegistry *registry;
65 BusMatchmaker *matchmaker;
67 unsigned int fork : 1;
68 unsigned int syslog : 1;
69 unsigned int keep_umask : 1;
70 unsigned int allow_anonymous : 1;
71 unsigned int systemd_activation : 1;
72 dbus_bool_t watches_enabled;
75 static dbus_int32_t server_data_slot = -1;
82 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
85 server_get_context (DBusServer *server)
90 if (!dbus_server_allocate_data_slot (&server_data_slot))
93 bd = BUS_SERVER_DATA (server);
96 dbus_server_free_data_slot (&server_data_slot);
100 context = bd->context;
102 dbus_server_free_data_slot (&server_data_slot);
108 add_server_watch (DBusWatch *watch,
111 DBusServer *server = data;
114 context = server_get_context (server);
116 return _dbus_loop_add_watch (context->loop, watch);
120 remove_server_watch (DBusWatch *watch,
123 DBusServer *server = data;
126 context = server_get_context (server);
128 _dbus_loop_remove_watch (context->loop, watch);
132 toggle_server_watch (DBusWatch *watch,
135 DBusServer *server = data;
138 context = server_get_context (server);
140 _dbus_loop_toggle_watch (context->loop, watch);
144 add_server_timeout (DBusTimeout *timeout,
147 DBusServer *server = data;
150 context = server_get_context (server);
152 return _dbus_loop_add_timeout (context->loop, timeout);
156 remove_server_timeout (DBusTimeout *timeout,
159 DBusServer *server = data;
162 context = server_get_context (server);
164 _dbus_loop_remove_timeout (context->loop, timeout);
168 new_connection_callback (DBusServer *server,
169 DBusConnection *new_connection,
172 BusContext *context = data;
174 if (!bus_connections_setup_connection (context->connections, new_connection))
176 _dbus_verbose ("No memory to setup new connection\n");
178 /* if we don't do this, it will get unref'd without
179 * being disconnected... kind of strange really
180 * that we have to do this, people won't get it right
183 dbus_connection_close (new_connection);
186 dbus_connection_set_max_received_size (new_connection,
187 context->limits.max_incoming_bytes);
189 dbus_connection_set_max_message_size (new_connection,
190 context->limits.max_message_size);
192 dbus_connection_set_max_received_unix_fds (new_connection,
193 context->limits.max_incoming_unix_fds);
195 dbus_connection_set_max_message_unix_fds (new_connection,
196 context->limits.max_message_unix_fds);
198 dbus_connection_set_allow_anonymous (new_connection,
199 context->allow_anonymous);
201 /* on OOM, we won't have ref'd the connection so it will die. */
205 free_server_data (void *data)
207 BusServerData *bd = data;
213 setup_server (BusContext *context,
215 char **auth_mechanisms,
220 bd = dbus_new0 (BusServerData, 1);
221 if (bd == NULL || !dbus_server_set_data (server,
223 bd, free_server_data))
230 bd->context = context;
232 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
238 dbus_server_set_new_connection_function (server,
239 new_connection_callback,
242 if (!dbus_server_set_watch_functions (server,
253 if (!dbus_server_set_timeout_functions (server,
255 remove_server_timeout,
266 /* This code only gets executed the first time the
267 * config files are parsed. It is not executed
268 * when config files are reloaded.
271 process_config_first_time_only (BusContext *context,
272 BusConfigParser *parser,
273 const DBusString *address,
274 BusContextFlags flags,
277 DBusString log_prefix;
279 DBusList **addresses;
280 const char *user, *pidfile;
281 char **auth_mechanisms;
282 DBusList **auth_mechanisms_list;
286 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
289 auth_mechanisms = NULL;
292 _dbus_init_system_log (TRUE);
294 if (flags & BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION)
295 context->systemd_activation = TRUE;
297 context->systemd_activation = FALSE;
299 /* Check for an existing pid file. Of course this is a race;
300 * we'd have to use fcntl() locks on the pid file to
301 * avoid that. But we want to check for the pid file
302 * before overwriting any existing sockets, etc.
305 if (flags & BUS_CONTEXT_FLAG_WRITE_PID_FILE)
306 pidfile = bus_config_parser_get_pidfile (parser);
313 _dbus_string_init_const (&u, pidfile);
315 if (_dbus_stat (&u, &stbuf, NULL))
321 _dbus_string_init (&p);
322 _dbus_file_get_contents(&p, &u, NULL);
323 _dbus_string_parse_int(&p, 0, &pid, NULL);
324 _dbus_string_free(&p);
326 if ((kill((int)pid, 0))) {
327 dbus_set_error(NULL, DBUS_ERROR_FILE_EXISTS,
328 "pid %ld not running, removing stale pid file\n",
330 _dbus_delete_file(&u, NULL);
333 dbus_set_error (error, DBUS_ERROR_FAILED,
334 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
343 /* keep around the pid filename so we can delete it later */
344 context->pidfile = _dbus_strdup (pidfile);
346 /* note that type may be NULL */
347 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
348 if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
351 user = bus_config_parser_get_user (parser);
354 context->user = _dbus_strdup (user);
355 if (context->user == NULL)
359 /* Set up the prefix for syslog messages */
360 if (!_dbus_string_init (&log_prefix))
362 if (context->type && !strcmp (context->type, "system"))
364 if (!_dbus_string_append (&log_prefix, "[system] "))
367 else if (context->type && !strcmp (context->type, "session"))
369 DBusCredentials *credentials;
371 credentials = _dbus_credentials_new_from_current_process ();
374 if (!_dbus_string_append (&log_prefix, "[session "))
376 _dbus_credentials_unref (credentials);
379 if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
381 _dbus_credentials_unref (credentials);
384 if (!_dbus_string_append (&log_prefix, "] "))
386 _dbus_credentials_unref (credentials);
389 _dbus_credentials_unref (credentials);
391 if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
393 _dbus_string_free (&log_prefix);
395 /* Build an array of auth mechanisms */
397 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
398 len = _dbus_list_get_length (auth_mechanisms_list);
404 auth_mechanisms = dbus_new0 (char*, len + 1);
405 if (auth_mechanisms == NULL)
409 link = _dbus_list_get_first_link (auth_mechanisms_list);
412 auth_mechanisms[i] = _dbus_strdup (link->data);
413 if (auth_mechanisms[i] == NULL)
415 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
421 auth_mechanisms = NULL;
424 /* Listen on our addresses */
430 server = dbus_server_listen (_dbus_string_get_const_data(address), error);
433 _DBUS_ASSERT_ERROR_IS_SET (error);
436 else if (!setup_server (context, server, auth_mechanisms, error))
438 _DBUS_ASSERT_ERROR_IS_SET (error);
442 if (!_dbus_list_append (&context->servers, server))
447 addresses = bus_config_parser_get_addresses (parser);
449 link = _dbus_list_get_first_link (addresses);
454 server = dbus_server_listen (link->data, error);
457 _DBUS_ASSERT_ERROR_IS_SET (error);
460 else if (!setup_server (context, server, auth_mechanisms, error))
462 _DBUS_ASSERT_ERROR_IS_SET (error);
466 if (!_dbus_list_append (&context->servers, server))
469 link = _dbus_list_get_next_link (addresses, link);
473 context->fork = bus_config_parser_get_fork (parser);
474 context->syslog = bus_config_parser_get_syslog (parser);
475 context->keep_umask = bus_config_parser_get_keep_umask (parser);
476 context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
478 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
482 dbus_free_string_array (auth_mechanisms);
487 dbus_free_string_array (auth_mechanisms);
491 /* This code gets executed every time the config files
492 * are parsed: both during BusContext construction
493 * and on reloads. This function is slightly screwy
494 * since it can do a "half reload" in out-of-memory
495 * situations. Realistically, unlikely to ever matter.
498 process_config_every_time (BusContext *context,
499 BusConfigParser *parser,
500 dbus_bool_t is_reload,
503 DBusString full_address;
507 const char *servicehelper;
512 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
517 if (!_dbus_string_init (&full_address))
523 /* get our limits and timeout lengths */
524 bus_config_parser_get_limits (parser, &context->limits);
527 bus_policy_unref (context->policy);
528 context->policy = bus_config_parser_steal_policy (parser);
529 _dbus_assert (context->policy != NULL);
531 /* context->connections is NULL when creating new BusContext */
532 if (context->connections)
534 _dbus_verbose ("Reload policy rules for completed connections\n");
535 retval = bus_connections_reload_policy (context->connections, error);
538 _DBUS_ASSERT_ERROR_IS_SET (error);
543 /* We have to build the address backward, so that
544 * <listen> later in the config file have priority
546 link = _dbus_list_get_last_link (&context->servers);
549 addr = dbus_server_get_address (link->data);
556 if (_dbus_string_get_length (&full_address) > 0)
558 if (!_dbus_string_append (&full_address, ";"))
565 if (!_dbus_string_append (&full_address, addr))
574 link = _dbus_list_get_prev_link (&context->servers, link);
578 dbus_free (context->address);
580 if (!_dbus_string_copy_data (&full_address, &context->address))
586 /* get the service directories */
587 dirs = bus_config_parser_get_service_dirs (parser);
589 /* and the service helper */
590 servicehelper = bus_config_parser_get_servicehelper (parser);
592 s = _dbus_strdup(servicehelper);
593 if (s == NULL && servicehelper != NULL)
600 dbus_free(context->servicehelper);
601 context->servicehelper = s;
604 /* Create activation subsystem */
605 if (context->activation)
607 if (!bus_activation_reload (context->activation, &full_address, dirs, error))
612 context->activation = bus_activation_new (context, &full_address, dirs, error);
615 if (context->activation == NULL)
617 _DBUS_ASSERT_ERROR_IS_SET (error);
621 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
625 _dbus_string_free (&full_address);
634 list_concat_new (DBusList **a,
642 for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
644 if (!_dbus_list_append (result, link->data))
647 for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
649 if (!_dbus_list_append (result, link->data))
655 _dbus_list_clear (result);
660 raise_file_descriptor_limit (BusContext *context)
663 /* I just picked this out of thin air; we need some extra
664 * descriptors for things like any internal pipes we create,
665 * inotify, connections to SELinux, etc.
667 unsigned int arbitrary_extra_fds = 32;
670 limit = context->limits.max_completed_connections +
671 context->limits.max_incomplete_connections
672 + arbitrary_extra_fds;
674 _dbus_request_file_descriptor_limit (limit);
678 process_config_postinit (BusContext *context,
679 BusConfigParser *parser,
682 DBusHashTable *service_context_table;
683 DBusList *watched_dirs = NULL;
685 raise_file_descriptor_limit (context);
687 service_context_table = bus_config_parser_steal_service_context_table (parser);
688 if (!bus_registry_set_service_context_table (context->registry,
689 service_context_table))
695 _dbus_hash_table_unref (service_context_table);
697 /* We need to monitor both the configuration directories and directories
698 * containing .service files.
700 if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
701 bus_config_parser_get_service_dirs (parser),
708 bus_set_watched_dirs (context, &watched_dirs);
710 _dbus_list_clear (&watched_dirs);
716 bus_context_new (const DBusString *config_file,
717 BusContextFlags flags,
718 DBusPipe *print_addr_pipe,
719 DBusPipe *print_pid_pipe,
720 const DBusString *address,
724 BusConfigParser *parser;
726 _dbus_assert ((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 ||
727 (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS) == 0);
729 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
734 if (!dbus_server_allocate_data_slot (&server_data_slot))
740 context = dbus_new0 (BusContext, 1);
746 context->refcount = 1;
748 _dbus_generate_uuid (&context->uuid);
750 if (!_dbus_string_copy_data (config_file, &context->config_file))
756 context->loop = _dbus_loop_new ();
757 if (context->loop == NULL)
763 context->watches_enabled = TRUE;
765 context->registry = bus_registry_new (context);
766 if (context->registry == NULL)
772 parser = bus_config_load (config_file, TRUE, NULL, error);
775 _DBUS_ASSERT_ERROR_IS_SET (error);
779 if (!process_config_first_time_only (context, parser, address, flags, error))
781 _DBUS_ASSERT_ERROR_IS_SET (error);
784 if (!process_config_every_time (context, parser, FALSE, error))
786 _DBUS_ASSERT_ERROR_IS_SET (error);
790 /* we need another ref of the server data slot for the context
793 if (!dbus_server_allocate_data_slot (&server_data_slot))
794 _dbus_assert_not_reached ("second ref of server data slot failed");
796 /* Note that we don't know whether the print_addr_pipe is
797 * one of the sockets we're using to listen on, or some
798 * other random thing. But I think the answer is "don't do
801 if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
804 const char *a = bus_context_get_address (context);
807 _dbus_assert (a != NULL);
808 if (!_dbus_string_init (&addr))
814 if (!_dbus_string_append (&addr, a) ||
815 !_dbus_string_append (&addr, "\n"))
817 _dbus_string_free (&addr);
822 bytes = _dbus_string_get_length (&addr);
823 if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
825 /* pipe write returns an error on failure but not short write */
826 if (error != NULL && !dbus_error_is_set (error))
828 dbus_set_error (error, DBUS_ERROR_FAILED,
829 "Printing message bus address: did not write all bytes\n");
831 _dbus_string_free (&addr);
835 if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
836 _dbus_pipe_close (print_addr_pipe, NULL);
838 _dbus_string_free (&addr);
841 context->connections = bus_connections_new (context);
842 if (context->connections == NULL)
848 context->matchmaker = bus_matchmaker_new ();
849 if (context->matchmaker == NULL)
855 /* check user before we fork */
856 if (context->user != NULL)
858 if (!_dbus_verify_daemon_user (context->user))
860 dbus_set_error (error, DBUS_ERROR_FAILED,
861 "Could not get UID and GID for username \"%s\"",
867 /* Now become a daemon if appropriate and write out pid file in any case */
871 if (context->pidfile)
872 _dbus_string_init_const (&u, context->pidfile);
874 if (((flags & BUS_CONTEXT_FLAG_FORK_NEVER) == 0 && context->fork) ||
875 (flags & BUS_CONTEXT_FLAG_FORK_ALWAYS))
877 _dbus_verbose ("Forking and becoming daemon\n");
879 if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
882 context->keep_umask))
884 _DBUS_ASSERT_ERROR_IS_SET (error);
890 _dbus_verbose ("Fork not requested\n");
892 /* Need to write PID file and to PID pipe for ourselves,
893 * not for the child process. This is a no-op if the pidfile
894 * is NULL and print_pid_pipe is NULL.
896 if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
901 _DBUS_ASSERT_ERROR_IS_SET (error);
907 if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
908 !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
909 _dbus_pipe_close (print_pid_pipe, NULL);
911 if (!bus_selinux_full_init ())
913 bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but D-Bus initialization failed; check system log\n");
916 if (!process_config_postinit (context, parser, error))
918 _DBUS_ASSERT_ERROR_IS_SET (error);
924 bus_config_parser_unref (parser);
928 /* Here we change our credentials if required,
929 * as soon as we've set up our sockets and pidfile
931 if (context->user != NULL)
933 if (!_dbus_change_to_daemon_user (context->user, error))
935 _DBUS_ASSERT_ERROR_IS_SET (error);
940 /* FIXME - why not just put this in full_init() below? */
941 bus_selinux_audit_init ();
945 dbus_server_free_data_slot (&server_data_slot);
951 bus_config_parser_unref (parser);
953 bus_context_unref (context);
955 if (server_data_slot >= 0)
956 dbus_server_free_data_slot (&server_data_slot);
962 bus_context_get_id (BusContext *context,
965 return _dbus_uuid_encode (&context->uuid, uuid);
969 bus_context_reload_config (BusContext *context,
972 BusConfigParser *parser;
973 DBusString config_file;
976 /* Flush the user database cache */
977 _dbus_flush_caches ();
980 _dbus_string_init_const (&config_file, context->config_file);
981 parser = bus_config_load (&config_file, TRUE, NULL, error);
984 _DBUS_ASSERT_ERROR_IS_SET (error);
988 if (!process_config_every_time (context, parser, TRUE, error))
990 _DBUS_ASSERT_ERROR_IS_SET (error);
993 if (!process_config_postinit (context, parser, error))
995 _DBUS_ASSERT_ERROR_IS_SET (error);
1000 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
1003 bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
1005 bus_config_parser_unref (parser);
1010 shutdown_server (BusContext *context,
1013 if (server == NULL ||
1014 !dbus_server_get_is_connected (server))
1017 if (!dbus_server_set_watch_functions (server,
1021 _dbus_assert_not_reached ("setting watch functions to NULL failed");
1023 if (!dbus_server_set_timeout_functions (server,
1027 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
1029 dbus_server_disconnect (server);
1033 bus_context_shutdown (BusContext *context)
1037 link = _dbus_list_get_first_link (&context->servers);
1038 while (link != NULL)
1040 shutdown_server (context, link->data);
1042 link = _dbus_list_get_next_link (&context->servers, link);
1047 bus_context_ref (BusContext *context)
1049 _dbus_assert (context->refcount > 0);
1050 context->refcount += 1;
1056 bus_context_unref (BusContext *context)
1058 _dbus_assert (context->refcount > 0);
1059 context->refcount -= 1;
1061 if (context->refcount == 0)
1065 _dbus_verbose ("Finalizing bus context %p\n", context);
1067 bus_context_shutdown (context);
1069 if (context->connections)
1071 bus_connections_unref (context->connections);
1072 context->connections = NULL;
1075 if (context->registry)
1077 bus_registry_unref (context->registry);
1078 context->registry = NULL;
1081 if (context->activation)
1083 bus_activation_unref (context->activation);
1084 context->activation = NULL;
1087 link = _dbus_list_get_first_link (&context->servers);
1088 while (link != NULL)
1090 dbus_server_unref (link->data);
1092 link = _dbus_list_get_next_link (&context->servers, link);
1094 _dbus_list_clear (&context->servers);
1096 if (context->policy)
1098 bus_policy_unref (context->policy);
1099 context->policy = NULL;
1104 _dbus_loop_unref (context->loop);
1105 context->loop = NULL;
1108 if (context->matchmaker)
1110 bus_matchmaker_unref (context->matchmaker);
1111 context->matchmaker = NULL;
1114 dbus_free (context->config_file);
1115 dbus_free (context->log_prefix);
1116 dbus_free (context->type);
1117 dbus_free (context->address);
1118 dbus_free (context->user);
1119 dbus_free (context->servicehelper);
1121 if (context->pidfile)
1124 _dbus_string_init_const (&u, context->pidfile);
1126 /* Deliberately ignore errors here, since there's not much
1127 * we can do about it, and we're exiting anyways.
1129 _dbus_delete_file (&u, NULL);
1131 dbus_free (context->pidfile);
1133 dbus_free (context);
1135 dbus_server_free_data_slot (&server_data_slot);
1139 /* type may be NULL */
1141 bus_context_get_type (BusContext *context)
1143 return context->type;
1147 bus_context_get_address (BusContext *context)
1149 return context->address;
1153 bus_context_get_servicehelper (BusContext *context)
1155 return context->servicehelper;
1159 bus_context_get_systemd_activation (BusContext *context)
1161 return context->systemd_activation;
1165 bus_context_get_registry (BusContext *context)
1167 return context->registry;
1171 bus_context_get_connections (BusContext *context)
1173 return context->connections;
1177 bus_context_get_activation (BusContext *context)
1179 return context->activation;
1183 bus_context_get_matchmaker (BusContext *context)
1185 return context->matchmaker;
1189 bus_context_get_loop (BusContext *context)
1191 return context->loop;
1195 bus_context_allow_unix_user (BusContext *context,
1198 return bus_policy_allow_unix_user (context->policy,
1202 /* For now this is never actually called because the default
1203 * DBusConnection behavior of 'same user that owns the bus can connect'
1204 * is all it would do.
1207 bus_context_allow_windows_user (BusContext *context,
1208 const char *windows_sid)
1210 return bus_policy_allow_windows_user (context->policy,
1215 bus_context_get_policy (BusContext *context)
1217 return context->policy;
1221 bus_context_create_client_policy (BusContext *context,
1222 DBusConnection *connection,
1225 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1226 return bus_policy_create_client_policy (context->policy, connection,
1231 bus_context_get_activation_timeout (BusContext *context)
1234 return context->limits.activation_timeout;
1238 bus_context_get_auth_timeout (BusContext *context)
1240 return context->limits.auth_timeout;
1244 bus_context_get_pending_fd_timeout (BusContext *context)
1246 return context->limits.pending_fd_timeout;
1250 bus_context_get_max_completed_connections (BusContext *context)
1252 return context->limits.max_completed_connections;
1256 bus_context_get_max_incomplete_connections (BusContext *context)
1258 return context->limits.max_incomplete_connections;
1262 bus_context_get_max_connections_per_user (BusContext *context)
1264 return context->limits.max_connections_per_user;
1268 bus_context_get_max_pending_activations (BusContext *context)
1270 return context->limits.max_pending_activations;
1274 bus_context_get_max_services_per_connection (BusContext *context)
1276 return context->limits.max_services_per_connection;
1280 bus_context_get_max_match_rules_per_connection (BusContext *context)
1282 return context->limits.max_match_rules_per_connection;
1286 bus_context_get_max_replies_per_connection (BusContext *context)
1288 return context->limits.max_replies_per_connection;
1292 bus_context_get_reply_timeout (BusContext *context)
1294 return context->limits.reply_timeout;
1298 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
1301 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1305 if (!context->syslog)
1307 /* we're not syslogging; just output to stderr */
1308 va_start (args, msg);
1309 vfprintf (stderr, msg, args);
1310 fprintf (stderr, "\n");
1315 va_start (args, msg);
1317 if (context->log_prefix)
1319 DBusString full_msg;
1321 if (!_dbus_string_init (&full_msg))
1323 if (!_dbus_string_append (&full_msg, context->log_prefix))
1325 if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1328 _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1330 _dbus_string_free (&full_msg);
1333 _dbus_system_logv (severity, msg, args);
1339 static inline const char *
1340 nonnull (const char *maybe_null,
1341 const char *if_null)
1343 return (maybe_null ? maybe_null : if_null);
1347 * Log something about a message, usually that it was rejected.
1350 complain_about_message (BusContext *context,
1351 const char *error_name,
1352 const char *complaint,
1354 DBusMessage *message,
1355 DBusConnection *sender,
1356 DBusConnection *proposed_recipient,
1357 dbus_bool_t requested_reply,
1361 DBusError stack_error = DBUS_ERROR_INIT;
1362 const char *sender_name;
1363 const char *sender_loginfo;
1364 const char *proposed_recipient_loginfo;
1366 if (error == NULL && !log)
1371 sender_name = bus_connection_get_name (sender);
1372 sender_loginfo = bus_connection_get_loginfo (sender);
1376 sender_name = "(unset)";
1377 sender_loginfo = "(bus)";
1380 if (proposed_recipient != NULL)
1381 proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1383 proposed_recipient_loginfo = "bus";
1385 dbus_set_error (&stack_error, error_name,
1386 "%s, %d matched rules; type=\"%s\", sender=\"%s\" (%s) "
1387 "interface=\"%s\" member=\"%s\" error name=\"%s\" "
1388 "requested_reply=\"%d\" destination=\"%s\" (%s)",
1391 dbus_message_type_to_string (dbus_message_get_type (message)),
1394 nonnull (dbus_message_get_interface (message), "(unset)"),
1395 nonnull (dbus_message_get_member (message), "(unset)"),
1396 nonnull (dbus_message_get_error_name (message), "(unset)"),
1398 nonnull (dbus_message_get_destination (message), DBUS_SERVICE_DBUS),
1399 proposed_recipient_loginfo);
1401 /* If we hit OOM while setting the error, this will syslog "out of memory"
1402 * which is itself an indication that something is seriously wrong */
1404 bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, "%s",
1405 stack_error.message);
1407 dbus_move_error (&stack_error, error);
1411 * addressed_recipient is the recipient specified in the message.
1413 * proposed_recipient is the recipient we're considering sending
1414 * to right this second, and may be an eavesdropper.
1416 * sender is the sender of the message.
1418 * NULL for proposed_recipient or sender definitely means the bus driver.
1420 * NULL for addressed_recipient may mean the bus driver, or may mean
1421 * no destination was specified in the message (e.g. a signal).
1424 bus_context_check_security_policy (BusContext *context,
1425 BusTransaction *transaction,
1426 DBusConnection *sender,
1427 DBusConnection *addressed_recipient,
1428 DBusConnection *proposed_recipient,
1429 DBusMessage *message,
1433 BusClientPolicy *sender_policy;
1434 BusClientPolicy *recipient_policy;
1435 dbus_int32_t toggles;
1438 dbus_bool_t requested_reply;
1440 type = dbus_message_get_type (message);
1441 dest = dbus_message_get_destination (message);
1443 /* dispatch.c was supposed to ensure these invariants */
1444 _dbus_assert (dest != NULL ||
1445 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1446 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1447 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1448 addressed_recipient != NULL ||
1449 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1453 case DBUS_MESSAGE_TYPE_METHOD_CALL:
1454 case DBUS_MESSAGE_TYPE_SIGNAL:
1455 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1456 case DBUS_MESSAGE_TYPE_ERROR:
1460 _dbus_verbose ("security check disallowing message of unknown type %d\n",
1463 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1464 "Message bus will not accept messages of unknown type\n");
1469 requested_reply = FALSE;
1473 /* First verify the SELinux access controls. If allowed then
1474 * go on with the standard checks.
1476 if (!bus_selinux_allows_send (sender, proposed_recipient,
1477 dbus_message_type_to_string (dbus_message_get_type (message)),
1478 dbus_message_get_interface (message),
1479 dbus_message_get_member (message),
1480 dbus_message_get_error_name (message),
1481 dest ? dest : DBUS_SERVICE_DBUS, error))
1483 if (error != NULL && !dbus_error_is_set (error))
1485 /* don't syslog this, just set the error: avc_has_perm should
1486 * have already written to either the audit log or syslog */
1487 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1488 "An SELinux policy prevents this sender from sending this "
1489 "message to this recipient",
1490 0, message, sender, proposed_recipient, FALSE, FALSE, error);
1491 _dbus_verbose ("SELinux security check denying send to service\n");
1497 if (bus_connection_is_active (sender))
1499 sender_policy = bus_connection_get_policy (sender);
1500 _dbus_assert (sender_policy != NULL);
1502 /* Fill in requested_reply variable with TRUE if this is a
1503 * reply and the reply was pending.
1505 if (dbus_message_get_reply_serial (message) != 0)
1507 if (proposed_recipient != NULL /* not to the bus driver */ &&
1508 addressed_recipient == proposed_recipient /* not eavesdropping */)
1512 dbus_error_init (&error2);
1513 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1515 sender, addressed_recipient, message,
1517 if (dbus_error_is_set (&error2))
1519 dbus_move_error (&error2, error);
1527 /* Policy for inactive connections is that they can only send
1528 * the hello message to the bus driver
1530 if (proposed_recipient == NULL &&
1531 dbus_message_is_method_call (message,
1532 DBUS_INTERFACE_DBUS,
1535 _dbus_verbose ("security check allowing %s message\n",
1541 _dbus_verbose ("security check disallowing non-%s message\n",
1544 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1545 "Client tried to send a message other than %s without being registered",
1554 sender_policy = NULL;
1556 /* If the sender is the bus driver, we assume any reply was a
1557 * requested reply as bus driver won't send bogus ones
1559 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1560 dbus_message_get_reply_serial (message) != 0)
1561 requested_reply = TRUE;
1564 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1565 (sender == NULL && sender_policy == NULL));
1567 if (proposed_recipient != NULL)
1569 /* only the bus driver can send to an inactive recipient (as it
1570 * owns no services, so other apps can't address it). Inactive
1571 * recipients can receive any message.
1573 if (bus_connection_is_active (proposed_recipient))
1575 recipient_policy = bus_connection_get_policy (proposed_recipient);
1576 _dbus_assert (recipient_policy != NULL);
1578 else if (sender == NULL)
1580 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1581 recipient_policy = NULL;
1585 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1586 recipient_policy = NULL;
1590 recipient_policy = NULL;
1592 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1593 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1594 (proposed_recipient == NULL && recipient_policy == NULL));
1597 if (sender_policy &&
1598 !bus_client_policy_check_can_send (sender_policy,
1602 message, &toggles, &log))
1604 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1605 "Rejected send message", toggles,
1606 message, sender, proposed_recipient, requested_reply,
1607 (addressed_recipient == proposed_recipient), error);
1608 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1614 /* We want to drop this message, and are only not doing so for backwards
1616 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1617 "Would reject message", toggles,
1618 message, sender, proposed_recipient, requested_reply,
1622 if (recipient_policy &&
1623 !bus_client_policy_check_can_receive (recipient_policy,
1627 addressed_recipient, proposed_recipient,
1630 complain_about_message (context, DBUS_ERROR_ACCESS_DENIED,
1631 "Rejected receive message", toggles,
1632 message, sender, proposed_recipient, requested_reply,
1633 (addressed_recipient == proposed_recipient), NULL);
1634 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1638 /* See if limits on size have been exceeded */
1639 if (proposed_recipient &&
1640 ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1641 (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1643 complain_about_message (context, DBUS_ERROR_LIMITS_EXCEEDED,
1644 "Rejected: destination has a full message queue",
1645 0, message, sender, proposed_recipient, requested_reply, TRUE,
1647 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1651 /* Record that we will allow a reply here in the future (don't
1652 * bother if the recipient is the bus or this is an eavesdropping
1653 * connection). Only the addressed recipient may reply.
1655 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1657 addressed_recipient &&
1658 addressed_recipient == proposed_recipient && /* not eavesdropping */
1659 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1661 sender, addressed_recipient,
1664 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1668 _dbus_verbose ("security policy allowing message\n");
1673 bus_context_check_all_watches (BusContext *context)
1676 dbus_bool_t enabled = TRUE;
1678 if (bus_connections_get_n_incomplete (context->connections) >=
1679 bus_context_get_max_incomplete_connections (context))
1684 if (context->watches_enabled == enabled)
1687 context->watches_enabled = enabled;
1689 for (link = _dbus_list_get_first_link (&context->servers);
1691 link = _dbus_list_get_next_link (&context->servers, link))
1693 /* A BusContext might contains several DBusServer (if there are
1694 * several <listen> configuration items) and a DBusServer might
1695 * contain several DBusWatch in its DBusWatchList (if getaddrinfo
1696 * returns several addresses on a dual IPv4-IPv6 stack or if
1697 * systemd passes several fds).
1698 * We want to enable/disable them all.
1700 DBusServer *server = link->data;
1701 _dbus_server_toggle_all_watches (server, enabled);