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"
32 #include <dbus/dbus-list.h>
33 #include <dbus/dbus-hash.h>
34 #include <dbus/dbus-internals.h>
44 BusConnections *connections;
45 BusActivation *activation;
46 BusRegistry *registry;
48 BusMatchmaker *matchmaker;
49 DBusUserDatabase *user_database;
53 static dbus_int32_t server_data_slot = -1;
60 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
63 server_get_context (DBusServer *server)
68 if (!dbus_server_allocate_data_slot (&server_data_slot))
71 bd = BUS_SERVER_DATA (server);
74 dbus_server_free_data_slot (&server_data_slot);
78 context = bd->context;
80 dbus_server_free_data_slot (&server_data_slot);
86 server_watch_callback (DBusWatch *watch,
87 unsigned int condition,
90 /* FIXME this can be done in dbus-mainloop.c
91 * if the code in activation.c for the babysitter
92 * watch handler is fixed.
95 return dbus_watch_handle (watch, condition);
99 add_server_watch (DBusWatch *watch,
102 DBusServer *server = data;
105 context = server_get_context (server);
107 return _dbus_loop_add_watch (context->loop,
108 watch, server_watch_callback, server,
113 remove_server_watch (DBusWatch *watch,
116 DBusServer *server = data;
119 context = server_get_context (server);
121 _dbus_loop_remove_watch (context->loop,
122 watch, server_watch_callback, server);
127 server_timeout_callback (DBusTimeout *timeout,
130 /* can return FALSE on OOM but we just let it fire again later */
131 dbus_timeout_handle (timeout);
135 add_server_timeout (DBusTimeout *timeout,
138 DBusServer *server = data;
141 context = server_get_context (server);
143 return _dbus_loop_add_timeout (context->loop,
144 timeout, server_timeout_callback, server, NULL);
148 remove_server_timeout (DBusTimeout *timeout,
151 DBusServer *server = data;
154 context = server_get_context (server);
156 _dbus_loop_remove_timeout (context->loop,
157 timeout, server_timeout_callback, server);
161 new_connection_callback (DBusServer *server,
162 DBusConnection *new_connection,
165 BusContext *context = data;
167 if (!bus_connections_setup_connection (context->connections, new_connection))
169 _dbus_verbose ("No memory to setup new connection\n");
171 /* if we don't do this, it will get unref'd without
172 * being disconnected... kind of strange really
173 * that we have to do this, people won't get it right
176 dbus_connection_disconnect (new_connection);
179 dbus_connection_set_max_received_size (new_connection,
180 context->limits.max_incoming_bytes);
182 dbus_connection_set_max_message_size (new_connection,
183 context->limits.max_message_size);
185 /* on OOM, we won't have ref'd the connection so it will die. */
189 free_server_data (void *data)
191 BusServerData *bd = data;
197 setup_server (BusContext *context,
199 char **auth_mechanisms,
204 bd = dbus_new0 (BusServerData, 1);
205 if (!dbus_server_set_data (server,
207 bd, free_server_data))
214 bd->context = context;
216 if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
222 dbus_server_set_new_connection_function (server,
223 new_connection_callback,
226 if (!dbus_server_set_watch_functions (server,
237 if (!dbus_server_set_timeout_functions (server,
239 remove_server_timeout,
251 bus_context_new (const DBusString *config_file,
252 dbus_bool_t force_fork,
259 DBusList **addresses;
260 BusConfigParser *parser;
261 DBusString full_address;
262 const char *user, *pidfile;
263 char **auth_mechanisms;
264 DBusList **auth_mechanisms_list;
267 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
269 if (!_dbus_string_init (&full_address))
275 if (!dbus_server_allocate_data_slot (&server_data_slot))
278 _dbus_string_free (&full_address);
284 auth_mechanisms = NULL;
286 parser = bus_config_load (config_file, TRUE, error);
290 /* Check for an existing pid file. Of course this is a race;
291 * we'd have to use fcntl() locks on the pid file to
292 * avoid that. But we want to check for the pid file
293 * before overwriting any existing sockets, etc.
295 pidfile = bus_config_parser_get_pidfile (parser);
302 dbus_error_init (&tmp_error);
303 _dbus_string_init_const (&u, pidfile);
305 if (_dbus_stat (&u, &stbuf, &tmp_error))
307 dbus_set_error (error, DBUS_ERROR_FAILED,
308 "The pid file \"%s\" exists, if the message bus is not running, remove this file",
310 dbus_error_free (&tmp_error);
315 context = dbus_new0 (BusContext, 1);
322 context->refcount = 1;
324 /* get our limits and timeout lengths */
325 bus_config_parser_get_limits (parser, &context->limits);
327 /* we need another ref of the server data slot for the context
330 if (!dbus_server_allocate_data_slot (&server_data_slot))
331 _dbus_assert_not_reached ("second ref of server data slot failed");
333 context->user_database = _dbus_user_database_new ();
334 if (context->user_database == NULL)
340 context->loop = _dbus_loop_new ();
341 if (context->loop == NULL)
347 /* Build an array of auth mechanisms */
349 auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
350 len = _dbus_list_get_length (auth_mechanisms_list);
356 auth_mechanisms = dbus_new0 (char*, len + 1);
357 if (auth_mechanisms == NULL)
361 link = _dbus_list_get_first_link (auth_mechanisms_list);
364 auth_mechanisms[i] = _dbus_strdup (link->data);
365 if (auth_mechanisms[i] == NULL)
367 link = _dbus_list_get_next_link (auth_mechanisms_list, link);
372 auth_mechanisms = NULL;
375 /* Listen on our addresses */
377 addresses = bus_config_parser_get_addresses (parser);
379 link = _dbus_list_get_first_link (addresses);
384 server = dbus_server_listen (link->data, error);
387 else if (!setup_server (context, server, auth_mechanisms, error))
390 if (!_dbus_list_append (&context->servers, server))
396 link = _dbus_list_get_next_link (addresses, link);
399 /* note that type may be NULL */
400 context->type = _dbus_strdup (bus_config_parser_get_type (parser));
402 /* We have to build the address backward, so that
403 * <listen> later in the config file have priority
405 link = _dbus_list_get_last_link (&context->servers);
410 addr = dbus_server_get_address (link->data);
417 if (_dbus_string_get_length (&full_address) > 0)
419 if (!_dbus_string_append (&full_address, ";"))
426 if (!_dbus_string_append (&full_address, addr))
434 link = _dbus_list_get_prev_link (&context->servers, link);
437 if (!_dbus_string_copy_data (&full_address, &context->address))
443 /* Note that we don't know whether the print_addr_fd is
444 * one of the sockets we're using to listen on, or some
445 * other random thing. But I think the answer is "don't do
448 if (print_addr_fd >= 0)
451 const char *a = bus_context_get_address (context);
454 _dbus_assert (a != NULL);
455 if (!_dbus_string_init (&addr))
461 if (!_dbus_string_append (&addr, a) ||
462 !_dbus_string_append (&addr, "\n"))
464 _dbus_string_free (&addr);
469 bytes = _dbus_string_get_length (&addr);
470 if (_dbus_write (print_addr_fd, &addr, 0, bytes) != bytes)
472 dbus_set_error (error, DBUS_ERROR_FAILED,
473 "Printing message bus address: %s\n",
474 _dbus_strerror (errno));
475 _dbus_string_free (&addr);
479 if (print_addr_fd > 2)
480 _dbus_close (print_addr_fd, NULL);
482 _dbus_string_free (&addr);
485 /* Create activation subsystem */
487 context->activation = bus_activation_new (context, &full_address,
488 bus_config_parser_get_service_dirs (parser),
490 if (context->activation == NULL)
492 _DBUS_ASSERT_ERROR_IS_SET (error);
496 context->connections = bus_connections_new (context);
497 if (context->connections == NULL)
503 context->registry = bus_registry_new (context);
504 if (context->registry == NULL)
510 context->matchmaker = bus_matchmaker_new ();
511 if (context->matchmaker == NULL)
517 context->policy = bus_config_parser_steal_policy (parser);
518 _dbus_assert (context->policy != NULL);
520 /* Now become a daemon if appropriate */
521 if (force_fork || bus_config_parser_get_fork (parser))
526 _dbus_string_init_const (&u, pidfile);
528 if (!_dbus_become_daemon (pidfile ? &u : NULL, error))
533 /* Need to write PID file for ourselves, not for the child process */
538 _dbus_string_init_const (&u, pidfile);
540 if (!_dbus_write_pid_file (&u, _dbus_getpid (), error))
545 /* keep around the pid filename so we can delete it later */
546 context->pidfile = _dbus_strdup (pidfile);
548 /* Write PID if requested */
549 if (print_pid_fd >= 0)
554 if (!_dbus_string_init (&pid))
560 if (!_dbus_string_append_int (&pid, _dbus_getpid ()) ||
561 !_dbus_string_append (&pid, "\n"))
563 _dbus_string_free (&pid);
568 bytes = _dbus_string_get_length (&pid);
569 if (_dbus_write (print_pid_fd, &pid, 0, bytes) != bytes)
571 dbus_set_error (error, DBUS_ERROR_FAILED,
572 "Printing message bus PID: %s\n",
573 _dbus_strerror (errno));
574 _dbus_string_free (&pid);
578 if (print_pid_fd > 2)
579 _dbus_close (print_pid_fd, NULL);
581 _dbus_string_free (&pid);
584 /* Here we change our credentials if required,
585 * as soon as we've set up our sockets and pidfile
587 user = bus_config_parser_get_user (parser);
590 DBusCredentials creds;
593 _dbus_string_init_const (&u, user);
595 if (!_dbus_credentials_from_username (&u, &creds) ||
599 dbus_set_error (error, DBUS_ERROR_FAILED,
600 "Could not get UID and GID for username \"%s\"",
605 if (!_dbus_change_identity (creds.uid, creds.gid, error))
609 bus_config_parser_unref (parser);
610 _dbus_string_free (&full_address);
611 dbus_free_string_array (auth_mechanisms);
612 dbus_server_free_data_slot (&server_data_slot);
618 bus_config_parser_unref (parser);
621 bus_context_unref (context);
623 _dbus_string_free (&full_address);
624 dbus_free_string_array (auth_mechanisms);
626 dbus_server_free_data_slot (&server_data_slot);
632 shutdown_server (BusContext *context,
635 if (server == NULL ||
636 !dbus_server_get_is_connected (server))
639 if (!dbus_server_set_watch_functions (server,
643 _dbus_assert_not_reached ("setting watch functions to NULL failed");
645 if (!dbus_server_set_timeout_functions (server,
649 _dbus_assert_not_reached ("setting timeout functions to NULL failed");
651 dbus_server_disconnect (server);
655 bus_context_shutdown (BusContext *context)
659 link = _dbus_list_get_first_link (&context->servers);
662 shutdown_server (context, link->data);
664 link = _dbus_list_get_next_link (&context->servers, link);
669 bus_context_ref (BusContext *context)
671 _dbus_assert (context->refcount > 0);
672 context->refcount += 1;
676 bus_context_unref (BusContext *context)
678 _dbus_assert (context->refcount > 0);
679 context->refcount -= 1;
681 if (context->refcount == 0)
685 _dbus_verbose ("Finalizing bus context %p\n", context);
687 bus_context_shutdown (context);
689 if (context->connections)
691 bus_connections_unref (context->connections);
692 context->connections = NULL;
695 if (context->registry)
697 bus_registry_unref (context->registry);
698 context->registry = NULL;
701 if (context->activation)
703 bus_activation_unref (context->activation);
704 context->activation = NULL;
707 link = _dbus_list_get_first_link (&context->servers);
710 dbus_server_unref (link->data);
712 link = _dbus_list_get_next_link (&context->servers, link);
714 _dbus_list_clear (&context->servers);
718 bus_policy_unref (context->policy);
719 context->policy = NULL;
724 _dbus_loop_unref (context->loop);
725 context->loop = NULL;
728 if (context->matchmaker)
730 bus_matchmaker_unref (context->matchmaker);
731 context->matchmaker = NULL;
734 dbus_free (context->type);
735 dbus_free (context->address);
737 if (context->pidfile)
740 _dbus_string_init_const (&u, context->pidfile);
742 /* Deliberately ignore errors here, since there's not much
743 * we can do about it, and we're exiting anyways.
745 _dbus_delete_file (&u, NULL);
747 dbus_free (context->pidfile);
750 _dbus_user_database_unref (context->user_database);
754 dbus_server_free_data_slot (&server_data_slot);
758 /* type may be NULL */
760 bus_context_get_type (BusContext *context)
762 return context->type;
766 bus_context_get_address (BusContext *context)
768 return context->address;
772 bus_context_get_registry (BusContext *context)
774 return context->registry;
778 bus_context_get_connections (BusContext *context)
780 return context->connections;
784 bus_context_get_activation (BusContext *context)
786 return context->activation;
790 bus_context_get_matchmaker (BusContext *context)
792 return context->matchmaker;
796 bus_context_get_loop (BusContext *context)
798 return context->loop;
802 bus_context_get_user_database (BusContext *context)
804 return context->user_database;
808 bus_context_allow_user (BusContext *context,
811 return bus_policy_allow_user (context->policy,
812 context->user_database,
817 bus_context_create_client_policy (BusContext *context,
818 DBusConnection *connection,
821 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
822 return bus_policy_create_client_policy (context->policy, connection,
827 bus_context_get_activation_timeout (BusContext *context)
830 return context->limits.activation_timeout;
834 bus_context_get_auth_timeout (BusContext *context)
836 return context->limits.auth_timeout;
840 bus_context_get_max_completed_connections (BusContext *context)
842 return context->limits.max_completed_connections;
846 bus_context_get_max_incomplete_connections (BusContext *context)
848 return context->limits.max_incomplete_connections;
852 bus_context_get_max_connections_per_user (BusContext *context)
854 return context->limits.max_connections_per_user;
858 bus_context_get_max_pending_activations (BusContext *context)
860 return context->limits.max_pending_activations;
864 bus_context_get_max_services_per_connection (BusContext *context)
866 return context->limits.max_services_per_connection;
870 bus_context_get_max_match_rules_per_connection (BusContext *context)
872 return context->limits.max_match_rules_per_connection;
876 bus_context_get_max_replies_per_connection (BusContext *context)
878 return context->limits.max_replies_per_connection;
882 bus_context_get_reply_timeout (BusContext *context)
884 return context->limits.reply_timeout;
888 * addressed_recipient is the recipient specified in the message.
890 * proposed_recipient is the recipient we're considering sending
891 * to right this second, and may be an eavesdropper.
893 * sender is the sender of the message.
895 * NULL for proposed_recipient or sender definitely means the bus driver.
897 * NULL for addressed_recipient may mean the bus driver, or may mean
898 * no destination was specified in the message (e.g. a signal).
901 bus_context_check_security_policy (BusContext *context,
902 BusTransaction *transaction,
903 DBusConnection *sender,
904 DBusConnection *addressed_recipient,
905 DBusConnection *proposed_recipient,
906 DBusMessage *message,
909 BusClientPolicy *sender_policy;
910 BusClientPolicy *recipient_policy;
912 dbus_bool_t requested_reply;
914 type = dbus_message_get_type (message);
916 /* dispatch.c was supposed to ensure these invariants */
917 _dbus_assert (dbus_message_get_destination (message) != NULL ||
918 type == DBUS_MESSAGE_TYPE_SIGNAL);
919 _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
920 addressed_recipient != NULL ||
921 strcmp (dbus_message_get_destination (message), DBUS_SERVICE_ORG_FREEDESKTOP_DBUS) == 0);
925 case DBUS_MESSAGE_TYPE_METHOD_CALL:
926 case DBUS_MESSAGE_TYPE_SIGNAL:
927 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
928 case DBUS_MESSAGE_TYPE_ERROR:
932 _dbus_verbose ("security check disallowing message of unknown type %d\n",
935 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
936 "Message bus will not accept messages of unknown type\n");
941 requested_reply = FALSE;
945 if (bus_connection_is_active (sender))
947 sender_policy = bus_connection_get_policy (sender);
948 _dbus_assert (sender_policy != NULL);
950 /* Fill in requested_reply variable with TRUE if this is a
951 * reply and the reply was pending.
953 if (dbus_message_get_reply_serial (message) != 0)
955 if (proposed_recipient != NULL /* not to the bus driver */ &&
956 addressed_recipient == proposed_recipient /* not eavesdropping */)
960 dbus_error_init (&error2);
961 requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
963 sender, addressed_recipient, message,
965 if (dbus_error_is_set (&error2))
967 dbus_move_error (&error2, error);
975 /* Policy for inactive connections is that they can only send
976 * the hello message to the bus driver
978 if (proposed_recipient == NULL &&
979 dbus_message_is_method_call (message,
980 DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
983 _dbus_verbose ("security check allowing %s message\n",
989 _dbus_verbose ("security check disallowing non-%s message\n",
992 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
993 "Client tried to send a message other than %s without being registered",
1002 sender_policy = NULL;
1004 /* If the sender is the bus driver, we assume any reply was a
1005 * requested reply as bus driver won't send bogus ones
1007 if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1008 dbus_message_get_reply_serial (message) != 0)
1009 requested_reply = TRUE;
1012 _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1013 (sender == NULL && sender_policy == NULL));
1015 if (proposed_recipient != NULL)
1017 /* only the bus driver can send to an inactive recipient (as it
1018 * owns no services, so other apps can't address it). Inactive
1019 * recipients can receive any message.
1021 if (bus_connection_is_active (proposed_recipient))
1023 recipient_policy = bus_connection_get_policy (proposed_recipient);
1024 _dbus_assert (recipient_policy != NULL);
1026 else if (sender == NULL)
1028 _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1029 recipient_policy = NULL;
1033 _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1034 recipient_policy = NULL;
1038 recipient_policy = NULL;
1040 _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1041 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1042 (proposed_recipient == NULL && recipient_policy == NULL));
1044 if (sender_policy &&
1045 !bus_client_policy_check_can_send (sender_policy,
1046 context->registry, proposed_recipient,
1049 const char *dest = dbus_message_get_destination (message);
1050 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1051 "A security policy in place prevents this sender "
1052 "from sending this message to this recipient, "
1053 "see message bus configuration file (rejected message "
1054 "had interface \"%s\" member \"%s\" error name \"%s\" destination \"%s\")",
1055 dbus_message_get_interface (message) ?
1056 dbus_message_get_interface (message) : "(unset)",
1057 dbus_message_get_member (message) ?
1058 dbus_message_get_member (message) : "(unset)",
1059 dbus_message_get_error_name (message) ?
1060 dbus_message_get_error_name (message) : "(unset)",
1061 dest ? dest : DBUS_SERVICE_ORG_FREEDESKTOP_DBUS);
1062 _dbus_verbose ("security policy disallowing message due to sender policy\n");
1066 if (recipient_policy &&
1067 !bus_client_policy_check_can_receive (recipient_policy,
1071 addressed_recipient, proposed_recipient,
1074 const char *dest = dbus_message_get_destination (message);
1075 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1076 "A security policy in place prevents this recipient "
1077 "from receiving this message from this sender, "
1078 "see message bus configuration file (rejected message "
1079 "had interface \"%s\" member \"%s\" error name \"%s\" destination \"%s\" reply serial %u requested_reply=%d)",
1080 dbus_message_get_interface (message) ?
1081 dbus_message_get_interface (message) : "(unset)",
1082 dbus_message_get_member (message) ?
1083 dbus_message_get_member (message) : "(unset)",
1084 dbus_message_get_error_name (message) ?
1085 dbus_message_get_error_name (message) : "(unset)",
1086 dest ? dest : DBUS_SERVICE_ORG_FREEDESKTOP_DBUS,
1087 dbus_message_get_reply_serial (message),
1089 _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1093 /* See if limits on size have been exceeded */
1094 if (proposed_recipient &&
1095 dbus_connection_get_outgoing_size (proposed_recipient) >
1096 context->limits.max_outgoing_bytes)
1098 const char *dest = dbus_message_get_destination (message);
1099 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1100 "The destination service \"%s\" has a full message queue",
1101 dest ? dest : (proposed_recipient ?
1102 bus_connection_get_name (proposed_recipient) :
1103 DBUS_SERVICE_ORG_FREEDESKTOP_DBUS));
1104 _dbus_verbose ("security policy disallowing message due to full message queue\n");
1108 if (type == DBUS_MESSAGE_TYPE_METHOD_CALL)
1110 /* Record that we will allow a reply here in the future (don't
1111 * bother if the recipient is the bus). Only the addressed recipient
1114 if (sender && addressed_recipient &&
1115 !bus_connections_expect_reply (bus_connection_get_connections (sender),
1117 sender, addressed_recipient,
1120 _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1125 _dbus_verbose ("security policy allowing message\n");