1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-transport.c DBusTransport object (internal to D-Bus implementation)
4 * Copyright (C) 2002, 2003 Red Hat Inc.
5 * Copyright (C) 2013 Samsung Electronics
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <../config.h>
27 #include "dbus-transport-protected.h"
28 #include "dbus-transport-unix.h"
29 #include "dbus-transport-socket.h"
30 #include "dbus-connection-internal.h"
31 #include "dbus-watch.h"
32 #include "dbus-auth.h"
33 #include "dbus-address.h"
34 #include "dbus-credentials.h"
35 #include "dbus-mainloop.h"
36 #include "dbus-message.h"
37 #ifdef ENABLE_KDBUS_TRANSPORT
38 #include "dbus-transport-kdbus.h"
40 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
41 #include "dbus-server-debug-pipe.h"
45 * @defgroup DBusTransport DBusTransport object
46 * @ingroup DBusInternals
47 * @brief "Backend" for a DBusConnection.
49 * Types and functions related to DBusTransport. A transport is an
50 * abstraction that can send and receive data via various kinds of
51 * network connections or other IPC mechanisms.
57 * @typedef DBusTransport
59 * Opaque object representing a way message stream.
60 * DBusTransport abstracts various kinds of actual
61 * transport mechanism, such as different network protocols,
62 * or encryption schemes.
66 live_messages_notify (DBusCounter *counter,
69 DBusTransport *transport = user_data;
71 _dbus_transport_ref (transport);
74 _dbus_verbose ("Size counter value is now %d\n",
75 (int) _dbus_counter_get_size_value (counter));
76 _dbus_verbose ("Unix FD counter value is now %d\n",
77 (int) _dbus_counter_get_unix_fd_value (counter));
80 /* disable or re-enable the read watch for the transport if
83 if (transport->vtable->live_messages_changed)
85 _dbus_connection_lock (transport->connection);
86 (* transport->vtable->live_messages_changed) (transport);
87 _dbus_connection_unlock (transport->connection);
90 _dbus_transport_unref (transport);
94 * Initializes the base class members of DBusTransport. Chained up to
95 * by subclasses in their constructor. The server GUID is the
96 * globally unique ID for the server creating this connection
97 * and will be #NULL for the client side of a connection. The GUID
100 * @param transport the transport being created.
101 * @param vtable the subclass vtable.
102 * @param server_guid non-#NULL if this transport is on the server side of a connection
103 * @param address the address of the transport
104 * @returns #TRUE on success.
107 _dbus_transport_init_base (DBusTransport *transport,
108 const DBusTransportVTable *vtable,
109 const DBusString *server_guid,
110 const DBusString *address)
112 DBusMessageLoader *loader;
114 DBusCounter *counter;
115 char *address_copy = NULL;
116 DBusCredentials *creds;
118 loader = _dbus_message_loader_new ();
123 auth = _dbus_auth_server_new (server_guid);
126 _dbus_assert (address != NULL);
127 if (!_dbus_string_copy_data (address, &address_copy))
129 _dbus_message_loader_unref (loader);
132 #ifdef ENABLE_KDBUS_TRANSPORT
133 if(address_copy == strstr(address_copy, "kdbus:path="))
134 auth = _dbus_auth_client_new_kdbus();
137 auth = _dbus_auth_client_new ();
142 _dbus_message_loader_unref (loader);
144 if (address_copy != NULL)
145 dbus_free(address_copy);
150 counter = _dbus_counter_new ();
153 _dbus_auth_unref (auth);
154 _dbus_message_loader_unref (loader);
156 if (address_copy != NULL)
157 dbus_free(address_copy);
162 creds = _dbus_credentials_new ();
165 _dbus_counter_unref (counter);
166 _dbus_auth_unref (auth);
167 _dbus_message_loader_unref (loader);
169 if (address_copy != NULL)
170 dbus_free(address_copy);
177 _dbus_assert (address == NULL);
181 transport->refcount = 1;
182 transport->vtable = vtable;
183 transport->loader = loader;
184 transport->auth = auth;
185 transport->live_messages = counter;
186 transport->authenticated = FALSE;
187 transport->disconnected = FALSE;
188 transport->is_server = (server_guid != NULL);
189 transport->send_credentials_pending = !transport->is_server;
190 transport->receive_credentials_pending = transport->is_server;
191 transport->address = address_copy;
193 transport->unix_user_function = NULL;
194 transport->unix_user_data = NULL;
195 transport->free_unix_user_data = NULL;
197 transport->windows_user_function = NULL;
198 transport->windows_user_data = NULL;
199 transport->free_windows_user_data = NULL;
201 transport->expected_guid = NULL;
203 /* Try to default to something that won't totally hose the system,
204 * but doesn't impose too much of a limitation.
206 transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63;
208 /* On Linux RLIMIT_NOFILE defaults to 1024, so allowing 4096 fds live
209 should be more than enough */
210 transport->max_live_messages_unix_fds = 4096;
212 /* credentials read from socket if any */
213 transport->credentials = creds;
215 _dbus_counter_set_notify (transport->live_messages,
216 transport->max_live_messages_size,
217 transport->max_live_messages_unix_fds,
218 live_messages_notify,
221 if (transport->address)
222 _dbus_verbose ("Initialized transport on address %s\n", transport->address);
228 * Finalizes base class members of DBusTransport.
229 * Chained up to from subclass finalizers.
231 * @param transport the transport.
234 _dbus_transport_finalize_base (DBusTransport *transport)
236 if (!transport->disconnected)
237 _dbus_transport_disconnect (transport);
239 if (transport->free_unix_user_data != NULL)
240 (* transport->free_unix_user_data) (transport->unix_user_data);
242 if (transport->free_windows_user_data != NULL)
243 (* transport->free_windows_user_data) (transport->windows_user_data);
245 _dbus_message_loader_unref (transport->loader);
246 _dbus_auth_unref (transport->auth);
247 _dbus_counter_set_notify (transport->live_messages,
249 _dbus_counter_unref (transport->live_messages);
250 dbus_free (transport->address);
251 dbus_free (transport->expected_guid);
252 if (transport->credentials)
253 _dbus_credentials_unref (transport->credentials);
258 * Verifies if a given D-Bus address is a valid address
259 * by attempting to connect to it. If it is, returns the
260 * opened DBusTransport object. If it isn't, returns #NULL
263 * @param address the address to be checked.
264 * @param error address where an error can be returned.
265 * @returns a new transport, or #NULL on failure.
267 static DBusTransport*
268 check_address (const char *address, DBusError *error)
270 DBusAddressEntry **entries;
271 DBusTransport *transport = NULL;
274 _dbus_assert (address != NULL);
276 if (!dbus_parse_address (address, &entries, &len, error))
277 return NULL; /* not a valid address */
279 for (i = 0; i < len; i++)
281 transport = _dbus_transport_open (entries[i], error);
282 if (transport != NULL)
286 dbus_address_entries_free (entries);
291 * Creates a new transport for the "autostart" method.
292 * This creates a client-side of a transport.
294 * @param scope scope of autolaunch (Windows only)
295 * @param error address where an error can be returned.
296 * @returns a new transport, or #NULL on failure.
298 static DBusTransport*
299 _dbus_transport_new_for_autolaunch (const char *scope, DBusError *error)
302 DBusTransport *result = NULL;
304 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
306 if (!_dbus_string_init (&address))
308 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
312 if (!_dbus_get_autolaunch_address (scope, &address, error))
314 _DBUS_ASSERT_ERROR_IS_SET (error);
318 result = check_address (_dbus_string_get_const_data (&address), error);
320 _DBUS_ASSERT_ERROR_IS_SET (error);
322 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
325 _dbus_string_free (&address);
329 static DBusTransportOpenResult
330 _dbus_transport_open_autolaunch (DBusAddressEntry *entry,
331 DBusTransport **transport_p,
336 method = dbus_address_entry_get_method (entry);
337 _dbus_assert (method != NULL);
339 if (strcmp (method, "autolaunch") == 0)
341 const char *scope = dbus_address_entry_get_value (entry, "scope");
343 *transport_p = _dbus_transport_new_for_autolaunch (scope, error);
345 if (*transport_p == NULL)
347 _DBUS_ASSERT_ERROR_IS_SET (error);
348 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
352 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
353 return DBUS_TRANSPORT_OPEN_OK;
358 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
359 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
363 static const struct {
364 DBusTransportOpenResult (* func) (DBusAddressEntry *entry,
365 DBusTransport **transport_p,
368 #ifdef ENABLE_KDBUS_TRANSPORT
369 { _dbus_transport_open_kdbus },
371 { _dbus_transport_open_socket },
372 { _dbus_transport_open_platform_specific },
373 { _dbus_transport_open_autolaunch }
374 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
375 , { _dbus_transport_open_debug_pipe }
380 * Try to open a new transport for the given address entry. (This
381 * opens a client-side-of-the-connection transport.)
383 * @param entry the address entry
384 * @param error location to store reason for failure.
385 * @returns new transport of #NULL on failure.
388 _dbus_transport_open (DBusAddressEntry *entry,
391 DBusTransport *transport;
392 const char *expected_guid_orig;
395 DBusError tmp_error = DBUS_ERROR_INIT;
397 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
400 expected_guid_orig = dbus_address_entry_get_value (entry, "guid");
401 expected_guid = _dbus_strdup (expected_guid_orig);
403 if (expected_guid_orig != NULL && expected_guid == NULL)
405 _DBUS_SET_OOM (error);
409 for (i = 0; i < (int) _DBUS_N_ELEMENTS (open_funcs); ++i)
411 DBusTransportOpenResult result;
413 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
414 result = (* open_funcs[i].func) (entry, &transport, &tmp_error);
418 case DBUS_TRANSPORT_OPEN_OK:
419 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
422 case DBUS_TRANSPORT_OPEN_NOT_HANDLED:
423 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
424 /* keep going through the loop of open funcs */
426 case DBUS_TRANSPORT_OPEN_BAD_ADDRESS:
427 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
430 case DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT:
431 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
439 if (transport == NULL)
441 if (!dbus_error_is_set (&tmp_error))
442 _dbus_set_bad_address (&tmp_error,
444 "Unknown address type (examples of valid types are \"tcp\" and on UNIX \"unix\")");
446 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
447 dbus_move_error(&tmp_error, error);
448 dbus_free (expected_guid);
452 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
454 /* In the case of autostart the initial guid is NULL
455 * and the autostart transport recursively calls
456 * _dbus_open_transport wich returns a transport
457 * with a guid. That guid is the definitive one.
459 * FIXME: if more transports are added they may have
460 * an effect on the expected_guid semantics (i.e.
461 * expected_guid and transport->expected_guid may
462 * both have values). This is very unlikely though
463 * we should either throw asserts here for those
464 * corner cases or refactor the code so it is
465 * clearer on what is expected and what is not
468 transport->expected_guid = expected_guid;
475 * Increments the reference count for the transport.
477 * @param transport the transport.
478 * @returns the transport.
481 _dbus_transport_ref (DBusTransport *transport)
483 _dbus_assert (transport->refcount > 0);
485 transport->refcount += 1;
491 * Decrements the reference count for the transport.
492 * Disconnects and finalizes the transport if
493 * the reference count reaches zero.
495 * @param transport the transport.
498 _dbus_transport_unref (DBusTransport *transport)
500 _dbus_assert (transport != NULL);
501 _dbus_assert (transport->refcount > 0);
503 transport->refcount -= 1;
504 if (transport->refcount == 0)
506 _dbus_verbose ("finalizing\n");
508 _dbus_assert (transport->vtable->finalize != NULL);
510 (* transport->vtable->finalize) (transport);
515 * Closes our end of the connection to a remote application. Further
516 * attempts to use this transport will fail. Only the first call to
517 * _dbus_transport_disconnect() will have an effect.
519 * @param transport the transport.
523 _dbus_transport_disconnect (DBusTransport *transport)
525 _dbus_verbose ("start\n");
527 _dbus_assert (transport->vtable->disconnect != NULL);
529 if (transport->disconnected)
532 (* transport->vtable->disconnect) (transport);
534 transport->disconnected = TRUE;
536 _dbus_verbose ("end\n");
540 * Returns #TRUE if the transport has not been disconnected.
541 * Disconnection can result from _dbus_transport_disconnect()
542 * or because the server drops its end of the connection.
544 * @param transport the transport.
545 * @returns whether we're connected
548 _dbus_transport_get_is_connected (DBusTransport *transport)
550 return !transport->disconnected;
554 auth_via_unix_user_function (DBusTransport *transport)
556 DBusCredentials *auth_identity;
558 DBusConnection *connection;
559 DBusAllowUnixUserFunction unix_user_function;
560 void *unix_user_data;
563 /* Dropping the lock here probably isn't that safe. */
565 auth_identity = _dbus_auth_get_identity (transport->auth);
566 _dbus_assert (auth_identity != NULL);
568 connection = transport->connection;
569 unix_user_function = transport->unix_user_function;
570 unix_user_data = transport->unix_user_data;
571 uid = _dbus_credentials_get_unix_uid (auth_identity);
573 _dbus_verbose ("unlock\n");
574 _dbus_connection_unlock (connection);
576 allow = (* unix_user_function) (connection,
580 _dbus_verbose ("lock post unix user function\n");
581 _dbus_connection_lock (connection);
585 _dbus_verbose ("Client UID "DBUS_UID_FORMAT" authorized\n", uid);
589 _dbus_verbose ("Client UID "DBUS_UID_FORMAT
590 " was rejected, disconnecting\n",
591 _dbus_credentials_get_unix_uid (auth_identity));
592 _dbus_transport_disconnect (transport);
599 auth_via_windows_user_function (DBusTransport *transport)
601 DBusCredentials *auth_identity;
603 DBusConnection *connection;
604 DBusAllowWindowsUserFunction windows_user_function;
605 void *windows_user_data;
608 /* Dropping the lock here probably isn't that safe. */
610 auth_identity = _dbus_auth_get_identity (transport->auth);
611 _dbus_assert (auth_identity != NULL);
613 connection = transport->connection;
614 windows_user_function = transport->windows_user_function;
615 windows_user_data = transport->unix_user_data;
616 windows_sid = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
618 if (windows_sid == NULL)
624 _dbus_verbose ("unlock\n");
625 _dbus_connection_unlock (connection);
627 allow = (* windows_user_function) (connection,
631 _dbus_verbose ("lock post windows user function\n");
632 _dbus_connection_lock (connection);
636 _dbus_verbose ("Client SID '%s' authorized\n", windows_sid);
640 _dbus_verbose ("Client SID '%s' was rejected, disconnecting\n",
641 _dbus_credentials_get_windows_sid (auth_identity));
642 _dbus_transport_disconnect (transport);
649 auth_via_default_rules (DBusTransport *transport)
651 DBusCredentials *auth_identity;
652 DBusCredentials *our_identity;
655 auth_identity = _dbus_auth_get_identity (transport->auth);
656 _dbus_assert (auth_identity != NULL);
658 /* By default, connection is allowed if the client is 1) root or 2)
659 * has the same UID as us or 3) anonymous is allowed.
662 our_identity = _dbus_credentials_new_from_current_process ();
663 if (our_identity == NULL)
669 if (transport->allow_anonymous ||
670 _dbus_credentials_get_unix_uid (auth_identity) == 0 ||
671 _dbus_credentials_same_user (our_identity,
674 if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
675 _dbus_verbose ("Client authorized as SID '%s'"
676 "matching our SID '%s'\n",
677 _dbus_credentials_get_windows_sid(auth_identity),
678 _dbus_credentials_get_windows_sid(our_identity));
680 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
681 " matching our UID "DBUS_UID_FORMAT"\n",
682 _dbus_credentials_get_unix_uid(auth_identity),
683 _dbus_credentials_get_unix_uid(our_identity));
684 /* We have authenticated! */
689 if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
690 _dbus_verbose ("Client authorized as SID '%s'"
691 " but our SID is '%s', disconnecting\n",
692 (_dbus_credentials_get_windows_sid(auth_identity) ?
693 _dbus_credentials_get_windows_sid(auth_identity) : "<null>"),
694 (_dbus_credentials_get_windows_sid(our_identity) ?
695 _dbus_credentials_get_windows_sid(our_identity) : "<null>"));
697 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
698 " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
699 _dbus_credentials_get_unix_uid(auth_identity),
700 _dbus_credentials_get_unix_uid(our_identity));
701 _dbus_transport_disconnect (transport);
705 _dbus_credentials_unref (our_identity);
711 * Returns #TRUE if we have been authenticated. It will return #TRUE even if
712 * the transport is now disconnected, but was ever authenticated before
715 * This replaces the older _dbus_transport_get_is_authenticated() which
718 * @param transport the transport
719 * @returns whether we're authenticated
722 _dbus_transport_peek_is_authenticated (DBusTransport *transport)
724 return transport->authenticated;
728 * Returns #TRUE if we have been authenticated. It will return #TRUE even if
729 * the transport is now disconnected, but was ever authenticated before
732 * If we have not finished authenticating, but we have enough buffered input
733 * to finish the job, then this function will do so before it returns.
735 * This used to be called _dbus_transport_get_is_authenticated(), but that
736 * name seems inappropriate for a function with side-effects.
738 * @todo we drop connection->mutex when calling the unix_user_function,
739 * and windows_user_function, which may not be safe really.
741 * @param transport the transport
742 * @returns whether we're authenticated
745 _dbus_transport_try_to_authenticate (DBusTransport *transport)
747 if (transport->authenticated)
751 dbus_bool_t maybe_authenticated;
753 if (transport->disconnected)
756 /* paranoia ref since we call user callbacks sometimes */
757 _dbus_connection_ref_unlocked (transport->connection);
759 maybe_authenticated =
760 (!(transport->send_credentials_pending ||
761 transport->receive_credentials_pending));
763 if (maybe_authenticated)
765 switch (_dbus_auth_do_work (transport->auth))
767 case DBUS_AUTH_STATE_AUTHENTICATED:
768 /* leave as maybe_authenticated */
771 maybe_authenticated = FALSE;
775 /* If we're the client, verify the GUID
777 if (maybe_authenticated && !transport->is_server)
779 const char *server_guid;
781 server_guid = _dbus_auth_get_guid_from_server (transport->auth);
782 _dbus_assert (server_guid != NULL);
784 if (transport->expected_guid &&
785 strcmp (transport->expected_guid, server_guid) != 0)
787 _dbus_verbose ("Client expected GUID '%s' and we got '%s' from the server\n",
788 transport->expected_guid, server_guid);
789 _dbus_transport_disconnect (transport);
790 _dbus_connection_unref_unlocked (transport->connection);
795 /* If we're the server, see if we want to allow this identity to proceed.
797 if (maybe_authenticated && transport->is_server)
800 DBusCredentials *auth_identity;
802 auth_identity = _dbus_auth_get_identity (transport->auth);
803 _dbus_assert (auth_identity != NULL);
805 /* If we have an auth'd user and a user function, delegate
806 * deciding whether auth credentials are good enough to the
807 * app; otherwise, use our default decision process.
809 if (transport->unix_user_function != NULL &&
810 _dbus_credentials_include (auth_identity, DBUS_CREDENTIAL_UNIX_USER_ID))
812 allow = auth_via_unix_user_function (transport);
814 else if (transport->windows_user_function != NULL &&
815 _dbus_credentials_include (auth_identity, DBUS_CREDENTIAL_WINDOWS_SID))
817 allow = auth_via_windows_user_function (transport);
821 allow = auth_via_default_rules (transport);
825 maybe_authenticated = FALSE;
828 transport->authenticated = maybe_authenticated;
830 _dbus_connection_unref_unlocked (transport->connection);
831 return maybe_authenticated;
836 * See dbus_connection_get_is_anonymous().
838 * @param transport the transport
839 * @returns #TRUE if not authenticated or authenticated as anonymous
842 _dbus_transport_get_is_anonymous (DBusTransport *transport)
844 DBusCredentials *auth_identity;
846 if (!transport->authenticated)
849 auth_identity = _dbus_auth_get_identity (transport->auth);
851 if (_dbus_credentials_are_anonymous (auth_identity))
858 * Returns TRUE if the transport supports sending unix fds.
860 * @param transport the transport
861 * @returns #TRUE if TRUE it is possible to send unix fds across the transport.
864 _dbus_transport_can_pass_unix_fd(DBusTransport *transport)
866 return DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport);
870 * Gets the address of a transport. It will be
871 * #NULL for a server-side transport.
873 * @param transport the transport
874 * @returns transport's address
877 _dbus_transport_get_address (DBusTransport *transport)
879 return transport->address;
883 * Gets the id of the server we are connected to (see
884 * dbus_server_get_id()). Only works on client side.
886 * @param transport the transport
887 * @returns transport's server's id or #NULL if we are the server side
890 _dbus_transport_get_server_id (DBusTransport *transport)
892 if (transport->is_server)
894 else if (transport->authenticated)
895 return _dbus_auth_get_guid_from_server (transport->auth);
897 return transport->expected_guid;
901 * Handles a watch by reading data, writing data, or disconnecting
902 * the transport, as appropriate for the given condition.
904 * @param transport the transport.
905 * @param watch the watch.
906 * @param condition the current state of the watched file descriptor.
907 * @returns #FALSE if not enough memory to fully handle the watch
910 _dbus_transport_handle_watch (DBusTransport *transport,
912 unsigned int condition)
916 _dbus_assert (transport->vtable->handle_watch != NULL);
918 if (transport->disconnected)
921 if (dbus_watch_get_socket (watch) < 0)
923 _dbus_warn_check_failed ("Tried to handle an invalidated watch; this watch should have been removed\n");
927 _dbus_watch_sanitize_condition (watch, &condition);
929 _dbus_transport_ref (transport);
930 _dbus_watch_ref (watch);
931 retval = (* transport->vtable->handle_watch) (transport, watch, condition);
932 _dbus_watch_unref (watch);
933 _dbus_transport_unref (transport);
939 * Sets the connection using this transport. Allows the transport
940 * to add watches to the connection, queue incoming messages,
941 * and pull outgoing messages.
943 * @param transport the transport.
944 * @param connection the connection.
945 * @returns #FALSE if not enough memory
948 _dbus_transport_set_connection (DBusTransport *transport,
949 DBusConnection *connection)
951 _dbus_assert (transport->vtable->connection_set != NULL);
952 _dbus_assert (transport->connection == NULL);
954 transport->connection = connection;
956 _dbus_transport_ref (transport);
957 if (!(* transport->vtable->connection_set) (transport))
958 transport->connection = NULL;
959 _dbus_transport_unref (transport);
961 return transport->connection != NULL;
965 * Get the socket file descriptor, if any.
967 * @param transport the transport
968 * @param fd_p pointer to fill in with the descriptor
969 * @returns #TRUE if a descriptor was available
972 _dbus_transport_get_socket_fd (DBusTransport *transport,
977 if (transport->vtable->get_socket_fd == NULL)
980 if (transport->disconnected)
983 _dbus_transport_ref (transport);
985 retval = (* transport->vtable->get_socket_fd) (transport,
988 _dbus_transport_unref (transport);
994 * Performs a single poll()/select() on the transport's file
995 * descriptors and then reads/writes data as appropriate,
996 * queueing incoming messages and sending outgoing messages.
997 * This is the backend for _dbus_connection_do_iteration().
998 * See _dbus_connection_do_iteration() for full details.
1000 * @param transport the transport.
1001 * @param flags indicates whether to read or write, and whether to block.
1002 * @param timeout_milliseconds if blocking, timeout or -1 for no timeout.
1005 _dbus_transport_do_iteration (DBusTransport *transport,
1007 int timeout_milliseconds)
1009 _dbus_assert (transport->vtable->do_iteration != NULL);
1011 _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n",
1012 flags, timeout_milliseconds, !transport->disconnected);
1014 if ((flags & (DBUS_ITERATION_DO_WRITING |
1015 DBUS_ITERATION_DO_READING)) == 0)
1016 return; /* Nothing to do */
1018 if (transport->disconnected)
1021 _dbus_transport_ref (transport);
1022 (* transport->vtable->do_iteration) (transport, flags,
1023 timeout_milliseconds);
1024 _dbus_transport_unref (transport);
1026 _dbus_verbose ("end\n");
1030 recover_unused_bytes (DBusTransport *transport)
1032 if (_dbus_auth_needs_decoding (transport->auth))
1034 DBusString plaintext;
1035 const DBusString *encoded;
1039 if (!_dbus_string_init (&plaintext))
1042 _dbus_auth_get_unused_bytes (transport->auth,
1045 if (!_dbus_auth_decode_data (transport->auth,
1046 encoded, &plaintext))
1048 _dbus_string_free (&plaintext);
1052 _dbus_message_loader_get_buffer (transport->loader,
1055 orig_len = _dbus_string_get_length (buffer);
1057 if (!_dbus_string_move (&plaintext, 0, buffer,
1060 _dbus_string_free (&plaintext);
1064 _dbus_verbose (" %d unused bytes sent to message loader\n",
1065 _dbus_string_get_length (buffer) -
1068 _dbus_message_loader_return_buffer (transport->loader,
1070 _dbus_string_get_length (buffer) -
1073 _dbus_auth_delete_unused_bytes (transport->auth);
1075 _dbus_string_free (&plaintext);
1079 const DBusString *bytes;
1082 dbus_bool_t succeeded;
1084 _dbus_message_loader_get_buffer (transport->loader,
1087 orig_len = _dbus_string_get_length (buffer);
1089 _dbus_auth_get_unused_bytes (transport->auth,
1093 if (!_dbus_string_copy (bytes, 0, buffer, _dbus_string_get_length (buffer)))
1096 _dbus_verbose (" %d unused bytes sent to message loader\n",
1097 _dbus_string_get_length (buffer) -
1100 _dbus_message_loader_return_buffer (transport->loader,
1102 _dbus_string_get_length (buffer) -
1106 _dbus_auth_delete_unused_bytes (transport->auth);
1114 _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
1119 * Reports our current dispatch status (whether there's buffered
1120 * data to be queued as messages, or not, or we need memory).
1122 * @param transport the transport
1123 * @returns current status
1126 _dbus_transport_get_dispatch_status (DBusTransport *transport)
1128 if (_dbus_counter_get_size_value (transport->live_messages) >= transport->max_live_messages_size ||
1129 _dbus_counter_get_unix_fd_value (transport->live_messages) >= transport->max_live_messages_unix_fds)
1130 return DBUS_DISPATCH_COMPLETE; /* complete for now */
1132 if (!_dbus_transport_try_to_authenticate (transport))
1134 if (_dbus_auth_do_work (transport->auth) ==
1135 DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
1136 return DBUS_DISPATCH_NEED_MEMORY;
1137 else if (!_dbus_transport_try_to_authenticate (transport))
1138 return DBUS_DISPATCH_COMPLETE;
1141 if (!transport->unused_bytes_recovered &&
1142 !recover_unused_bytes (transport))
1143 return DBUS_DISPATCH_NEED_MEMORY;
1145 transport->unused_bytes_recovered = TRUE;
1147 if (!_dbus_message_loader_queue_messages (transport->loader))
1148 return DBUS_DISPATCH_NEED_MEMORY;
1150 if (_dbus_message_loader_peek_message (transport->loader) != NULL)
1151 return DBUS_DISPATCH_DATA_REMAINS;
1153 return DBUS_DISPATCH_COMPLETE;
1157 * Processes data we've read while handling a watch, potentially
1158 * converting some of it to messages and queueing those messages on
1161 * @param transport the transport
1162 * @returns #TRUE if we had enough memory to queue all messages
1165 _dbus_transport_queue_messages (DBusTransport *transport)
1167 DBusDispatchStatus status;
1170 _dbus_verbose ("_dbus_transport_queue_messages()\n");
1173 /* Queue any messages */
1174 while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
1176 DBusMessage *message;
1179 link = _dbus_message_loader_pop_message_link (transport->loader);
1180 _dbus_assert (link != NULL);
1182 message = link->data;
1184 _dbus_verbose ("queueing received message %p\n", message);
1186 if (!_dbus_message_add_counter (message, transport->live_messages))
1188 _dbus_message_loader_putback_message_link (transport->loader,
1190 status = DBUS_DISPATCH_NEED_MEMORY;
1195 /* We didn't call the notify function when we added the counter, so
1196 * catch up now. Since we have the connection's lock, it's desirable
1197 * that we bypass the notify function and call this virtual method
1199 if (transport->vtable->live_messages_changed)
1200 (* transport->vtable->live_messages_changed) (transport);
1202 /* pass ownership of link and message ref to connection */
1203 _dbus_connection_queue_received_message_link (transport->connection,
1208 if (_dbus_message_loader_get_is_corrupted (transport->loader))
1210 _dbus_verbose ("Corrupted message stream, disconnecting\n");
1211 _dbus_transport_disconnect (transport);
1214 return status != DBUS_DISPATCH_NEED_MEMORY;
1218 * See dbus_connection_set_max_message_size().
1220 * @param transport the transport
1221 * @param size the max size of a single message
1224 _dbus_transport_set_max_message_size (DBusTransport *transport,
1227 _dbus_message_loader_set_max_message_size (transport->loader, size);
1231 * See dbus_connection_set_max_message_unix_fds().
1233 * @param transport the transport
1234 * @param n the max number of unix fds of a single message
1237 _dbus_transport_set_max_message_unix_fds (DBusTransport *transport,
1240 _dbus_message_loader_set_max_message_unix_fds (transport->loader, n);
1244 * See dbus_connection_get_max_message_size().
1246 * @param transport the transport
1247 * @returns max message size
1250 _dbus_transport_get_max_message_size (DBusTransport *transport)
1252 return _dbus_message_loader_get_max_message_size (transport->loader);
1256 * See dbus_connection_get_max_message_unix_fds().
1258 * @param transport the transport
1259 * @returns max message unix fds
1262 _dbus_transport_get_max_message_unix_fds (DBusTransport *transport)
1264 return _dbus_message_loader_get_max_message_unix_fds (transport->loader);
1268 * See dbus_connection_set_max_received_size().
1270 * @param transport the transport
1271 * @param size the max size of all incoming messages
1274 _dbus_transport_set_max_received_size (DBusTransport *transport,
1277 transport->max_live_messages_size = size;
1278 _dbus_counter_set_notify (transport->live_messages,
1279 transport->max_live_messages_size,
1280 transport->max_live_messages_unix_fds,
1281 live_messages_notify,
1286 * See dbus_connection_set_max_received_unix_fds().
1288 * @param transport the transport
1289 * @param n the max unix fds of all incoming messages
1292 _dbus_transport_set_max_received_unix_fds (DBusTransport *transport,
1295 transport->max_live_messages_unix_fds = n;
1296 _dbus_counter_set_notify (transport->live_messages,
1297 transport->max_live_messages_size,
1298 transport->max_live_messages_unix_fds,
1299 live_messages_notify,
1304 * See dbus_connection_get_max_received_size().
1306 * @param transport the transport
1307 * @returns max bytes for all live messages
1310 _dbus_transport_get_max_received_size (DBusTransport *transport)
1312 return transport->max_live_messages_size;
1316 * See dbus_connection_set_max_received_unix_fds().
1318 * @param transport the transport
1319 * @returns max unix fds for all live messages
1322 _dbus_transport_get_max_received_unix_fds (DBusTransport *transport)
1324 return transport->max_live_messages_unix_fds;
1328 * See dbus_connection_get_unix_user().
1330 * @param transport the transport
1331 * @param uid return location for the user ID
1332 * @returns #TRUE if uid is filled in with a valid user ID
1335 _dbus_transport_get_unix_user (DBusTransport *transport,
1338 DBusCredentials *auth_identity;
1340 *uid = _DBUS_INT32_MAX; /* better than some root or system user in
1341 * case of bugs in the caller. Caller should
1342 * never use this value on purpose, however.
1345 if (!transport->authenticated)
1348 auth_identity = _dbus_auth_get_identity (transport->auth);
1350 if (_dbus_credentials_include (auth_identity,
1351 DBUS_CREDENTIAL_UNIX_USER_ID))
1353 *uid = _dbus_credentials_get_unix_uid (auth_identity);
1361 * See dbus_connection_get_unix_process_id().
1363 * @param transport the transport
1364 * @param pid return location for the process ID
1365 * @returns #TRUE if uid is filled in with a valid process ID
1368 _dbus_transport_get_unix_process_id (DBusTransport *transport,
1371 DBusCredentials *auth_identity;
1373 *pid = DBUS_PID_UNSET; /* Caller should never use this value on purpose,
1374 * but we set it to a safe number, INT_MAX,
1375 * just to root out possible bugs in bad callers.
1378 if (!transport->authenticated)
1381 auth_identity = _dbus_auth_get_identity (transport->auth);
1383 if (_dbus_credentials_include (auth_identity,
1384 DBUS_CREDENTIAL_UNIX_PROCESS_ID))
1386 *pid = _dbus_credentials_get_pid (auth_identity);
1394 * See dbus_connection_get_adt_audit_session_data().
1396 * @param transport the transport
1397 * @param data return location for the ADT audit data
1398 * @param data_size return length of audit data
1399 * @returns #TRUE if audit data is filled in with a valid ucred
1402 _dbus_transport_get_adt_audit_session_data (DBusTransport *transport,
1406 DBusCredentials *auth_identity;
1411 if (!transport->authenticated)
1414 auth_identity = _dbus_auth_get_identity (transport->auth);
1416 if (_dbus_credentials_include (auth_identity,
1417 DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID))
1419 *data = (void *) _dbus_credentials_get_adt_audit_data (auth_identity);
1420 *data_size = _dbus_credentials_get_adt_audit_data_size (auth_identity);
1428 * See dbus_connection_set_unix_user_function().
1430 * @param transport the transport
1431 * @param function the predicate
1432 * @param data data to pass to the predicate
1433 * @param free_data_function function to free the data
1434 * @param old_data the old user data to be freed
1435 * @param old_free_data_function old free data function to free it with
1438 _dbus_transport_set_unix_user_function (DBusTransport *transport,
1439 DBusAllowUnixUserFunction function,
1441 DBusFreeFunction free_data_function,
1443 DBusFreeFunction *old_free_data_function)
1445 *old_data = transport->unix_user_data;
1446 *old_free_data_function = transport->free_unix_user_data;
1448 transport->unix_user_function = function;
1449 transport->unix_user_data = data;
1450 transport->free_unix_user_data = free_data_function;
1454 * See dbus_connection_get_windows_user().
1456 * @param transport the transport
1457 * @param windows_sid_p return location for the user ID
1458 * @returns #TRUE if user is available; the returned value may still be #NULL if no memory to copy it
1461 _dbus_transport_get_windows_user (DBusTransport *transport,
1462 char **windows_sid_p)
1464 DBusCredentials *auth_identity;
1466 *windows_sid_p = NULL;
1468 if (!transport->authenticated)
1471 auth_identity = _dbus_auth_get_identity (transport->auth);
1473 if (_dbus_credentials_include (auth_identity,
1474 DBUS_CREDENTIAL_WINDOWS_SID))
1476 /* If no memory, we are supposed to return TRUE and set NULL */
1477 *windows_sid_p = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
1486 * See dbus_connection_set_windows_user_function().
1488 * @param transport the transport
1489 * @param function the predicate
1490 * @param data data to pass to the predicate
1491 * @param free_data_function function to free the data
1492 * @param old_data the old user data to be freed
1493 * @param old_free_data_function old free data function to free it with
1497 _dbus_transport_set_windows_user_function (DBusTransport *transport,
1498 DBusAllowWindowsUserFunction function,
1500 DBusFreeFunction free_data_function,
1502 DBusFreeFunction *old_free_data_function)
1504 *old_data = transport->windows_user_data;
1505 *old_free_data_function = transport->free_windows_user_data;
1507 transport->windows_user_function = function;
1508 transport->windows_user_data = data;
1509 transport->free_windows_user_data = free_data_function;
1513 * Sets the SASL authentication mechanisms supported by this transport.
1515 * @param transport the transport
1516 * @param mechanisms the #NULL-terminated array of mechanisms
1518 * @returns #FALSE if no memory
1521 _dbus_transport_set_auth_mechanisms (DBusTransport *transport,
1522 const char **mechanisms)
1524 return _dbus_auth_set_mechanisms (transport->auth, mechanisms);
1528 * See dbus_connection_set_allow_anonymous()
1530 * @param transport the transport
1531 * @param value #TRUE to allow anonymous connection
1534 _dbus_transport_set_allow_anonymous (DBusTransport *transport,
1537 transport->allow_anonymous = value != FALSE;
1540 #ifdef DBUS_ENABLE_STATS
1542 _dbus_transport_get_stats (DBusTransport *transport,
1543 dbus_uint32_t *queue_bytes,
1544 dbus_uint32_t *queue_fds,
1545 dbus_uint32_t *peak_queue_bytes,
1546 dbus_uint32_t *peak_queue_fds)
1548 if (queue_bytes != NULL)
1549 *queue_bytes = _dbus_counter_get_size_value (transport->live_messages);
1551 if (queue_fds != NULL)
1552 *queue_fds = _dbus_counter_get_unix_fd_value (transport->live_messages);
1554 if (peak_queue_bytes != NULL)
1555 *peak_queue_bytes = _dbus_counter_get_peak_size_value (transport->live_messages);
1557 if (peak_queue_fds != NULL)
1558 *peak_queue_fds = _dbus_counter_get_peak_unix_fd_value (transport->live_messages);
1560 #endif /* DBUS_ENABLE_STATS */