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.
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
25 #include "dbus-transport-protected.h"
26 #include "dbus-transport-unix.h"
27 #include "dbus-transport-socket.h"
28 #include "dbus-connection-internal.h"
29 #include "dbus-watch.h"
30 #include "dbus-auth.h"
31 #include "dbus-address.h"
32 #include "dbus-credentials.h"
33 #include "dbus-message-private.h"
34 #include "dbus-marshal-header.h"
35 #ifdef DBUS_BUILD_TESTS
36 #include "dbus-server-debug-pipe.h"
40 * @defgroup DBusTransport DBusTransport object
41 * @ingroup DBusInternals
42 * @brief "Backend" for a DBusConnection.
44 * Types and functions related to DBusTransport. A transport is an
45 * abstraction that can send and receive data via various kinds of
46 * network connections or other IPC mechanisms.
52 * @typedef DBusTransport
54 * Opaque object representing a way message stream.
55 * DBusTransport abstracts various kinds of actual
56 * transport mechanism, such as different network protocols,
57 * or encryption schemes.
61 live_messages_notify (DBusCounter *counter,
64 DBusTransport *transport = user_data;
66 _dbus_transport_ref (transport);
69 _dbus_verbose ("Size counter value is now %d\n",
70 (int) _dbus_counter_get_size_value (counter));
71 _dbus_verbose ("Unix FD counter value is now %d\n",
72 (int) _dbus_counter_get_unix_fd_value (counter));
75 /* disable or re-enable the read watch for the transport if
78 if (transport->vtable->live_messages_changed)
79 (* transport->vtable->live_messages_changed) (transport);
81 _dbus_transport_unref (transport);
85 * Initializes the base class members of DBusTransport. Chained up to
86 * by subclasses in their constructor. The server GUID is the
87 * globally unique ID for the server creating this connection
88 * and will be #NULL for the client side of a connection. The GUID
91 * @param transport the transport being created.
92 * @param vtable the subclass vtable.
93 * @param server_guid non-#NULL if this transport is on the server side of a connection
94 * @param address the address of the transport
95 * @returns #TRUE on success.
98 _dbus_transport_init_base (DBusTransport *transport,
99 const DBusTransportVTable *vtable,
100 const DBusString *server_guid,
101 const DBusString *address)
103 DBusMessageLoader *loader;
105 DBusCounter *counter;
107 DBusCredentials *creds;
109 loader = _dbus_message_loader_new ();
114 auth = _dbus_auth_server_new (server_guid);
116 auth = _dbus_auth_client_new ();
119 _dbus_message_loader_unref (loader);
123 counter = _dbus_counter_new ();
126 _dbus_auth_unref (auth);
127 _dbus_message_loader_unref (loader);
131 creds = _dbus_credentials_new ();
134 _dbus_counter_unref (counter);
135 _dbus_auth_unref (auth);
136 _dbus_message_loader_unref (loader);
142 _dbus_assert (address == NULL);
147 _dbus_assert (address != NULL);
149 if (!_dbus_string_copy_data (address, &address_copy))
151 _dbus_credentials_unref (creds);
152 _dbus_counter_unref (counter);
153 _dbus_auth_unref (auth);
154 _dbus_message_loader_unref (loader);
159 transport->refcount = 1;
160 transport->vtable = vtable;
161 transport->loader = loader;
162 transport->auth = auth;
163 transport->live_messages = counter;
164 transport->authenticated = FALSE;
165 transport->disconnected = FALSE;
166 transport->is_server = (server_guid != NULL);
167 transport->send_credentials_pending = !transport->is_server;
168 transport->receive_credentials_pending = transport->is_server;
169 transport->address = address_copy;
171 transport->unix_user_function = NULL;
172 transport->unix_user_data = NULL;
173 transport->free_unix_user_data = NULL;
175 transport->windows_user_function = NULL;
176 transport->windows_user_data = NULL;
177 transport->free_windows_user_data = NULL;
179 transport->expected_guid = NULL;
181 /* Try to default to something that won't totally hose the system,
182 * but doesn't impose too much of a limitation.
184 transport->max_live_messages_size = _DBUS_ONE_MEGABYTE * 63;
186 /* On Linux RLIMIT_NOFILE defaults to 1024, so allowing 4096 fds live
187 should be more than enough */
188 transport->max_live_messages_unix_fds = 4096;
190 /* credentials read from socket if any */
191 transport->credentials = creds;
193 _dbus_counter_set_notify (transport->live_messages,
194 transport->max_live_messages_size,
195 transport->max_live_messages_unix_fds,
196 live_messages_notify,
199 if (transport->address)
200 _dbus_verbose ("Initialized transport on address %s\n", transport->address);
206 * Finalizes base class members of DBusTransport.
207 * Chained up to from subclass finalizers.
209 * @param transport the transport.
212 _dbus_transport_finalize_base (DBusTransport *transport)
214 if (!transport->disconnected)
215 _dbus_transport_disconnect (transport);
217 if (transport->free_unix_user_data != NULL)
218 (* transport->free_unix_user_data) (transport->unix_user_data);
220 if (transport->free_windows_user_data != NULL)
221 (* transport->free_windows_user_data) (transport->windows_user_data);
223 _dbus_message_loader_unref (transport->loader);
224 _dbus_auth_unref (transport->auth);
225 _dbus_counter_set_notify (transport->live_messages,
227 _dbus_counter_unref (transport->live_messages);
228 dbus_free (transport->address);
229 dbus_free (transport->expected_guid);
230 if (transport->credentials)
231 _dbus_credentials_unref (transport->credentials);
236 * Verifies if a given D-Bus address is a valid address
237 * by attempting to connect to it. If it is, returns the
238 * opened DBusTransport object. If it isn't, returns #NULL
241 * @param error address where an error can be returned.
242 * @returns a new transport, or #NULL on failure.
244 static DBusTransport*
245 check_address (const char *address, DBusError *error)
247 DBusAddressEntry **entries;
248 DBusTransport *transport = NULL;
251 _dbus_assert (address != NULL);
252 _dbus_assert (*address != '\0');
254 if (!dbus_parse_address (address, &entries, &len, error))
255 return NULL; /* not a valid address */
257 for (i = 0; i < len; i++)
259 transport = _dbus_transport_open (entries[i], error);
260 if (transport != NULL)
264 dbus_address_entries_free (entries);
269 * Creates a new transport for the "autostart" method.
270 * This creates a client-side of a transport.
272 * @param error address where an error can be returned.
273 * @returns a new transport, or #NULL on failure.
275 static DBusTransport*
276 _dbus_transport_new_for_autolaunch (DBusError *error)
279 DBusTransport *result = NULL;
281 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
283 if (!_dbus_string_init (&address))
285 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
289 if (!_dbus_get_autolaunch_address (&address, error))
291 _DBUS_ASSERT_ERROR_IS_SET (error);
295 result = check_address (_dbus_string_get_const_data (&address), error);
297 _DBUS_ASSERT_ERROR_IS_SET (error);
299 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
302 _dbus_string_free (&address);
306 static DBusTransportOpenResult
307 _dbus_transport_open_autolaunch (DBusAddressEntry *entry,
308 DBusTransport **transport_p,
313 method = dbus_address_entry_get_method (entry);
314 _dbus_assert (method != NULL);
316 if (strcmp (method, "autolaunch") == 0)
318 *transport_p = _dbus_transport_new_for_autolaunch (error);
320 if (*transport_p == NULL)
322 _DBUS_ASSERT_ERROR_IS_SET (error);
323 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
327 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
328 return DBUS_TRANSPORT_OPEN_OK;
333 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
334 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
338 static const struct {
339 DBusTransportOpenResult (* func) (DBusAddressEntry *entry,
340 DBusTransport **transport_p,
343 { _dbus_transport_open_socket },
344 { _dbus_transport_open_platform_specific },
345 { _dbus_transport_open_autolaunch }
346 #ifdef DBUS_BUILD_TESTS
347 , { _dbus_transport_open_debug_pipe }
352 * Try to open a new transport for the given address entry. (This
353 * opens a client-side-of-the-connection transport.)
355 * @param entry the address entry
356 * @param error location to store reason for failure.
357 * @returns new transport of #NULL on failure.
360 _dbus_transport_open (DBusAddressEntry *entry,
363 DBusTransport *transport;
364 const char *expected_guid_orig;
367 DBusError tmp_error = DBUS_ERROR_INIT;
369 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
372 expected_guid_orig = dbus_address_entry_get_value (entry, "guid");
373 expected_guid = _dbus_strdup (expected_guid_orig);
375 if (expected_guid_orig != NULL && expected_guid == NULL)
377 _DBUS_SET_OOM (error);
381 for (i = 0; i < (int) _DBUS_N_ELEMENTS (open_funcs); ++i)
383 DBusTransportOpenResult result;
385 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
386 result = (* open_funcs[i].func) (entry, &transport, &tmp_error);
390 case DBUS_TRANSPORT_OPEN_OK:
391 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
394 case DBUS_TRANSPORT_OPEN_NOT_HANDLED:
395 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
396 /* keep going through the loop of open funcs */
398 case DBUS_TRANSPORT_OPEN_BAD_ADDRESS:
399 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
402 case DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT:
403 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
411 if (transport == NULL)
413 if (!dbus_error_is_set (&tmp_error))
414 _dbus_set_bad_address (&tmp_error,
416 "Unknown address type (examples of valid types are \"tcp\" and on UNIX \"unix\")");
418 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
419 dbus_move_error(&tmp_error, error);
420 dbus_free (expected_guid);
424 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
426 /* In the case of autostart the initial guid is NULL
427 * and the autostart transport recursively calls
428 * _dbus_open_transport wich returns a transport
429 * with a guid. That guid is the definitive one.
431 * FIXME: if more transports are added they may have
432 * an effect on the expected_guid semantics (i.e.
433 * expected_guid and transport->expected_guid may
434 * both have values). This is very unlikely though
435 * we should either throw asserts here for those
436 * corner cases or refactor the code so it is
437 * clearer on what is expected and what is not
440 transport->expected_guid = expected_guid;
447 * Increments the reference count for the transport.
449 * @param transport the transport.
450 * @returns the transport.
453 _dbus_transport_ref (DBusTransport *transport)
455 _dbus_assert (transport->refcount > 0);
457 transport->refcount += 1;
463 * Decrements the reference count for the transport.
464 * Disconnects and finalizes the transport if
465 * the reference count reaches zero.
467 * @param transport the transport.
470 _dbus_transport_unref (DBusTransport *transport)
472 _dbus_assert (transport != NULL);
473 _dbus_assert (transport->refcount > 0);
475 transport->refcount -= 1;
476 if (transport->refcount == 0)
478 _dbus_verbose ("finalizing\n");
480 _dbus_assert (transport->vtable->finalize != NULL);
482 (* transport->vtable->finalize) (transport);
487 * Closes our end of the connection to a remote application. Further
488 * attempts to use this transport will fail. Only the first call to
489 * _dbus_transport_disconnect() will have an effect.
491 * @param transport the transport.
495 _dbus_transport_disconnect (DBusTransport *transport)
497 _dbus_verbose ("start\n");
499 _dbus_assert (transport->vtable->disconnect != NULL);
501 if (transport->disconnected)
504 (* transport->vtable->disconnect) (transport);
506 transport->disconnected = TRUE;
508 _dbus_verbose ("end\n");
512 * Returns #TRUE if the transport has not been disconnected.
513 * Disconnection can result from _dbus_transport_disconnect()
514 * or because the server drops its end of the connection.
516 * @param transport the transport.
517 * @returns whether we're connected
520 _dbus_transport_get_is_connected (DBusTransport *transport)
522 return !transport->disconnected;
526 auth_via_unix_user_function (DBusTransport *transport)
528 DBusCredentials *auth_identity;
530 DBusConnection *connection;
531 DBusAllowUnixUserFunction unix_user_function;
532 void *unix_user_data;
535 /* Dropping the lock here probably isn't that safe. */
537 auth_identity = _dbus_auth_get_identity (transport->auth);
538 _dbus_assert (auth_identity != NULL);
540 connection = transport->connection;
541 unix_user_function = transport->unix_user_function;
542 unix_user_data = transport->unix_user_data;
543 uid = _dbus_credentials_get_unix_uid (auth_identity);
545 _dbus_verbose ("unlock\n");
546 _dbus_connection_unlock (connection);
548 allow = (* unix_user_function) (connection,
552 _dbus_verbose ("lock post unix user function\n");
553 _dbus_connection_lock (connection);
557 _dbus_verbose ("Client UID "DBUS_UID_FORMAT" authorized\n", uid);
561 _dbus_verbose ("Client UID "DBUS_UID_FORMAT
562 " was rejected, disconnecting\n",
563 _dbus_credentials_get_unix_uid (auth_identity));
564 _dbus_transport_disconnect (transport);
571 auth_via_windows_user_function (DBusTransport *transport)
573 DBusCredentials *auth_identity;
575 DBusConnection *connection;
576 DBusAllowWindowsUserFunction windows_user_function;
577 void *windows_user_data;
580 /* Dropping the lock here probably isn't that safe. */
582 auth_identity = _dbus_auth_get_identity (transport->auth);
583 _dbus_assert (auth_identity != NULL);
585 connection = transport->connection;
586 windows_user_function = transport->windows_user_function;
587 windows_user_data = transport->unix_user_data;
588 windows_sid = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
590 if (windows_sid == NULL)
596 _dbus_verbose ("unlock\n");
597 _dbus_connection_unlock (connection);
599 allow = (* windows_user_function) (connection,
603 _dbus_verbose ("lock post windows user function\n");
604 _dbus_connection_lock (connection);
608 _dbus_verbose ("Client SID '%s' authorized\n", windows_sid);
612 _dbus_verbose ("Client SID '%s' was rejected, disconnecting\n",
613 _dbus_credentials_get_windows_sid (auth_identity));
614 _dbus_transport_disconnect (transport);
621 auth_via_default_rules (DBusTransport *transport)
623 DBusCredentials *auth_identity;
624 DBusCredentials *our_identity;
627 auth_identity = _dbus_auth_get_identity (transport->auth);
628 _dbus_assert (auth_identity != NULL);
630 /* By default, connection is allowed if the client is 1) root or 2)
631 * has the same UID as us or 3) anonymous is allowed.
634 our_identity = _dbus_credentials_new_from_current_process ();
635 if (our_identity == NULL)
641 if (transport->allow_anonymous ||
642 _dbus_credentials_get_unix_uid (auth_identity) == 0 ||
643 _dbus_credentials_same_user (our_identity,
646 if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
647 _dbus_verbose ("Client authorized as SID '%s'"
648 "matching our SID '%s'\n",
649 _dbus_credentials_get_windows_sid(auth_identity),
650 _dbus_credentials_get_windows_sid(our_identity));
652 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
653 " matching our UID "DBUS_UID_FORMAT"\n",
654 _dbus_credentials_get_unix_uid(auth_identity),
655 _dbus_credentials_get_unix_uid(our_identity));
656 /* We have authenticated! */
661 if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
662 _dbus_verbose ("Client authorized as SID '%s'"
663 " but our SID is '%s', disconnecting\n",
664 (_dbus_credentials_get_windows_sid(auth_identity) ?
665 _dbus_credentials_get_windows_sid(auth_identity) : "<null>"),
666 (_dbus_credentials_get_windows_sid(our_identity) ?
667 _dbus_credentials_get_windows_sid(our_identity) : "<null>"));
669 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
670 " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
671 _dbus_credentials_get_unix_uid(auth_identity),
672 _dbus_credentials_get_unix_uid(our_identity));
673 _dbus_transport_disconnect (transport);
677 _dbus_credentials_unref (our_identity);
684 * Returns #TRUE if we have been authenticated. Will return #TRUE
685 * even if the transport is disconnected.
687 * @todo we drop connection->mutex when calling the unix_user_function,
688 * and windows_user_function, which may not be safe really.
690 * @param transport the transport
691 * @returns whether we're authenticated
694 _dbus_transport_get_is_authenticated (DBusTransport *transport)
696 if (transport->authenticated)
700 dbus_bool_t maybe_authenticated;
702 if (transport->disconnected)
705 /* paranoia ref since we call user callbacks sometimes */
706 _dbus_connection_ref_unlocked (transport->connection);
708 maybe_authenticated =
709 (!(transport->send_credentials_pending ||
710 transport->receive_credentials_pending));
712 if (maybe_authenticated)
714 switch (_dbus_auth_do_work (transport->auth))
716 case DBUS_AUTH_STATE_AUTHENTICATED:
717 /* leave as maybe_authenticated */
720 maybe_authenticated = FALSE;
724 /* If we're the client, verify the GUID
726 if (maybe_authenticated && !transport->is_server)
728 const char *server_guid;
730 server_guid = _dbus_auth_get_guid_from_server (transport->auth);
731 _dbus_assert (server_guid != NULL);
733 if (transport->expected_guid &&
734 strcmp (transport->expected_guid, server_guid) != 0)
736 _dbus_verbose ("Client expected GUID '%s' and we got '%s' from the server\n",
737 transport->expected_guid, server_guid);
738 _dbus_transport_disconnect (transport);
739 _dbus_connection_unref_unlocked (transport->connection);
743 if (transport->expected_guid == NULL)
745 transport->expected_guid = _dbus_strdup (server_guid);
747 if (transport->expected_guid == NULL)
749 _dbus_verbose ("No memory to complete auth\n");
755 /* If we're the server, see if we want to allow this identity to proceed.
757 if (maybe_authenticated && transport->is_server)
760 DBusCredentials *auth_identity;
762 auth_identity = _dbus_auth_get_identity (transport->auth);
763 _dbus_assert (auth_identity != NULL);
765 /* If we have an auth'd user and a user function, delegate
766 * deciding whether auth credentials are good enough to the
767 * app; otherwise, use our default decision process.
769 if (transport->unix_user_function != NULL &&
770 _dbus_credentials_include (auth_identity, DBUS_CREDENTIAL_UNIX_USER_ID))
772 allow = auth_via_unix_user_function (transport);
774 else if (transport->windows_user_function != NULL &&
775 _dbus_credentials_include (auth_identity, DBUS_CREDENTIAL_WINDOWS_SID))
777 allow = auth_via_windows_user_function (transport);
781 allow = auth_via_default_rules (transport);
785 maybe_authenticated = FALSE;
788 transport->authenticated = maybe_authenticated;
790 _dbus_connection_unref_unlocked (transport->connection);
791 return maybe_authenticated;
796 * See dbus_connection_get_is_anonymous().
798 * @param transport the transport
799 * @returns #TRUE if not authenticated or authenticated as anonymous
802 _dbus_transport_get_is_anonymous (DBusTransport *transport)
804 DBusCredentials *auth_identity;
806 if (!transport->authenticated)
809 auth_identity = _dbus_auth_get_identity (transport->auth);
811 if (_dbus_credentials_are_anonymous (auth_identity))
818 * Returns TRUE if the transport supports sending unix fds.
820 * @param transport the transport
821 * @returns #TRUE if TRUE it is possible to send unix fds across the transport.
824 _dbus_transport_can_pass_unix_fd(DBusTransport *transport)
826 return DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport);
830 * Gets the address of a transport. It will be
831 * #NULL for a server-side transport.
833 * @param transport the transport
834 * @returns transport's address
837 _dbus_transport_get_address (DBusTransport *transport)
839 return transport->address;
843 * Gets the id of the server we are connected to (see
844 * dbus_server_get_id()). Only works on client side.
846 * @param transport the transport
847 * @returns transport's server's id or #NULL if we are the server side
850 _dbus_transport_get_server_id (DBusTransport *transport)
852 if (transport->is_server)
855 return transport->expected_guid;
859 * Handles a watch by reading data, writing data, or disconnecting
860 * the transport, as appropriate for the given condition.
862 * @param transport the transport.
863 * @param watch the watch.
864 * @param condition the current state of the watched file descriptor.
865 * @returns #FALSE if not enough memory to fully handle the watch
868 _dbus_transport_handle_watch (DBusTransport *transport,
870 unsigned int condition)
874 _dbus_assert (transport->vtable->handle_watch != NULL);
876 if (transport->disconnected)
879 if (dbus_watch_get_socket (watch) < 0)
881 _dbus_warn_check_failed ("Tried to handle an invalidated watch; this watch should have been removed\n");
885 _dbus_watch_sanitize_condition (watch, &condition);
887 _dbus_transport_ref (transport);
888 _dbus_watch_ref (watch);
889 retval = (* transport->vtable->handle_watch) (transport, watch, condition);
890 _dbus_watch_unref (watch);
891 _dbus_transport_unref (transport);
897 * Sets the connection using this transport. Allows the transport
898 * to add watches to the connection, queue incoming messages,
899 * and pull outgoing messages.
901 * @param transport the transport.
902 * @param connection the connection.
903 * @returns #FALSE if not enough memory
906 _dbus_transport_set_connection (DBusTransport *transport,
907 DBusConnection *connection)
909 _dbus_assert (transport->vtable->connection_set != NULL);
910 _dbus_assert (transport->connection == NULL);
912 transport->connection = connection;
914 _dbus_transport_ref (transport);
915 if (!(* transport->vtable->connection_set) (transport))
916 transport->connection = NULL;
917 _dbus_transport_unref (transport);
919 return transport->connection != NULL;
923 * Get the socket file descriptor, if any.
925 * @param transport the transport
926 * @param fd_p pointer to fill in with the descriptor
927 * @returns #TRUE if a descriptor was available
930 _dbus_transport_get_socket_fd (DBusTransport *transport,
935 if (transport->vtable->get_socket_fd == NULL)
938 if (transport->disconnected)
941 _dbus_transport_ref (transport);
943 retval = (* transport->vtable->get_socket_fd) (transport,
946 _dbus_transport_unref (transport);
952 * Performs a single poll()/select() on the transport's file
953 * descriptors and then reads/writes data as appropriate,
954 * queueing incoming messages and sending outgoing messages.
955 * This is the backend for _dbus_connection_do_iteration().
956 * See _dbus_connection_do_iteration() for full details.
958 * @param transport the transport.
959 * @param flags indicates whether to read or write, and whether to block.
960 * @param timeout_milliseconds if blocking, timeout or -1 for no timeout.
963 _dbus_transport_do_iteration (DBusTransport *transport,
965 int timeout_milliseconds)
967 _dbus_assert (transport->vtable->do_iteration != NULL);
969 _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n",
970 flags, timeout_milliseconds, !transport->disconnected);
972 if ((flags & (DBUS_ITERATION_DO_WRITING |
973 DBUS_ITERATION_DO_READING)) == 0)
974 return; /* Nothing to do */
976 if (transport->disconnected)
979 _dbus_transport_ref (transport);
980 (* transport->vtable->do_iteration) (transport, flags,
981 timeout_milliseconds);
982 _dbus_transport_unref (transport);
984 _dbus_verbose ("end\n");
988 recover_unused_bytes (DBusTransport *transport)
990 if (_dbus_auth_needs_decoding (transport->auth))
992 DBusString plaintext;
993 const DBusString *encoded;
997 if (!_dbus_string_init (&plaintext))
1000 _dbus_auth_get_unused_bytes (transport->auth,
1003 if (!_dbus_auth_decode_data (transport->auth,
1004 encoded, &plaintext))
1006 _dbus_string_free (&plaintext);
1010 _dbus_message_loader_get_buffer (transport->loader,
1013 orig_len = _dbus_string_get_length (buffer);
1015 if (!_dbus_string_move (&plaintext, 0, buffer,
1018 _dbus_string_free (&plaintext);
1022 _dbus_verbose (" %d unused bytes sent to message loader\n",
1023 _dbus_string_get_length (buffer) -
1026 _dbus_message_loader_return_buffer (transport->loader,
1028 _dbus_string_get_length (buffer) -
1031 _dbus_auth_delete_unused_bytes (transport->auth);
1033 _dbus_string_free (&plaintext);
1037 const DBusString *bytes;
1040 dbus_bool_t succeeded;
1042 _dbus_message_loader_get_buffer (transport->loader,
1045 orig_len = _dbus_string_get_length (buffer);
1047 _dbus_auth_get_unused_bytes (transport->auth,
1051 if (!_dbus_string_copy (bytes, 0, buffer, _dbus_string_get_length (buffer)))
1054 _dbus_verbose (" %d unused bytes sent to message loader\n",
1055 _dbus_string_get_length (buffer) -
1058 _dbus_message_loader_return_buffer (transport->loader,
1060 _dbus_string_get_length (buffer) -
1064 _dbus_auth_delete_unused_bytes (transport->auth);
1072 _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
1077 * Reports our current dispatch status (whether there's buffered
1078 * data to be queued as messages, or not, or we need memory).
1080 * @param transport the transport
1081 * @returns current status
1084 _dbus_transport_get_dispatch_status (DBusTransport *transport)
1086 if (_dbus_counter_get_size_value (transport->live_messages) >= transport->max_live_messages_size ||
1087 _dbus_counter_get_unix_fd_value (transport->live_messages) >= transport->max_live_messages_unix_fds)
1088 return DBUS_DISPATCH_COMPLETE; /* complete for now */
1090 if (!_dbus_transport_get_is_authenticated (transport))
1092 if (_dbus_auth_do_work (transport->auth) ==
1093 DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
1094 return DBUS_DISPATCH_NEED_MEMORY;
1095 else if (!_dbus_transport_get_is_authenticated (transport))
1096 return DBUS_DISPATCH_COMPLETE;
1099 if (!transport->unused_bytes_recovered &&
1100 !recover_unused_bytes (transport))
1101 return DBUS_DISPATCH_NEED_MEMORY;
1103 transport->unused_bytes_recovered = TRUE;
1105 if (!_dbus_message_loader_queue_messages (transport->loader))
1106 return DBUS_DISPATCH_NEED_MEMORY;
1108 if (_dbus_message_loader_peek_message (transport->loader) != NULL)
1109 return DBUS_DISPATCH_DATA_REMAINS;
1111 return DBUS_DISPATCH_COMPLETE;
1115 * Processes data we've read while handling a watch, potentially
1116 * converting some of it to messages and queueing those messages on
1119 * @param transport the transport
1120 * @returns #TRUE if we had enough memory to queue all messages
1123 _dbus_transport_queue_messages (DBusTransport *transport)
1125 DBusDispatchStatus status;
1128 _dbus_verbose ("_dbus_transport_queue_messages()\n");
1131 /* Queue any messages */
1132 while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
1134 DBusMessage *message;
1137 link = _dbus_message_loader_pop_message_link (transport->loader);
1138 _dbus_assert (link != NULL);
1140 message = link->data;
1142 _dbus_verbose ("queueing received message %p\n", message);
1144 if (!_dbus_message_add_counter (message, transport->live_messages))
1146 _dbus_message_loader_putback_message_link (transport->loader,
1148 status = DBUS_DISPATCH_NEED_MEMORY;
1153 /* pass ownership of link and message ref to connection */
1154 _dbus_connection_queue_received_message_link (transport->connection,
1159 if (_dbus_message_loader_get_is_corrupted (transport->loader))
1161 _dbus_verbose ("Corrupted message stream, disconnecting\n");
1162 _dbus_transport_disconnect (transport);
1165 return status != DBUS_DISPATCH_NEED_MEMORY;
1169 * See dbus_connection_set_max_message_size().
1171 * @param transport the transport
1172 * @param size the max size of a single message
1175 _dbus_transport_set_max_message_size (DBusTransport *transport,
1178 _dbus_message_loader_set_max_message_size (transport->loader, size);
1182 * See dbus_connection_set_max_message_unix_fds().
1184 * @param transport the transport
1185 * @param n the max number of unix fds of a single message
1188 _dbus_transport_set_max_message_unix_fds (DBusTransport *transport,
1191 _dbus_message_loader_set_max_message_unix_fds (transport->loader, n);
1195 * See dbus_connection_get_max_message_size().
1197 * @param transport the transport
1198 * @returns max message size
1201 _dbus_transport_get_max_message_size (DBusTransport *transport)
1203 return _dbus_message_loader_get_max_message_size (transport->loader);
1207 * See dbus_connection_get_max_message_unix_fds().
1209 * @param transport the transport
1210 * @returns max message unix fds
1213 _dbus_transport_get_max_message_unix_fds (DBusTransport *transport)
1215 return _dbus_message_loader_get_max_message_unix_fds (transport->loader);
1219 * See dbus_connection_set_max_received_size().
1221 * @param transport the transport
1222 * @param size the max size of all incoming messages
1225 _dbus_transport_set_max_received_size (DBusTransport *transport,
1228 transport->max_live_messages_size = size;
1229 _dbus_counter_set_notify (transport->live_messages,
1230 transport->max_live_messages_size,
1231 transport->max_live_messages_unix_fds,
1232 live_messages_notify,
1237 * See dbus_connection_set_max_received_unix_fds().
1239 * @param transport the transport
1240 * @param n the max unix fds of all incoming messages
1243 _dbus_transport_set_max_received_unix_fds (DBusTransport *transport,
1246 transport->max_live_messages_unix_fds = n;
1247 _dbus_counter_set_notify (transport->live_messages,
1248 transport->max_live_messages_size,
1249 transport->max_live_messages_unix_fds,
1250 live_messages_notify,
1255 * See dbus_connection_get_max_received_size().
1257 * @param transport the transport
1258 * @returns max bytes for all live messages
1261 _dbus_transport_get_max_received_size (DBusTransport *transport)
1263 return transport->max_live_messages_size;
1267 * See dbus_connection_set_max_received_unix_fds().
1269 * @param transport the transport
1270 * @returns max unix fds for all live messages
1273 _dbus_transport_get_max_received_unix_fds (DBusTransport *transport)
1275 return transport->max_live_messages_unix_fds;
1279 * See dbus_connection_get_unix_user().
1281 * @param transport the transport
1282 * @param uid return location for the user ID
1283 * @returns #TRUE if uid is filled in with a valid user ID
1286 _dbus_transport_get_unix_user (DBusTransport *transport,
1289 DBusCredentials *auth_identity;
1291 *uid = _DBUS_INT32_MAX; /* better than some root or system user in
1292 * case of bugs in the caller. Caller should
1293 * never use this value on purpose, however.
1296 if (!transport->authenticated)
1299 auth_identity = _dbus_auth_get_identity (transport->auth);
1301 if (_dbus_credentials_include (auth_identity,
1302 DBUS_CREDENTIAL_UNIX_USER_ID))
1304 *uid = _dbus_credentials_get_unix_uid (auth_identity);
1312 * See dbus_connection_get_unix_process_id().
1314 * @param transport the transport
1315 * @param pid return location for the process ID
1316 * @returns #TRUE if uid is filled in with a valid process ID
1319 _dbus_transport_get_unix_process_id (DBusTransport *transport,
1322 DBusCredentials *auth_identity;
1324 *pid = DBUS_PID_UNSET; /* Caller should never use this value on purpose,
1325 * but we set it to a safe number, INT_MAX,
1326 * just to root out possible bugs in bad callers.
1329 if (!transport->authenticated)
1332 auth_identity = _dbus_auth_get_identity (transport->auth);
1334 if (_dbus_credentials_include (auth_identity,
1335 DBUS_CREDENTIAL_UNIX_PROCESS_ID))
1337 *pid = _dbus_credentials_get_unix_pid (auth_identity);
1345 * See dbus_connection_get_adt_audit_session_data().
1347 * @param transport the transport
1348 * @param data return location for the ADT audit data
1349 * @param data_size return length of audit data
1350 * @returns #TRUE if audit data is filled in with a valid ucred
1353 _dbus_transport_get_adt_audit_session_data (DBusTransport *transport,
1357 DBusCredentials *auth_identity;
1362 if (!transport->authenticated)
1365 auth_identity = _dbus_auth_get_identity (transport->auth);
1367 if (_dbus_credentials_include (auth_identity,
1368 DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID))
1370 *data = (void *) _dbus_credentials_get_adt_audit_data (auth_identity);
1371 *data_size = _dbus_credentials_get_adt_audit_data_size (auth_identity);
1379 * See dbus_connection_set_unix_user_function().
1381 * @param transport the transport
1382 * @param function the predicate
1383 * @param data data to pass to the predicate
1384 * @param free_data_function function to free the data
1385 * @param old_data the old user data to be freed
1386 * @param old_free_data_function old free data function to free it with
1389 _dbus_transport_set_unix_user_function (DBusTransport *transport,
1390 DBusAllowUnixUserFunction function,
1392 DBusFreeFunction free_data_function,
1394 DBusFreeFunction *old_free_data_function)
1396 *old_data = transport->unix_user_data;
1397 *old_free_data_function = transport->free_unix_user_data;
1399 transport->unix_user_function = function;
1400 transport->unix_user_data = data;
1401 transport->free_unix_user_data = free_data_function;
1405 * See dbus_connection_get_windows_user().
1407 * @param transport the transport
1408 * @param windows_sid_p return location for the user ID
1409 * @returns #TRUE if user is available; the returned value may still be #NULL if no memory to copy it
1412 _dbus_transport_get_windows_user (DBusTransport *transport,
1413 char **windows_sid_p)
1415 DBusCredentials *auth_identity;
1417 *windows_sid_p = NULL;
1419 if (!transport->authenticated)
1422 auth_identity = _dbus_auth_get_identity (transport->auth);
1424 if (_dbus_credentials_include (auth_identity,
1425 DBUS_CREDENTIAL_WINDOWS_SID))
1427 /* If no memory, we are supposed to return TRUE and set NULL */
1428 *windows_sid_p = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
1437 * See dbus_connection_set_windows_user_function().
1439 * @param transport the transport
1440 * @param function the predicate
1441 * @param data data to pass to the predicate
1442 * @param free_data_function function to free the data
1443 * @param old_data the old user data to be freed
1444 * @param old_free_data_function old free data function to free it with
1448 _dbus_transport_set_windows_user_function (DBusTransport *transport,
1449 DBusAllowWindowsUserFunction function,
1451 DBusFreeFunction free_data_function,
1453 DBusFreeFunction *old_free_data_function)
1455 *old_data = transport->windows_user_data;
1456 *old_free_data_function = transport->free_windows_user_data;
1458 transport->windows_user_function = function;
1459 transport->windows_user_data = data;
1460 transport->free_windows_user_data = free_data_function;
1464 * Sets the SASL authentication mechanisms supported by this transport.
1466 * @param transport the transport
1467 * @param mechanisms the #NULL-terminated array of mechanisms
1469 * @returns #FALSE if no memory
1472 _dbus_transport_set_auth_mechanisms (DBusTransport *transport,
1473 const char **mechanisms)
1475 return _dbus_auth_set_mechanisms (transport->auth, mechanisms);
1479 * See dbus_connection_set_allow_anonymous()
1481 * @param transport the transport
1482 * @param value #TRUE to allow anonymous connection
1485 _dbus_transport_set_allow_anonymous (DBusTransport *transport,
1488 transport->allow_anonymous = value != FALSE;