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 (const char *scope, 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 (scope, &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 const char *scope = dbus_address_entry_get_value (entry, "scope");
320 *transport_p = _dbus_transport_new_for_autolaunch (scope, error);
322 if (*transport_p == NULL)
324 _DBUS_ASSERT_ERROR_IS_SET (error);
325 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
329 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
330 return DBUS_TRANSPORT_OPEN_OK;
335 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
336 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
340 static const struct {
341 DBusTransportOpenResult (* func) (DBusAddressEntry *entry,
342 DBusTransport **transport_p,
345 { _dbus_transport_open_socket },
346 { _dbus_transport_open_platform_specific },
347 { _dbus_transport_open_autolaunch }
348 #ifdef DBUS_BUILD_TESTS
349 , { _dbus_transport_open_debug_pipe }
354 * Try to open a new transport for the given address entry. (This
355 * opens a client-side-of-the-connection transport.)
357 * @param entry the address entry
358 * @param error location to store reason for failure.
359 * @returns new transport of #NULL on failure.
362 _dbus_transport_open (DBusAddressEntry *entry,
365 DBusTransport *transport;
366 const char *expected_guid_orig;
369 DBusError tmp_error = DBUS_ERROR_INIT;
371 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
374 expected_guid_orig = dbus_address_entry_get_value (entry, "guid");
375 expected_guid = _dbus_strdup (expected_guid_orig);
377 if (expected_guid_orig != NULL && expected_guid == NULL)
379 _DBUS_SET_OOM (error);
383 for (i = 0; i < (int) _DBUS_N_ELEMENTS (open_funcs); ++i)
385 DBusTransportOpenResult result;
387 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
388 result = (* open_funcs[i].func) (entry, &transport, &tmp_error);
392 case DBUS_TRANSPORT_OPEN_OK:
393 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
396 case DBUS_TRANSPORT_OPEN_NOT_HANDLED:
397 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
398 /* keep going through the loop of open funcs */
400 case DBUS_TRANSPORT_OPEN_BAD_ADDRESS:
401 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
404 case DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT:
405 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
413 if (transport == NULL)
415 if (!dbus_error_is_set (&tmp_error))
416 _dbus_set_bad_address (&tmp_error,
418 "Unknown address type (examples of valid types are \"tcp\" and on UNIX \"unix\")");
420 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
421 dbus_move_error(&tmp_error, error);
422 dbus_free (expected_guid);
426 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
428 /* In the case of autostart the initial guid is NULL
429 * and the autostart transport recursively calls
430 * _dbus_open_transport wich returns a transport
431 * with a guid. That guid is the definitive one.
433 * FIXME: if more transports are added they may have
434 * an effect on the expected_guid semantics (i.e.
435 * expected_guid and transport->expected_guid may
436 * both have values). This is very unlikely though
437 * we should either throw asserts here for those
438 * corner cases or refactor the code so it is
439 * clearer on what is expected and what is not
442 transport->expected_guid = expected_guid;
449 * Increments the reference count for the transport.
451 * @param transport the transport.
452 * @returns the transport.
455 _dbus_transport_ref (DBusTransport *transport)
457 _dbus_assert (transport->refcount > 0);
459 transport->refcount += 1;
465 * Decrements the reference count for the transport.
466 * Disconnects and finalizes the transport if
467 * the reference count reaches zero.
469 * @param transport the transport.
472 _dbus_transport_unref (DBusTransport *transport)
474 _dbus_assert (transport != NULL);
475 _dbus_assert (transport->refcount > 0);
477 transport->refcount -= 1;
478 if (transport->refcount == 0)
480 _dbus_verbose ("finalizing\n");
482 _dbus_assert (transport->vtable->finalize != NULL);
484 (* transport->vtable->finalize) (transport);
489 * Closes our end of the connection to a remote application. Further
490 * attempts to use this transport will fail. Only the first call to
491 * _dbus_transport_disconnect() will have an effect.
493 * @param transport the transport.
497 _dbus_transport_disconnect (DBusTransport *transport)
499 _dbus_verbose ("start\n");
501 _dbus_assert (transport->vtable->disconnect != NULL);
503 if (transport->disconnected)
506 (* transport->vtable->disconnect) (transport);
508 transport->disconnected = TRUE;
510 _dbus_verbose ("end\n");
514 * Returns #TRUE if the transport has not been disconnected.
515 * Disconnection can result from _dbus_transport_disconnect()
516 * or because the server drops its end of the connection.
518 * @param transport the transport.
519 * @returns whether we're connected
522 _dbus_transport_get_is_connected (DBusTransport *transport)
524 return !transport->disconnected;
528 auth_via_unix_user_function (DBusTransport *transport)
530 DBusCredentials *auth_identity;
532 DBusConnection *connection;
533 DBusAllowUnixUserFunction unix_user_function;
534 void *unix_user_data;
537 /* Dropping the lock here probably isn't that safe. */
539 auth_identity = _dbus_auth_get_identity (transport->auth);
540 _dbus_assert (auth_identity != NULL);
542 connection = transport->connection;
543 unix_user_function = transport->unix_user_function;
544 unix_user_data = transport->unix_user_data;
545 uid = _dbus_credentials_get_unix_uid (auth_identity);
547 _dbus_verbose ("unlock\n");
548 _dbus_connection_unlock (connection);
550 allow = (* unix_user_function) (connection,
554 _dbus_verbose ("lock post unix user function\n");
555 _dbus_connection_lock (connection);
559 _dbus_verbose ("Client UID "DBUS_UID_FORMAT" authorized\n", uid);
563 _dbus_verbose ("Client UID "DBUS_UID_FORMAT
564 " was rejected, disconnecting\n",
565 _dbus_credentials_get_unix_uid (auth_identity));
566 _dbus_transport_disconnect (transport);
573 auth_via_windows_user_function (DBusTransport *transport)
575 DBusCredentials *auth_identity;
577 DBusConnection *connection;
578 DBusAllowWindowsUserFunction windows_user_function;
579 void *windows_user_data;
582 /* Dropping the lock here probably isn't that safe. */
584 auth_identity = _dbus_auth_get_identity (transport->auth);
585 _dbus_assert (auth_identity != NULL);
587 connection = transport->connection;
588 windows_user_function = transport->windows_user_function;
589 windows_user_data = transport->unix_user_data;
590 windows_sid = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
592 if (windows_sid == NULL)
598 _dbus_verbose ("unlock\n");
599 _dbus_connection_unlock (connection);
601 allow = (* windows_user_function) (connection,
605 _dbus_verbose ("lock post windows user function\n");
606 _dbus_connection_lock (connection);
610 _dbus_verbose ("Client SID '%s' authorized\n", windows_sid);
614 _dbus_verbose ("Client SID '%s' was rejected, disconnecting\n",
615 _dbus_credentials_get_windows_sid (auth_identity));
616 _dbus_transport_disconnect (transport);
623 auth_via_default_rules (DBusTransport *transport)
625 DBusCredentials *auth_identity;
626 DBusCredentials *our_identity;
629 auth_identity = _dbus_auth_get_identity (transport->auth);
630 _dbus_assert (auth_identity != NULL);
632 /* By default, connection is allowed if the client is 1) root or 2)
633 * has the same UID as us or 3) anonymous is allowed.
636 our_identity = _dbus_credentials_new_from_current_process ();
637 if (our_identity == NULL)
643 if (transport->allow_anonymous ||
644 _dbus_credentials_get_unix_uid (auth_identity) == 0 ||
645 _dbus_credentials_same_user (our_identity,
648 if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
649 _dbus_verbose ("Client authorized as SID '%s'"
650 "matching our SID '%s'\n",
651 _dbus_credentials_get_windows_sid(auth_identity),
652 _dbus_credentials_get_windows_sid(our_identity));
654 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
655 " matching our UID "DBUS_UID_FORMAT"\n",
656 _dbus_credentials_get_unix_uid(auth_identity),
657 _dbus_credentials_get_unix_uid(our_identity));
658 /* We have authenticated! */
663 if (_dbus_credentials_include(our_identity,DBUS_CREDENTIAL_WINDOWS_SID))
664 _dbus_verbose ("Client authorized as SID '%s'"
665 " but our SID is '%s', disconnecting\n",
666 (_dbus_credentials_get_windows_sid(auth_identity) ?
667 _dbus_credentials_get_windows_sid(auth_identity) : "<null>"),
668 (_dbus_credentials_get_windows_sid(our_identity) ?
669 _dbus_credentials_get_windows_sid(our_identity) : "<null>"));
671 _dbus_verbose ("Client authorized as UID "DBUS_UID_FORMAT
672 " but our UID is "DBUS_UID_FORMAT", disconnecting\n",
673 _dbus_credentials_get_unix_uid(auth_identity),
674 _dbus_credentials_get_unix_uid(our_identity));
675 _dbus_transport_disconnect (transport);
679 _dbus_credentials_unref (our_identity);
686 * Returns #TRUE if we have been authenticated. Will return #TRUE
687 * even if the transport is disconnected.
689 * @todo we drop connection->mutex when calling the unix_user_function,
690 * and windows_user_function, which may not be safe really.
692 * @param transport the transport
693 * @returns whether we're authenticated
696 _dbus_transport_get_is_authenticated (DBusTransport *transport)
698 if (transport->authenticated)
702 dbus_bool_t maybe_authenticated;
704 if (transport->disconnected)
707 /* paranoia ref since we call user callbacks sometimes */
708 _dbus_connection_ref_unlocked (transport->connection);
710 maybe_authenticated =
711 (!(transport->send_credentials_pending ||
712 transport->receive_credentials_pending));
714 if (maybe_authenticated)
716 switch (_dbus_auth_do_work (transport->auth))
718 case DBUS_AUTH_STATE_AUTHENTICATED:
719 /* leave as maybe_authenticated */
722 maybe_authenticated = FALSE;
726 /* If we're the client, verify the GUID
728 if (maybe_authenticated && !transport->is_server)
730 const char *server_guid;
732 server_guid = _dbus_auth_get_guid_from_server (transport->auth);
733 _dbus_assert (server_guid != NULL);
735 if (transport->expected_guid &&
736 strcmp (transport->expected_guid, server_guid) != 0)
738 _dbus_verbose ("Client expected GUID '%s' and we got '%s' from the server\n",
739 transport->expected_guid, server_guid);
740 _dbus_transport_disconnect (transport);
741 _dbus_connection_unref_unlocked (transport->connection);
745 if (transport->expected_guid == NULL)
747 transport->expected_guid = _dbus_strdup (server_guid);
749 if (transport->expected_guid == NULL)
751 _dbus_verbose ("No memory to complete auth\n");
757 /* If we're the server, see if we want to allow this identity to proceed.
759 if (maybe_authenticated && transport->is_server)
762 DBusCredentials *auth_identity;
764 auth_identity = _dbus_auth_get_identity (transport->auth);
765 _dbus_assert (auth_identity != NULL);
767 /* If we have an auth'd user and a user function, delegate
768 * deciding whether auth credentials are good enough to the
769 * app; otherwise, use our default decision process.
771 if (transport->unix_user_function != NULL &&
772 _dbus_credentials_include (auth_identity, DBUS_CREDENTIAL_UNIX_USER_ID))
774 allow = auth_via_unix_user_function (transport);
776 else if (transport->windows_user_function != NULL &&
777 _dbus_credentials_include (auth_identity, DBUS_CREDENTIAL_WINDOWS_SID))
779 allow = auth_via_windows_user_function (transport);
783 allow = auth_via_default_rules (transport);
787 maybe_authenticated = FALSE;
790 transport->authenticated = maybe_authenticated;
792 _dbus_connection_unref_unlocked (transport->connection);
793 return maybe_authenticated;
798 * See dbus_connection_get_is_anonymous().
800 * @param transport the transport
801 * @returns #TRUE if not authenticated or authenticated as anonymous
804 _dbus_transport_get_is_anonymous (DBusTransport *transport)
806 DBusCredentials *auth_identity;
808 if (!transport->authenticated)
811 auth_identity = _dbus_auth_get_identity (transport->auth);
813 if (_dbus_credentials_are_anonymous (auth_identity))
820 * Returns TRUE if the transport supports sending unix fds.
822 * @param transport the transport
823 * @returns #TRUE if TRUE it is possible to send unix fds across the transport.
826 _dbus_transport_can_pass_unix_fd(DBusTransport *transport)
828 return DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport);
832 * Gets the address of a transport. It will be
833 * #NULL for a server-side transport.
835 * @param transport the transport
836 * @returns transport's address
839 _dbus_transport_get_address (DBusTransport *transport)
841 return transport->address;
845 * Gets the id of the server we are connected to (see
846 * dbus_server_get_id()). Only works on client side.
848 * @param transport the transport
849 * @returns transport's server's id or #NULL if we are the server side
852 _dbus_transport_get_server_id (DBusTransport *transport)
854 if (transport->is_server)
857 return transport->expected_guid;
861 * Handles a watch by reading data, writing data, or disconnecting
862 * the transport, as appropriate for the given condition.
864 * @param transport the transport.
865 * @param watch the watch.
866 * @param condition the current state of the watched file descriptor.
867 * @returns #FALSE if not enough memory to fully handle the watch
870 _dbus_transport_handle_watch (DBusTransport *transport,
872 unsigned int condition)
876 _dbus_assert (transport->vtable->handle_watch != NULL);
878 if (transport->disconnected)
881 if (dbus_watch_get_socket (watch) < 0)
883 _dbus_warn_check_failed ("Tried to handle an invalidated watch; this watch should have been removed\n");
887 _dbus_watch_sanitize_condition (watch, &condition);
889 _dbus_transport_ref (transport);
890 _dbus_watch_ref (watch);
891 retval = (* transport->vtable->handle_watch) (transport, watch, condition);
892 _dbus_watch_unref (watch);
893 _dbus_transport_unref (transport);
899 * Sets the connection using this transport. Allows the transport
900 * to add watches to the connection, queue incoming messages,
901 * and pull outgoing messages.
903 * @param transport the transport.
904 * @param connection the connection.
905 * @returns #FALSE if not enough memory
908 _dbus_transport_set_connection (DBusTransport *transport,
909 DBusConnection *connection)
911 _dbus_assert (transport->vtable->connection_set != NULL);
912 _dbus_assert (transport->connection == NULL);
914 transport->connection = connection;
916 _dbus_transport_ref (transport);
917 if (!(* transport->vtable->connection_set) (transport))
918 transport->connection = NULL;
919 _dbus_transport_unref (transport);
921 return transport->connection != NULL;
925 * Get the socket file descriptor, if any.
927 * @param transport the transport
928 * @param fd_p pointer to fill in with the descriptor
929 * @returns #TRUE if a descriptor was available
932 _dbus_transport_get_socket_fd (DBusTransport *transport,
937 if (transport->vtable->get_socket_fd == NULL)
940 if (transport->disconnected)
943 _dbus_transport_ref (transport);
945 retval = (* transport->vtable->get_socket_fd) (transport,
948 _dbus_transport_unref (transport);
954 * Performs a single poll()/select() on the transport's file
955 * descriptors and then reads/writes data as appropriate,
956 * queueing incoming messages and sending outgoing messages.
957 * This is the backend for _dbus_connection_do_iteration().
958 * See _dbus_connection_do_iteration() for full details.
960 * @param transport the transport.
961 * @param flags indicates whether to read or write, and whether to block.
962 * @param timeout_milliseconds if blocking, timeout or -1 for no timeout.
965 _dbus_transport_do_iteration (DBusTransport *transport,
967 int timeout_milliseconds)
969 _dbus_assert (transport->vtable->do_iteration != NULL);
971 _dbus_verbose ("Transport iteration flags 0x%x timeout %d connected = %d\n",
972 flags, timeout_milliseconds, !transport->disconnected);
974 if ((flags & (DBUS_ITERATION_DO_WRITING |
975 DBUS_ITERATION_DO_READING)) == 0)
976 return; /* Nothing to do */
978 if (transport->disconnected)
981 _dbus_transport_ref (transport);
982 (* transport->vtable->do_iteration) (transport, flags,
983 timeout_milliseconds);
984 _dbus_transport_unref (transport);
986 _dbus_verbose ("end\n");
990 recover_unused_bytes (DBusTransport *transport)
992 if (_dbus_auth_needs_decoding (transport->auth))
994 DBusString plaintext;
995 const DBusString *encoded;
999 if (!_dbus_string_init (&plaintext))
1002 _dbus_auth_get_unused_bytes (transport->auth,
1005 if (!_dbus_auth_decode_data (transport->auth,
1006 encoded, &plaintext))
1008 _dbus_string_free (&plaintext);
1012 _dbus_message_loader_get_buffer (transport->loader,
1015 orig_len = _dbus_string_get_length (buffer);
1017 if (!_dbus_string_move (&plaintext, 0, buffer,
1020 _dbus_string_free (&plaintext);
1024 _dbus_verbose (" %d unused bytes sent to message loader\n",
1025 _dbus_string_get_length (buffer) -
1028 _dbus_message_loader_return_buffer (transport->loader,
1030 _dbus_string_get_length (buffer) -
1033 _dbus_auth_delete_unused_bytes (transport->auth);
1035 _dbus_string_free (&plaintext);
1039 const DBusString *bytes;
1042 dbus_bool_t succeeded;
1044 _dbus_message_loader_get_buffer (transport->loader,
1047 orig_len = _dbus_string_get_length (buffer);
1049 _dbus_auth_get_unused_bytes (transport->auth,
1053 if (!_dbus_string_copy (bytes, 0, buffer, _dbus_string_get_length (buffer)))
1056 _dbus_verbose (" %d unused bytes sent to message loader\n",
1057 _dbus_string_get_length (buffer) -
1060 _dbus_message_loader_return_buffer (transport->loader,
1062 _dbus_string_get_length (buffer) -
1066 _dbus_auth_delete_unused_bytes (transport->auth);
1074 _dbus_verbose ("Not enough memory to transfer unused bytes from auth conversation\n");
1079 * Reports our current dispatch status (whether there's buffered
1080 * data to be queued as messages, or not, or we need memory).
1082 * @param transport the transport
1083 * @returns current status
1086 _dbus_transport_get_dispatch_status (DBusTransport *transport)
1088 if (_dbus_counter_get_size_value (transport->live_messages) >= transport->max_live_messages_size ||
1089 _dbus_counter_get_unix_fd_value (transport->live_messages) >= transport->max_live_messages_unix_fds)
1090 return DBUS_DISPATCH_COMPLETE; /* complete for now */
1092 if (!_dbus_transport_get_is_authenticated (transport))
1094 if (_dbus_auth_do_work (transport->auth) ==
1095 DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
1096 return DBUS_DISPATCH_NEED_MEMORY;
1097 else if (!_dbus_transport_get_is_authenticated (transport))
1098 return DBUS_DISPATCH_COMPLETE;
1101 if (!transport->unused_bytes_recovered &&
1102 !recover_unused_bytes (transport))
1103 return DBUS_DISPATCH_NEED_MEMORY;
1105 transport->unused_bytes_recovered = TRUE;
1107 if (!_dbus_message_loader_queue_messages (transport->loader))
1108 return DBUS_DISPATCH_NEED_MEMORY;
1110 if (_dbus_message_loader_peek_message (transport->loader) != NULL)
1111 return DBUS_DISPATCH_DATA_REMAINS;
1113 return DBUS_DISPATCH_COMPLETE;
1117 * Processes data we've read while handling a watch, potentially
1118 * converting some of it to messages and queueing those messages on
1121 * @param transport the transport
1122 * @returns #TRUE if we had enough memory to queue all messages
1125 _dbus_transport_queue_messages (DBusTransport *transport)
1127 DBusDispatchStatus status;
1130 _dbus_verbose ("_dbus_transport_queue_messages()\n");
1133 /* Queue any messages */
1134 while ((status = _dbus_transport_get_dispatch_status (transport)) == DBUS_DISPATCH_DATA_REMAINS)
1136 DBusMessage *message;
1139 link = _dbus_message_loader_pop_message_link (transport->loader);
1140 _dbus_assert (link != NULL);
1142 message = link->data;
1144 _dbus_verbose ("queueing received message %p\n", message);
1146 if (!_dbus_message_add_counter (message, transport->live_messages))
1148 _dbus_message_loader_putback_message_link (transport->loader,
1150 status = DBUS_DISPATCH_NEED_MEMORY;
1155 /* pass ownership of link and message ref to connection */
1156 _dbus_connection_queue_received_message_link (transport->connection,
1161 if (_dbus_message_loader_get_is_corrupted (transport->loader))
1163 _dbus_verbose ("Corrupted message stream, disconnecting\n");
1164 _dbus_transport_disconnect (transport);
1167 return status != DBUS_DISPATCH_NEED_MEMORY;
1171 * See dbus_connection_set_max_message_size().
1173 * @param transport the transport
1174 * @param size the max size of a single message
1177 _dbus_transport_set_max_message_size (DBusTransport *transport,
1180 _dbus_message_loader_set_max_message_size (transport->loader, size);
1184 * See dbus_connection_set_max_message_unix_fds().
1186 * @param transport the transport
1187 * @param n the max number of unix fds of a single message
1190 _dbus_transport_set_max_message_unix_fds (DBusTransport *transport,
1193 _dbus_message_loader_set_max_message_unix_fds (transport->loader, n);
1197 * See dbus_connection_get_max_message_size().
1199 * @param transport the transport
1200 * @returns max message size
1203 _dbus_transport_get_max_message_size (DBusTransport *transport)
1205 return _dbus_message_loader_get_max_message_size (transport->loader);
1209 * See dbus_connection_get_max_message_unix_fds().
1211 * @param transport the transport
1212 * @returns max message unix fds
1215 _dbus_transport_get_max_message_unix_fds (DBusTransport *transport)
1217 return _dbus_message_loader_get_max_message_unix_fds (transport->loader);
1221 * See dbus_connection_set_max_received_size().
1223 * @param transport the transport
1224 * @param size the max size of all incoming messages
1227 _dbus_transport_set_max_received_size (DBusTransport *transport,
1230 transport->max_live_messages_size = size;
1231 _dbus_counter_set_notify (transport->live_messages,
1232 transport->max_live_messages_size,
1233 transport->max_live_messages_unix_fds,
1234 live_messages_notify,
1239 * See dbus_connection_set_max_received_unix_fds().
1241 * @param transport the transport
1242 * @param n the max unix fds of all incoming messages
1245 _dbus_transport_set_max_received_unix_fds (DBusTransport *transport,
1248 transport->max_live_messages_unix_fds = n;
1249 _dbus_counter_set_notify (transport->live_messages,
1250 transport->max_live_messages_size,
1251 transport->max_live_messages_unix_fds,
1252 live_messages_notify,
1257 * See dbus_connection_get_max_received_size().
1259 * @param transport the transport
1260 * @returns max bytes for all live messages
1263 _dbus_transport_get_max_received_size (DBusTransport *transport)
1265 return transport->max_live_messages_size;
1269 * See dbus_connection_set_max_received_unix_fds().
1271 * @param transport the transport
1272 * @returns max unix fds for all live messages
1275 _dbus_transport_get_max_received_unix_fds (DBusTransport *transport)
1277 return transport->max_live_messages_unix_fds;
1281 * See dbus_connection_get_unix_user().
1283 * @param transport the transport
1284 * @param uid return location for the user ID
1285 * @returns #TRUE if uid is filled in with a valid user ID
1288 _dbus_transport_get_unix_user (DBusTransport *transport,
1291 DBusCredentials *auth_identity;
1293 *uid = _DBUS_INT32_MAX; /* better than some root or system user in
1294 * case of bugs in the caller. Caller should
1295 * never use this value on purpose, however.
1298 if (!transport->authenticated)
1301 auth_identity = _dbus_auth_get_identity (transport->auth);
1303 if (_dbus_credentials_include (auth_identity,
1304 DBUS_CREDENTIAL_UNIX_USER_ID))
1306 *uid = _dbus_credentials_get_unix_uid (auth_identity);
1314 * See dbus_connection_get_unix_process_id().
1316 * @param transport the transport
1317 * @param pid return location for the process ID
1318 * @returns #TRUE if uid is filled in with a valid process ID
1321 _dbus_transport_get_unix_process_id (DBusTransport *transport,
1324 DBusCredentials *auth_identity;
1326 *pid = DBUS_PID_UNSET; /* Caller should never use this value on purpose,
1327 * but we set it to a safe number, INT_MAX,
1328 * just to root out possible bugs in bad callers.
1331 if (!transport->authenticated)
1334 auth_identity = _dbus_auth_get_identity (transport->auth);
1336 if (_dbus_credentials_include (auth_identity,
1337 DBUS_CREDENTIAL_UNIX_PROCESS_ID))
1339 *pid = _dbus_credentials_get_unix_pid (auth_identity);
1347 * See dbus_connection_get_adt_audit_session_data().
1349 * @param transport the transport
1350 * @param data return location for the ADT audit data
1351 * @param data_size return length of audit data
1352 * @returns #TRUE if audit data is filled in with a valid ucred
1355 _dbus_transport_get_adt_audit_session_data (DBusTransport *transport,
1359 DBusCredentials *auth_identity;
1364 if (!transport->authenticated)
1367 auth_identity = _dbus_auth_get_identity (transport->auth);
1369 if (_dbus_credentials_include (auth_identity,
1370 DBUS_CREDENTIAL_ADT_AUDIT_DATA_ID))
1372 *data = (void *) _dbus_credentials_get_adt_audit_data (auth_identity);
1373 *data_size = _dbus_credentials_get_adt_audit_data_size (auth_identity);
1381 * See dbus_connection_set_unix_user_function().
1383 * @param transport the transport
1384 * @param function the predicate
1385 * @param data data to pass to the predicate
1386 * @param free_data_function function to free the data
1387 * @param old_data the old user data to be freed
1388 * @param old_free_data_function old free data function to free it with
1391 _dbus_transport_set_unix_user_function (DBusTransport *transport,
1392 DBusAllowUnixUserFunction function,
1394 DBusFreeFunction free_data_function,
1396 DBusFreeFunction *old_free_data_function)
1398 *old_data = transport->unix_user_data;
1399 *old_free_data_function = transport->free_unix_user_data;
1401 transport->unix_user_function = function;
1402 transport->unix_user_data = data;
1403 transport->free_unix_user_data = free_data_function;
1407 * See dbus_connection_get_windows_user().
1409 * @param transport the transport
1410 * @param windows_sid_p return location for the user ID
1411 * @returns #TRUE if user is available; the returned value may still be #NULL if no memory to copy it
1414 _dbus_transport_get_windows_user (DBusTransport *transport,
1415 char **windows_sid_p)
1417 DBusCredentials *auth_identity;
1419 *windows_sid_p = NULL;
1421 if (!transport->authenticated)
1424 auth_identity = _dbus_auth_get_identity (transport->auth);
1426 if (_dbus_credentials_include (auth_identity,
1427 DBUS_CREDENTIAL_WINDOWS_SID))
1429 /* If no memory, we are supposed to return TRUE and set NULL */
1430 *windows_sid_p = _dbus_strdup (_dbus_credentials_get_windows_sid (auth_identity));
1439 * See dbus_connection_set_windows_user_function().
1441 * @param transport the transport
1442 * @param function the predicate
1443 * @param data data to pass to the predicate
1444 * @param free_data_function function to free the data
1445 * @param old_data the old user data to be freed
1446 * @param old_free_data_function old free data function to free it with
1450 _dbus_transport_set_windows_user_function (DBusTransport *transport,
1451 DBusAllowWindowsUserFunction function,
1453 DBusFreeFunction free_data_function,
1455 DBusFreeFunction *old_free_data_function)
1457 *old_data = transport->windows_user_data;
1458 *old_free_data_function = transport->free_windows_user_data;
1460 transport->windows_user_function = function;
1461 transport->windows_user_data = data;
1462 transport->free_windows_user_data = free_data_function;
1466 * Sets the SASL authentication mechanisms supported by this transport.
1468 * @param transport the transport
1469 * @param mechanisms the #NULL-terminated array of mechanisms
1471 * @returns #FALSE if no memory
1474 _dbus_transport_set_auth_mechanisms (DBusTransport *transport,
1475 const char **mechanisms)
1477 return _dbus_auth_set_mechanisms (transport->auth, mechanisms);
1481 * See dbus_connection_set_allow_anonymous()
1483 * @param transport the transport
1484 * @param value #TRUE to allow anonymous connection
1487 _dbus_transport_set_allow_anonymous (DBusTransport *transport,
1490 transport->allow_anonymous = value != FALSE;