1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* bus.c message bus context object
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "activation.h"
26 #include "connection.h"
30 #include "config-parser.h"
31 #include <dbus/dbus-list.h>
32 #include <dbus/dbus-hash.h>
33 #include <dbus/dbus-internals.h>
43 BusConnections *connections;
44 BusActivation *activation;
45 BusRegistry *registry;
47 DBusUserDatabase *user_database;
51 static int server_data_slot = -1;
52 static int server_data_slot_refcount = 0;
59 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
62 server_data_slot_ref (void)
64 if (server_data_slot < 0)
66 server_data_slot = dbus_server_allocate_data_slot ();
68 if (server_data_slot < 0)
71 _dbus_assert (server_data_slot_refcount == 0);
74 server_data_slot_refcount += 1;
80 server_data_slot_unref (void)
82 _dbus_assert (server_data_slot_refcount > 0);
84 server_data_slot_refcount -= 1;
86 if (server_data_slot_refcount == 0)
88 dbus_server_free_data_slot (server_data_slot);
89 server_data_slot = -1;
94 server_get_context (DBusServer *server)
99 if (!server_data_slot_ref ())
102 bd = BUS_SERVER_DATA (server);
105 server_data_slot_unref ();
109 context = bd->context;
111 server_data_slot_unref ();
117 server_watch_callback (DBusWatch *watch,
118 unsigned int condition,
121 /* FIXME this can be done in dbus-mainloop.c
122 * if the code in activation.c for the babysitter
123 * watch handler is fixed.
126 return dbus_watch_handle (watch, condition);
130 add_server_watch (DBusWatch *watch,
133 DBusServer *server = data;
136 context = server_get_context (server);
138 return _dbus_loop_add_watch (context->loop,
139 watch, server_watch_callback, server,
144 remove_server_watch (DBusWatch *watch,
147 DBusServer *server = data;
150 context = server_get_context (server);
152 _dbus_loop_remove_watch (context->loop,
153 watch, server_watch_callback, server);
158 server_timeout_callback (DBusTimeout *timeout,
161 /* can return FALSE on OOM but we just let it fire again later */
162 dbus_timeout_handle (timeout);
166 add_server_timeout (DBusTimeout *timeout,
169 DBusServer *server = data;
172 context = server_get_context (server);
174 return _dbus_loop_add_timeout (context->loop,
175 timeout, server_timeout_callback, server, NULL);
179 remove_server_timeout (DBusTimeout *timeout,
182 DBusServer *server = data;
185 context = server_get_context (server);
187 _dbus_loop_remove_timeout (context->loop,
188 timeout, server_timeout_callback, server);
192 new_connection_callback (DBusServer *server,
193 DBusConnection *new_connection,
196 BusContext *context = data;
198 if (!bus_connections_setup_connection (context->connections, new_connection))
200 _dbus_verbose ("No memory to setup new connection\n");
202 /* if we don't do this, it will get unref'd without
203 * being disconnected... kind of strange really
204 * that we have to do this, people won't get it right
207 dbus_connection_disconnect (new_connection);
210 dbus_connection_set_max_received_size (new_connection,
211 context->limits.max_incoming_bytes);
213 dbus_connection_set_max_message_size (new_connection,
214 context->limits.max_message_size);
216 /* on OOM, we won't have ref'd the connection so it will die. */
220 free_server_data (void *data)
222 BusServerData *bd = data;
228 setup_server (BusContext *context,
230 char **auth_mechanisms,
235 bd = dbus_new0 (BusServerData, 1);
236 if (!dbus_server_set_data (server,
238 bd, free_server_data))
245 bd->context = context;
247 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
253 dbus_server_set_new_connection_function (server,
254 new_connection_callback,
257 if (!dbus_server_set_watch_functions (server,
268 if (!dbus_server_set_timeout_functions (server,
270 remove_server_timeout,
282 bus_context_new (const DBusString *config_file,
283 dbus_bool_t force_fork,
290 DBusList **addresses;
291 BusConfigParser *parser;
292 DBusString full_address;
293 const char *user, *pidfile;
294 char **auth_mechanisms;
295 DBusList **auth_mechanisms_list;
298 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
300 if (!_dbus_string_init (&full_address))
306 if (!server_data_slot_ref ())
309 _dbus_string_free (&full_address);
315 auth_mechanisms = NULL;
317 parser = bus_config_load (config_file, TRUE, error);
321 /* Check for an existing pid file. Of course this is a race;
322 * we'd have to use fcntl() locks on the pid file to
323 * avoid that. But we want to check for the pid file
324 * before overwriting any existing sockets, etc.
326 pidfile = bus_config_parser_get_pidfile (parser);
333 dbus_error_init (&tmp_error);
334 _dbus_string_init_const (&u, pidfile);
336 if (_dbus_stat (&u, &stbuf, &tmp_error))
338 dbus_set_error (error, DBUS_ERROR_FAILED,
339 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
341 dbus_error_free (&tmp_error);
346 context = dbus_new0 (BusContext, 1);
353 context->refcount = 1;
355 /* get our limits and timeout lengths */
356 bus_config_parser_get_limits (parser, &context->limits);
358 /* we need another ref of the server data slot for the context
361 if (!server_data_slot_ref ())
362 _dbus_assert_not_reached ("second ref of server data slot failed");
364 context->user_database = _dbus_user_database_new ();
365 if (context->user_database == NULL)
371 context->loop = _dbus_loop_new ();
372 if (context->loop == NULL)
378 /* Build an array of auth mechanisms */
380 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
381 len = _dbus_list_get_length (auth_mechanisms_list);
387 auth_mechanisms = dbus_new0 (char*, len + 1);
388 if (auth_mechanisms == NULL)
392 link = _dbus_list_get_first_link (auth_mechanisms_list);
395 auth_mechanisms[i] = _dbus_strdup (link->data);
396 if (auth_mechanisms[i] == NULL)
398 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
403 auth_mechanisms = NULL;
406 /* Listen on our addresses */
408 addresses = bus_config_parser_get_addresses (parser);
410 link = _dbus_list_get_first_link (addresses);
415 server = dbus_server_listen (link->data, error);
418 else if (!setup_server (context, server, auth_mechanisms, error))
421 if (!_dbus_list_append (&context->servers, server))
427 link = _dbus_list_get_next_link (addresses, link);
430 /* note that type may be NULL */
431 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
433 /* We have to build the address backward, so that
434 * <listen> later in the config file have priority
436 link = _dbus_list_get_last_link (&context->servers);
441 addr = dbus_server_get_address (link->data);
448 if (_dbus_string_get_length (&full_address) > 0)
450 if (!_dbus_string_append (&full_address, ";"))
457 if (!_dbus_string_append (&full_address, addr))
465 link = _dbus_list_get_prev_link (&context->servers, link);
468 if (!_dbus_string_copy_data (&full_address, &context->address))
474 /* Note that we don't know whether the print_addr_fd is
475 * one of the sockets we're using to listen on, or some
476 * other random thing. But I think the answer is "don't do
479 if (print_addr_fd >= 0)
482 const char *a = bus_context_get_address (context);
485 _dbus_assert (a != NULL);
486 if (!_dbus_string_init (&addr))
492 if (!_dbus_string_append (&addr, a) ||
493 !_dbus_string_append (&addr, "\n"))
495 _dbus_string_free (&addr);
500 bytes = _dbus_string_get_length (&addr);
501 if (_dbus_write (print_addr_fd, &addr, 0, bytes) != bytes)
503 dbus_set_error (error, DBUS_ERROR_FAILED,
504 "Printing message bus address: %s\n",
505 _dbus_strerror (errno));
506 _dbus_string_free (&addr);
510 if (print_addr_fd > 2)
511 _dbus_close (print_addr_fd, NULL);
513 _dbus_string_free (&addr);
516 /* Create activation subsystem */
518 context->activation = bus_activation_new (context, &full_address,
519 bus_config_parser_get_service_dirs (parser),
521 if (context->activation == NULL)
523 _DBUS_ASSERT_ERROR_IS_SET (error);
527 context->connections = bus_connections_new (context);
528 if (context->connections == NULL)
534 context->registry = bus_registry_new (context);
535 if (context->registry == NULL)
541 context->policy = bus_config_parser_steal_policy (parser);
542 _dbus_assert (context->policy != NULL);
544 /* Now become a daemon if appropriate */
545 if (force_fork || bus_config_parser_get_fork (parser))
550 _dbus_string_init_const (&u, pidfile);
552 if (!_dbus_become_daemon (pidfile ? &u : NULL, error))
557 /* Need to write PID file for ourselves, not for the child process */
562 _dbus_string_init_const (&u, pidfile);
564 if (!_dbus_write_pid_file (&u, _dbus_getpid (), error))
569 /* keep around the pid filename so we can delete it later */
570 context->pidfile = _dbus_strdup (pidfile);
572 /* Write PID if requested */
573 if (print_pid_fd >= 0)
578 if (!_dbus_string_init (&pid))
584 if (!_dbus_string_append_int (&pid, _dbus_getpid ()) ||
585 !_dbus_string_append (&pid, "\n"))
587 _dbus_string_free (&pid);
592 bytes = _dbus_string_get_length (&pid);
593 if (_dbus_write (print_pid_fd, &pid, 0, bytes) != bytes)
595 dbus_set_error (error, DBUS_ERROR_FAILED,
596 "Printing message bus PID: %s\n",
597 _dbus_strerror (errno));
598 _dbus_string_free (&pid);
602 if (print_pid_fd > 2)
603 _dbus_close (print_pid_fd, NULL);
605 _dbus_string_free (&pid);
608 /* Here we change our credentials if required,
609 * as soon as we've set up our sockets and pidfile
611 user = bus_config_parser_get_user (parser);
614 DBusCredentials creds;
617 _dbus_string_init_const (&u, user);
619 if (!_dbus_credentials_from_username (&u, &creds) ||
623 dbus_set_error (error, DBUS_ERROR_FAILED,
624 "Could not get UID and GID for username \"%s\"",
629 if (!_dbus_change_identity (creds.uid, creds.gid, error))
633 bus_config_parser_unref (parser);
634 _dbus_string_free (&full_address);
635 dbus_free_string_array (auth_mechanisms);
636 server_data_slot_unref ();
642 bus_config_parser_unref (parser);
645 bus_context_unref (context);
647 _dbus_string_free (&full_address);
648 dbus_free_string_array (auth_mechanisms);
650 server_data_slot_unref ();
656 shutdown_server (BusContext *context,
659 if (server == NULL ||
660 !dbus_server_get_is_connected (server))
663 if (!dbus_server_set_watch_functions (server,
667 _dbus_assert_not_reached ("setting watch functions to NULL failed");
669 if (!dbus_server_set_timeout_functions (server,
673 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
675 dbus_server_disconnect (server);
679 bus_context_shutdown (BusContext *context)
683 link = _dbus_list_get_first_link (&context->servers);
686 shutdown_server (context, link->data);
688 link = _dbus_list_get_next_link (&context->servers, link);
693 bus_context_ref (BusContext *context)
695 _dbus_assert (context->refcount > 0);
696 context->refcount += 1;
700 bus_context_unref (BusContext *context)
702 _dbus_assert (context->refcount > 0);
703 context->refcount -= 1;
705 if (context->refcount == 0)
709 _dbus_verbose ("Finalizing bus context %p\n", context);
711 bus_context_shutdown (context);
713 if (context->connections)
715 bus_connections_unref (context->connections);
716 context->connections = NULL;
719 if (context->registry)
721 bus_registry_unref (context->registry);
722 context->registry = NULL;
725 if (context->activation)
727 bus_activation_unref (context->activation);
728 context->activation = NULL;
731 link = _dbus_list_get_first_link (&context->servers);
734 dbus_server_unref (link->data);
736 link = _dbus_list_get_next_link (&context->servers, link);
738 _dbus_list_clear (&context->servers);
742 bus_policy_unref (context->policy);
743 context->policy = NULL;
748 _dbus_loop_unref (context->loop);
749 context->loop = NULL;
752 dbus_free (context->type);
753 dbus_free (context->address);
755 if (context->pidfile)
758 _dbus_string_init_const (&u, context->pidfile);
760 /* Deliberately ignore errors here, since there's not much
761 * we can do about it, and we're exiting anyways.
763 _dbus_delete_file (&u, NULL);
765 dbus_free (context->pidfile);
768 _dbus_user_database_unref (context->user_database);
772 server_data_slot_unref ();
776 /* type may be NULL */
778 bus_context_get_type (BusContext *context)
780 return context->type;
784 bus_context_get_address (BusContext *context)
786 return context->address;
790 bus_context_get_registry (BusContext *context)
792 return context->registry;
796 bus_context_get_connections (BusContext *context)
798 return context->connections;
802 bus_context_get_activation (BusContext *context)
804 return context->activation;
808 bus_context_get_loop (BusContext *context)
810 return context->loop;
814 bus_context_get_user_database (BusContext *context)
816 return context->user_database;
820 bus_context_allow_user (BusContext *context,
823 return bus_policy_allow_user (context->policy,
824 context->user_database,
829 bus_context_create_client_policy (BusContext *context,
830 DBusConnection *connection,
833 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
834 return bus_policy_create_client_policy (context->policy, connection,
839 bus_context_get_activation_timeout (BusContext *context)
842 return context->limits.activation_timeout;
846 bus_context_get_auth_timeout (BusContext *context)
848 return context->limits.auth_timeout;
852 bus_context_get_max_completed_connections (BusContext *context)
854 return context->limits.max_completed_connections;
858 bus_context_get_max_incomplete_connections (BusContext *context)
860 return context->limits.max_incomplete_connections;
864 bus_context_get_max_connections_per_user (BusContext *context)
866 return context->limits.max_connections_per_user;
870 bus_context_get_max_pending_activations (BusContext *context)
872 return context->limits.max_pending_activations;
876 bus_context_get_max_services_per_connection (BusContext *context)
878 return context->limits.max_services_per_connection;
882 bus_context_check_security_policy (BusContext *context,
883 DBusConnection *sender,
884 DBusConnection *recipient,
885 DBusMessage *message,
888 BusClientPolicy *sender_policy;
889 BusClientPolicy *recipient_policy;
891 /* NULL sender/receiver means the bus driver */
895 if (bus_connection_is_active (sender))
897 sender_policy = bus_connection_get_policy (sender);
898 _dbus_assert (sender_policy != NULL);
902 /* Policy for inactive connections is that they can only send
903 * the hello message to the bus driver
905 if (recipient == NULL &&
906 dbus_message_has_name (message, DBUS_MESSAGE_HELLO))
908 _dbus_verbose ("security check allowing %s message\n",
914 _dbus_verbose ("security check disallowing non-%s message\n",
917 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
918 "Client tried to send a message other than %s without being registered",
926 sender_policy = NULL;
928 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
929 (sender == NULL && sender_policy == NULL));
931 if (recipient != NULL)
933 /* only the bus driver can send to an inactive recipient (as it
934 * owns no services, so other apps can't address it). Inactive
935 * recipients can receive any message.
937 if (bus_connection_is_active (recipient))
939 recipient_policy = bus_connection_get_policy (recipient);
940 _dbus_assert (recipient_policy != NULL);
942 else if (sender == NULL)
944 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
945 recipient_policy = NULL;
949 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
950 recipient_policy = NULL;
954 recipient_policy = NULL;
956 _dbus_assert ((recipient != NULL && recipient_policy != NULL) ||
957 (recipient != NULL && sender == NULL && recipient_policy == NULL) ||
958 (recipient == NULL && recipient_policy == NULL));
961 !bus_client_policy_check_can_send (sender_policy,
962 context->registry, recipient,
965 const char *dest = dbus_message_get_destination (message);
966 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
967 "A security policy in place prevents this sender "
968 "from sending this message to this recipient, "
969 "see message bus configuration file (rejected message "
970 "had name \"%s\" destination \"%s\")",
971 dbus_message_get_name (message),
972 dest ? dest : DBUS_SERVICE_DBUS);
976 if (recipient_policy &&
977 !bus_client_policy_check_can_receive (recipient_policy,
978 context->registry, sender,
981 const char *dest = dbus_message_get_destination (message);
982 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
983 "A security policy in place prevents this recipient "
984 "from receiving this message from this sender, "
985 "see message bus configuration file (rejected message "
986 "had name \"%s\" destination \"%s\")",
987 dbus_message_get_name (message),
988 dest ? dest : DBUS_SERVICE_DBUS);
992 /* See if limits on size have been exceeded */
994 dbus_connection_get_outgoing_size (recipient) >
995 context->limits.max_outgoing_bytes)
997 const char *dest = dbus_message_get_destination (message);
998 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
999 "The destination service \"%s\" has a full message queue",
1000 dest ? dest : DBUS_SERVICE_DBUS);